Skip to content

Commit

Permalink
fix: show previous data to user
Browse files Browse the repository at this point in the history
close #73
  • Loading branch information
juancarlosfarah committed Mar 12, 2024
1 parent d27ca5c commit 8b77e31
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/modules/main/PlayerView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent, useMemo, useState } from 'react';
import { ChangeEvent, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { Box, Grid, TextField, Typography } from '@mui/material';
Expand Down Expand Up @@ -30,20 +30,23 @@ const PlayerView = (): JSX.Element => {
const { data: appData } = hooks.useAppData();
const { mutate: postAppData } = mutations.usePostAppData();

// use effect to get required app data
let savedAnswer = '';
const [answer, setAnswer] = useState<string>();
const [savedAnswer, setSavedAnswer] = useState<string>('');

if (appData) {
// only show the last answer
const savedAnswerObject = sortBy(appData, ['createdAt'])
.reverse()
.find(isAnswer) as AppData<UserAnswer>;
if (savedAnswerObject) {
savedAnswer = savedAnswerObject.data.answer ?? '';
// use effect to get required app data
useEffect(() => {
if (appData) {
// only show the last answer
const savedAnswerObject = sortBy(appData, ['createdAt'])
.reverse()
.find(isAnswer) as AppData<UserAnswer>;
if (savedAnswerObject) {
const savedAnswerText = savedAnswerObject.data.answer ?? '';
setAnswer(savedAnswerText);
setSavedAnswer(savedAnswerText);
}
}
}

const [answer, setAnswer] = useState<string>(savedAnswer);
}, [appData]);

const disableSave = useMemo(() => {
// disable if there is no user (logged out or anonymous)
Expand Down

0 comments on commit 8b77e31

Please sign in to comment.