From 228c980ffb4e5d06864c59a30fccd5ad1a9c01e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o?= <43091603+glazk0@users.noreply.github.com> Date: Sat, 22 Apr 2023 14:29:12 +0200 Subject: [PATCH] Adding verification on puzzle --- ui/Puzzle.tsx | 31 +++++++++++++++++++++++-------- ui/Puzzles.tsx | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/ui/Puzzle.tsx b/ui/Puzzle.tsx index 40ca5b0..777a012 100644 --- a/ui/Puzzle.tsx +++ b/ui/Puzzle.tsx @@ -20,8 +20,10 @@ type PuzzleData = { }; type Granted = { - tries: number; - score?: number; + tries: number | null; + score?: number | null; + message?: string | null; + success?: boolean | null; }; export default function Puzzle({ token, id }: { token: string; id: number }) { @@ -58,10 +60,14 @@ export default function Puzzle({ token, id }: { token: string; id: number }) { } }); - if (res.ok || res.status === 406) { - const data = (await res.json()) as { tries: number; score?: number }; - if (data.score) mutate(`puzzles/${puzzle?.id}`); - else setGranted(data); + if (res.ok || res.status === 406 || res.status === 423) { + const data = res.ok || res.status === 406 ? ((await res.json()) as Granted) : null; + if (data && data.score) mutate(`puzzles/${puzzle?.id}`); + else if (data && data.tries) setGranted(data); + else if (res.ok && data?.success) + setGranted({ tries: null, score: null, message: 'Réponse correcte' }); + else if (res.status === 423) + setGranted({ tries: null, score: null, message: 'Réponse incorrecte' }); } } @@ -99,8 +105,17 @@ export default function Puzzle({ token, id }: { token: string; id: number }) { /> {granted && (
Tentatives actuelles : {granted.tries}
- {granted.score &&Score : {granted.score}
} + {granted.message && ( +{granted.message}
+ )} + {granted.tries && ( ++ Tentatives actuelles : {granted.tries} +
+ )} + {granted.score && ( +Score : {granted.score}
+ )}