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: re-refreshing not handling correctly page updates #257

Merged
merged 4 commits into from
Apr 21, 2024
Merged
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
58 changes: 34 additions & 24 deletions webapp/src/pages/Game.jsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.log("pepe") pls

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function Game() {
const [roundDuration, setRoundDuration] = useState(0);
const [maxRoundNumber, setMaxRoundNumber] = useState(9);
const [hasImage, setHasImage] = useState(false);
const [hasAnswered, setHasAnswered] = useState(false);

const { t, i18n } = useTranslation();
const [isMenuOpen, setIsMenuOpen] = useState(false);
Expand Down Expand Up @@ -68,32 +69,33 @@ export default function Game() {
setNextDisabled(!anyOptionSelected);
};

const startNewRound = useCallback(async (gameId) => {
try{
const result = await startRound(gameId);
setTimeStartRound(new Date(result.data.round_start_time).getTime());
setRoundNumber(result.data.actual_round )
setRoundDuration(result.data.round_duration);
await assignQuestion(gameId);
setLoading(false);
}
catch(error){
console.log(error)
if(error.response.status === 409){
if(roundNumber >= 9){
navigate("/dashboard/game/results", { state: { correctAnswers: correctAnswers } });
} else {
await assignQuestion(gameId)
const startNewRound = useCallback(async (gameId, startsGame) => {
if (hasAnswered || startsGame) {
try{
const result = await startRound(gameId);
setTimeStartRound(new Date(result.data.round_start_time).getTime());
setRoundNumber(result.data.actual_round )
setRoundDuration(result.data.round_duration);
await assignQuestion(gameId);
setLoading(false);
} catch(error){
console.log(error)
if(error.response.status === 409){
if(roundNumber >= 9){
navigate("/dashboard/game/results", { state: { correctAnswers: correctAnswers } });
} else {
await assignQuestion(gameId)
}
}
}
}
}, [setTimeStartRound, setRoundDuration, setRoundNumber,
}, [setTimeStartRound, setRoundDuration, setRoundNumber, hasAnswered,
assignQuestion, setLoading, navigate, correctAnswers, roundNumber])

const nextRound = useCallback(async () => {
if (roundNumber + 1 > maxRoundNumber) {
await getGameDetails(gameId);
navigate("/dashboard/game/results", { state: { correctAnswers: correctAnswers } });
let gameDetails = (await getGameDetails(gameId)).data;
navigate("/dashboard/game/results", { state: { correctAnswers: gameDetails.correctly_answered_questions } });
} else {
setAnswer({});
setHasImage(false);
Expand All @@ -113,6 +115,7 @@ export default function Game() {
}
setNextDisabled(true);
setSelectedOption(null);
setLoading(true)
await nextRound();

} catch (error) {
Expand All @@ -131,11 +134,15 @@ export default function Game() {
setTimeStartRound(new Date(newGameResponse.round_start_time).getTime());
setRoundDuration(newGameResponse.round_duration)
setMaxRoundNumber(newGameResponse.rounds);
setRoundNumber(newGameResponse.actual_round);

try{
await assignQuestion(newGameResponse.id);
setCorrectAnswers(newGameResponse.correctly_answered_questions);
setLoading(false);
setTimeElapsed(Math.round((Date.now() - new Date(newGameResponse.round_start_time).getTime()) / 1000));
}catch(error){
startNewRound(newGameResponse.id);
startNewRound(newGameResponse.id, true);
}
} else {
navigate("/dashboard");
Expand All @@ -149,7 +156,7 @@ export default function Game() {
initializeGame();
}
}, [setGameId, gameId, setTimeStartRound, setRoundDuration, setMaxRoundNumber,
setQuestion, setLoading, startNewRound, navigate, assignQuestion]);
setQuestion, setLoading, startNewRound, navigate, assignQuestion, timeStartRound]);
useEffect(() => {
let timeout;
if (showConfetti)
Expand All @@ -160,15 +167,18 @@ export default function Game() {
useEffect(() => {
let timeout;
if (timeElapsed >= roundDuration && timeStartRound !== -1) {
timeout = setTimeout(() => nextRound(), 1000);
timeout = setTimeout(() => {
setHasAnswered(true);
nextRound();
}, 1000);

} else {
timeout = setTimeout(() => {
setTimeElapsed((prevTime) => prevTime + 1);
}, 1000);
}
return () => clearTimeout(timeout);
}, [timeElapsed, timeStartRound, roundDuration, nextRound]);
}, [timeElapsed, timeStartRound, roundDuration, nextRound, setHasAnswered]);


return (
Expand All @@ -185,7 +195,7 @@ export default function Game() {
</CircularProgress>
{
(!loading && hasImage) && <Flex maxH={"40vh"}
maxW={"40vh"} justify={"center"}>
maxW={"40vw"} justify={"center"}>
<Image src={question.image} alt={t("game.image")}></Image>
</Flex>
}
Expand Down