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

fix: update local state conditionally and remove white background #45

Merged
merged 2 commits into from
Jun 25, 2024
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
1 change: 1 addition & 0 deletions src/modules/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const theme = createTheme({
secondary: pink,
default: grey['500'],
background: {
default: 'transparent',
paper: '#fff',
},
},
Expand Down
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
19 changes: 16 additions & 3 deletions src/modules/question-view/QuestionView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ const QuestionView = (): JSX.Element => {
deleteAnswer,
submitAnswer,
setAnswer: setSavedAnswer,
status,
} = useUserAnswers();

const [answer, setAnswer] = useState<string>('');
const [isInit, setIsInit] = useState<boolean>(false);

const userAuthentified = useMemo(
() => typeof memberId === 'string' && memberId.length > 0,
Expand All @@ -60,8 +62,16 @@ const QuestionView = (): JSX.Element => {

// Update the answer if the stored value change
useEffect(() => {
setAnswer(userAnswer?.answer ?? '');
}, [userAnswer]);
if (
status === 'success' &&
answer.length === 0 &&
!isInit &&
typeof userAnswer !== 'undefined'
) {
setAnswer(userAnswer?.answer ?? '');
setIsInit(true);
}
}, [answer.length, isInit, status, userAnswer]);
const answerStatus = useMemo(() => userAnswer?.status, [userAnswer?.status]);

const showSubmitButton = useMemo(
Expand Down Expand Up @@ -162,7 +172,10 @@ const QuestionView = (): JSX.Element => {
<Button
disabled={!userAnswer}
variant="outlined"
onClick={() => deleteAnswer()}
onClick={() => {
setAnswer('');
deleteAnswer();
}}
startIcon={<ReplayIcon />}
data-cy={RESET_BTN_CY}
>
Expand Down
Loading