Adding verification on puzzle

This commit is contained in:
Théo 2023-04-22 14:29:12 +02:00
parent 5864a4c84b
commit 228c980ffb
2 changed files with 49 additions and 25 deletions

View file

@ -20,8 +20,10 @@ type PuzzleData = {
}; };
type Granted = { type Granted = {
tries: number; tries: number | null;
score?: number; score?: number | null;
message?: string | null;
success?: boolean | null;
}; };
export default function Puzzle({ token, id }: { token: string; id: number }) { 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) { if (res.ok || res.status === 406 || res.status === 423) {
const data = (await res.json()) as { tries: number; score?: number }; const data = res.ok || res.status === 406 ? ((await res.json()) as Granted) : null;
if (data.score) mutate(`puzzles/${puzzle?.id}`); if (data && data.score) mutate(`puzzles/${puzzle?.id}`);
else setGranted(data); 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 && ( {granted && (
<div className="flex flex-col"> <div className="flex flex-col">
<p className="text-sm text-muted">Tentatives actuelles : {granted.tries}</p> {granted.message && (
{granted.score && <p className="text-sm text-muted">Score : {granted.score}</p>} <p className="text-sm text-highlight-secondary">{granted.message}</p>
)}
{granted.tries && (
<p className="text-sm text-highlight-secondary">
Tentatives actuelles : {granted.tries}
</p>
)}
{granted.score && (
<p className="highlight-secondary text-sm">Score : {granted.score}</p>
)}
</div> </div>
)} )}
{/* <Input {/* <Input

View file

@ -42,16 +42,15 @@ export default function Puzzles({ token }: { token: string }) {
); );
} }
function isBetweenDates(chapter: Chapter) { function isBeforeStart(chapter: Chapter) {
if (!chapter.startDate || !chapter.endDate) { if (!chapter.startDate || !chapter.endDate) {
return false; return false;
} }
const startDate = new Date(chapter.startDate); const startDate = new Date(chapter.startDate);
const endDate = new Date(chapter.endDate);
const now = new Date(); const now = new Date();
if (startDate < now && endDate > now) { if (now > startDate) {
const minutes = 10 * 60 * 1000; const minutes = 10 * 60 * 1000;
if (startDate.getTime() + minutes < now.getTime()) { if (startDate.getTime() + minutes < now.getTime()) {
return true; return true;
@ -90,7 +89,7 @@ export default function Puzzles({ token }: { token: string }) {
<div className="flex flex-col justify-between md:flex-row md:items-center"> <div className="flex flex-col justify-between md:flex-row md:items-center">
<div className="flex items-center gap-x-2"> <div className="flex items-center gap-x-2">
<h1 className="text-xl font-semibold">{chapter.name}</h1> <h1 className="text-xl font-semibold">{chapter.name}</h1>
{!isInEventGroup(chapter) && !isBetweenDates(chapter) && ( {!isInEventGroup(chapter) && isBeforeStart(chapter) && (
<Dialog <Dialog
key={chapter.id} key={chapter.id}
title={chapter.name} title={chapter.name}
@ -117,14 +116,16 @@ export default function Puzzles({ token }: { token: string }) {
day: 'numeric', day: 'numeric',
month: 'long', month: 'long',
year: 'numeric', year: 'numeric',
hour: 'numeric' hour: 'numeric',
minute: 'numeric'
})}{' '} })}{' '}
-{' '} -{' '}
{new Date(chapter.endDate).toLocaleDateString('fr-FR', { {new Date(chapter.endDate).toLocaleDateString('fr-FR', {
day: 'numeric', day: 'numeric',
month: 'long', month: 'long',
year: 'numeric', year: 'numeric',
hour: 'numeric' hour: 'numeric',
minute: 'numeric'
})} })}
</span> </span>
</div> </div>
@ -211,15 +212,23 @@ export default function Puzzles({ token }: { token: string }) {
} }
function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) { function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) {
const isPuzzleAvailable = (chapter: Chapter) => { function isBeforeStart(chapter: Chapter) {
const today = new Date(); if (!chapter.startDate || !chapter.endDate) {
const startDate = new Date(chapter.startDate!); return false;
const endDate = new Date(chapter.endDate!); }
return (
(chapter.startDate && chapter.endDate && today >= startDate && today <= endDate) || const startDate = new Date(chapter.startDate);
(!chapter.startDate && !chapter.endDate) const now = new Date();
);
}; if (now > startDate) {
const minutes = 10 * 60 * 1000;
if (startDate.getTime() + minutes < now.getTime()) {
return true;
}
}
return false;
}
return ( return (
<li <li
className={cn( className={cn(
@ -229,11 +238,11 @@ function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) {
'border-yellow-600/30': puzzle.tags?.find((tag) => tag.name.toLowerCase() === 'medium'), 'border-yellow-600/30': puzzle.tags?.find((tag) => tag.name.toLowerCase() === 'medium'),
'border-red-600/30': puzzle.tags?.find((tag) => tag.name.toLowerCase() === 'hard'), 'border-red-600/30': puzzle.tags?.find((tag) => tag.name.toLowerCase() === 'hard'),
'border-highlight-primary': !puzzle.tags?.length, 'border-highlight-primary': !puzzle.tags?.length,
'cursor-not-allowed': !isPuzzleAvailable(chapter) 'cursor-not-allowed': !isBeforeStart(chapter)
} }
)} )}
> >
{isPuzzleAvailable(chapter) ? ( {isBeforeStart(chapter) ? (
<AppLink <AppLink
className="flex h-full w-full items-center justify-between p-4" className="flex h-full w-full items-center justify-between p-4"
key={puzzle.id} key={puzzle.id}