Skip to content

Commit

Permalink
fix: use query status to update
Browse files Browse the repository at this point in the history
  • Loading branch information
swouf committed Jun 19, 2024
1 parent febf8ba commit 329a10d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/modules/context/UserAnswersContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -47,7 +49,7 @@ const UserAnswersContext =
export const UserAnswersProvider: FC<{
children: ReactElement | ReactElement[];
}> = ({ children }) => {
const { data, isSuccess } = hooks.useAppData<UserAnswer>({
const { data, isSuccess, status } = hooks.useAppData<UserAnswer>({
type: AppDataType.UserAnswer,
});
const [userAnswerAppData, setUserAnswerAppData] =
Expand Down Expand Up @@ -150,12 +152,14 @@ export const UserAnswersProvider: FC<{
submitAnswer,
allAnswersAppData: isAdmin ? allAnswersAppData : undefined,
deleteAnswer,
status,
}),
[
allAnswersAppData,
deleteAnswer,
isAdmin,
setAnswer,
status,
submitAnswer,
userAnswerAppData?.data,
],
Expand Down
5 changes: 3 additions & 2 deletions src/modules/question-view/QuestionView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const QuestionView = (): JSX.Element => {
deleteAnswer,
submitAnswer,
setAnswer: setSavedAnswer,
status,
} = useUserAnswers();

const [answer, setAnswer] = useState<string>('');
Expand All @@ -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(
Expand Down

0 comments on commit 329a10d

Please sign in to comment.