'use client'; import cookies from 'js-cookie'; import { notFound, useRouter } from 'next/navigation'; import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { useSWRConfig } from 'swr'; import { usePuzzle } from '@/lib/hooks/use-puzzles'; import Button from './Button'; import Input from './Input'; import ToHTML from './ToHTML'; import { getURL } from '@/lib/utils'; type PuzzleData = { answer: string; // filename: string; // code_file: File[]; }; type Granted = { tries: number; score?: number; }; export default function Puzzle({ token, id }: { token: string; id: number }) { const [granted, setGranted] = useState(null); const { data: puzzle, isLoading } = usePuzzle({ token, id }); const { mutate } = useSWRConfig(); const router = useRouter(); const { register, handleSubmit } = useForm({ defaultValues: { answer: '' // filename: '', // code_file: [] } }); async function onSubmit(data: PuzzleData) { const formData = new FormData(); // if (data.code_file[0].size > 16 * 1024 * 1024) { // alert('Fichier trop volumineux'); // return; // } formData.append('answer', data.answer); // formData.append('filename', 'placeholder'); // formData.append('code_file', new Blob(), 'placeholder'); const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/puzzleResponse/${puzzle!.id}`, { method: 'POST', body: formData, headers: { Authorization: `Bearer ${cookies.get('token')}}` } }); 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 (!puzzle && isLoading) { return <>; } if (!puzzle) { notFound(); } return (

{puzzle.name}{' '} ({puzzle.scoreMax} points)

{!puzzle.score ? (
{granted && (

Tentatives actuelles : {granted.tries}

{granted.score &&

Score : {granted.score}

}
)} {/* */}
) : (

Tentative(s) : {puzzle.tries}

Score : {puzzle.score}

)}
); }