From 329a10d30de1cc0a7c58b3bf3ead0e4c037d64f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20La=20Scala?= Date: Tue, 18 Jun 2024 14:29:48 +0200 Subject: [PATCH] fix: use query status to update --- src/modules/context/UserAnswersContext.tsx | 6 +++++- src/modules/question-view/QuestionView.tsx | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/modules/context/UserAnswersContext.tsx b/src/modules/context/UserAnswersContext.tsx index 61feb698..6333cd3d 100644 --- a/src/modules/context/UserAnswersContext.tsx +++ b/src/modules/context/UserAnswersContext.tsx @@ -33,12 +33,14 @@ type UserAnswersContextType = { submitAnswer: () => void; deleteAnswer: (id?: UserAnswerAppData['id']) => void; allAnswersAppData?: UserAnswerAppData[]; + status: 'loading' | 'error' | 'success'; }; const defaultContextValue: UserAnswersContextType = { setAnswer: () => null, submitAnswer: () => null, deleteAnswer: () => null, + status: 'loading', }; const UserAnswersContext = @@ -47,7 +49,7 @@ const UserAnswersContext = export const UserAnswersProvider: FC<{ children: ReactElement | ReactElement[]; }> = ({ children }) => { - const { data, isSuccess } = hooks.useAppData({ + const { data, isSuccess, status } = hooks.useAppData({ type: AppDataType.UserAnswer, }); const [userAnswerAppData, setUserAnswerAppData] = @@ -150,12 +152,14 @@ export const UserAnswersProvider: FC<{ submitAnswer, allAnswersAppData: isAdmin ? allAnswersAppData : undefined, deleteAnswer, + status, }), [ allAnswersAppData, deleteAnswer, isAdmin, setAnswer, + status, submitAnswer, userAnswerAppData?.data, ], diff --git a/src/modules/question-view/QuestionView.tsx b/src/modules/question-view/QuestionView.tsx index 8fdb0a1d..f83b7f4d 100644 --- a/src/modules/question-view/QuestionView.tsx +++ b/src/modules/question-view/QuestionView.tsx @@ -43,6 +43,7 @@ const QuestionView = (): JSX.Element => { deleteAnswer, submitAnswer, setAnswer: setSavedAnswer, + status, } = useUserAnswers(); const [answer, setAnswer] = useState(''); @@ -55,11 +56,11 @@ const QuestionView = (): JSX.Element => { // Update the answer if the stored value change useEffect(() => { - if (!isInit) { + if (!isInit && status === 'success' && answer.length === 0) { setAnswer(userAnswer?.answer ?? ''); setIsInit(true); } - }, [isInit, userAnswer]); + }, [answer.length, isInit, status, userAnswer]); const answerStatus = useMemo(() => userAnswer?.status, [userAnswer?.status]); const showSubmitButton = useMemo(