Skip to content

Commit

Permalink
fix: use callback to debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
swouf committed May 27, 2024
1 parent 161ec85 commit 8d6976e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/config/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const DEFAULT_LANG = 'en';
export const DEBOUNCE_SAVE_MS = 500;
27 changes: 17 additions & 10 deletions src/modules/context/UserAnswersContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
FC,
ReactElement,
createContext,
useCallback,
useContext,
useEffect,
useMemo,
Expand All @@ -20,6 +21,7 @@ import {
UserAnswerAppData,
getDefaultUserAnswerAppData,
} from '@/config/appData';
import { DEBOUNCE_SAVE_MS } from '@/config/constants';
import { hooks, mutations } from '@/config/queryClient';
import { UserAnswer, UserAnswerStatus } from '@/interfaces/userAnswer';

Expand Down Expand Up @@ -54,7 +56,7 @@ export const UserAnswersProvider: FC<{
useState<UserAnswerAppData[]>();

const cachePayload = useRef<UserAnswer>();
const debouncedPatch = useRef<ReturnType<typeof debounce>>();
// const debouncedPatch = useRef<ReturnType<typeof debounce>>();
const { mutate: postAppData } = mutations.usePostAppData();
const { mutate: patchAppData } = mutations.usePatchAppData();
const { mutate: deleteAppData } = mutations.useDeleteAppData();
Expand All @@ -81,6 +83,14 @@ export const UserAnswersProvider: FC<{
}
}, [isSuccess, data, memberId]);

// eslint-disable-next-line react-hooks/exhaustive-deps
const patchCachedUserAnswer = useCallback(
debounce((payload) => {
patchAppData(payload);
}, DEBOUNCE_SAVE_MS),
[patchAppData],
);

const setAnswer = useMemo(
() =>
(answer: string): void => {
Expand All @@ -91,20 +101,17 @@ export const UserAnswersProvider: FC<{
: UserAnswerStatus.Saved,
};
if (userAnswerAppData?.id) {
// Eventually useless
cachePayload.current = payloadData;
debouncedPatch.current = debounce(
() =>
patchAppData({
...userAnswerAppData,
data: cachePayload.current,
}),
500,
);
patchCachedUserAnswer({
...userAnswerAppData,
data: cachePayload.current,
});
} else {
postAppData(getDefaultUserAnswerAppData(payloadData));
}
},
[autosubmit, patchAppData, postAppData, userAnswerAppData],
[autosubmit, patchCachedUserAnswer, postAppData, userAnswerAppData],
);

const submitAnswer = useMemo(
Expand Down
2 changes: 1 addition & 1 deletion src/modules/question-view/QuestionView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const QuestionView = (): JSX.Element => {
<Collapse orientation="horizontal" in={showResetButton}>
<Tooltip title={t('RESET_ANSWER')}>
<Button
disabled={answer.length === 0}
disabled={!userAnswer}
variant="outlined"
onClick={() => deleteAnswer()}
startIcon={<ReplayIcon />}
Expand Down

0 comments on commit 8d6976e

Please sign in to comment.