'use client'; import type { Puzzle as PuzzleType } from '@/lib/puzzles'; import cookies from 'js-cookie'; import { notFound } from 'next/navigation'; import { useForm } from 'react-hook-form'; import Button from './Button'; import Input from './Input'; import ToHTML from './ToHTML'; import { useState } from 'react'; type PuzzleData = { answer: string; // filename: string; // code_file: File[]; }; type Granted = { tries: number; score?: number; }; export default function Puzzle({ puzzle }: { puzzle: PuzzleType }) { if (!puzzle) { notFound(); } const [granted, setGranted] = useState(null); const { register, handleSubmit, formState: { errors }, setError } = 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.trim()); // 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) { setGranted(await res.json()); } } return (

{puzzle.name}

{/*

Chapitre

*/}
{granted && (

Tentatives actuelles : {granted.tries}

{granted.score &&

Score : {granted.score}

}
)} {/* */}
); }