Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On send survey add surveyId to LocalStorage and block multiple replies #128

Merged
merged 1 commit into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/features/surveys/constants/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum LocalStorageKeys {
LocalStorageKey = 'surveyId',
}
21 changes: 21 additions & 0 deletions src/features/surveys/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useEffect, useState } from 'react';

function useLocalStorage<T>(defaultValue: T, key: string) {
const [value, setValue] = useState(defaultValue);

useEffect(() => {
if (defaultValue === value) {
return;
}
window.localStorage.setItem(key, JSON.stringify(value));
}, [defaultValue, key, value]);

useEffect(() => {
const storageValue = window.localStorage.getItem(key);
setValue(storageValue != null ? JSON.parse(storageValue) : defaultValue);
}, [defaultValue, key]);

return [value, setValue] as const;
}

export default useLocalStorage;
16 changes: 14 additions & 2 deletions src/features/surveys/managers/surveyAnswerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { useRouter } from 'next/router';
import { useState, useEffect, useCallback } from 'react';
import toast from 'react-hot-toast';
import { db } from 'firebaseConfiguration';
import { LocalStorageKeys } from 'features/surveys/constants/types';
import useLocalStorage from 'features/surveys/hooks/useLocalStorage';

const DEFAULT_VALUE: string[] = [];

export const useSurveyAnswerManager = () => {
const router = useRouter();
Expand All @@ -16,6 +20,10 @@ export const useSurveyAnswerManager = () => {
const [buttonDisable, setButtonDisable] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [isAnswering, setIsAnswering] = useState(false);
const [localStorageValue, setLocalStorageValue] = useLocalStorage<string[]>(
DEFAULT_VALUE,
LocalStorageKeys.LocalStorageKey
);

const getSurveyData = useCallback(async () => {
const surveyData = await getDoc(doc(db, 'surveys', surveyId));
Expand All @@ -33,9 +41,12 @@ export const useSurveyAnswerManager = () => {
if (surveyId) {
getSurveyData();
}

if (localStorageValue.includes(surveyId) && !isAnswering) {
router.replace('/');
toast.success('You have answered this survey');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [localStorageValue]);

const handleIconClick = (icon: string) => {
setSelectedIcon(icon);
Expand All @@ -55,6 +66,7 @@ export const useSurveyAnswerManager = () => {
answer,
answerDate: new Date(),
});
setLocalStorageValue([...localStorageValue, surveyId]);
await router.replace('/');
toast.success('The reply has been sent');
} catch (error) {
Expand Down