'use client'; import { zodResolver } from '@hookform/resolvers/zod'; import cookies from 'js-cookie'; import { notFound, useRouter } from 'next/navigation'; import { useContext, useState } from 'react'; import { useForm } from 'react-hook-form'; import { useSWRConfig } from 'swr'; import * as z from 'zod'; import { usePuzzle } from '@/lib/hooks/use-puzzles'; import { getURL } from '@/lib/utils'; import { Button } from '@/components/ui/Button'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/Form'; import { Input } from '@/components/ui/Input'; import ToHTML from '@/components/ui/ToHTML'; import { UserContext } from '@/context/user'; import { useToast } from '@/lib/hooks/use-toast'; type PuzzleData = { answer: string; // filename: string; // code_file: File[]; }; type Granted = { tries: number | null; score?: number | null; message?: string | null; success?: boolean | null; }; export default function Puzzle({ token, id }: { token: string; id: number }) { const { data: me } = useContext(UserContext); 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 === 403 || 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' }); else if (res.status === 403) mutate(`puzzles/${puzzle?.id}`); } } if (!puzzle && isLoading) { return <>; } if (!puzzle) { notFound(); } // TODO : add a check to see if the user is in the group of the puzzle if (me?.groups.length === 0) { router.push('/dashboard/puzzles'); } return (

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

{!puzzle.score ? ( ) : (

Tentative{puzzle.tries && puzzle.tries > 1 ? 's' : ''} :{' '} {puzzle.tries}

Score : {puzzle.score}

)}
); } const InputFormSchema = z.object({ answer: z.string().nonempty().trim(), code_file: z.any().nullable() }); function InputForm() { const form = useForm>({ resolver: zodResolver(InputFormSchema), defaultValues: { answer: '', code_file: null } }); function onSubmit(data: z.infer) { console.log(data); form.reset(); } return (
( Réponse )} /> ( Fichier )} />
); }