"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; export default function AdminHomework({ homework, members }: any) { const router = useRouter(); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [videoFile, setVideoFile] = useState(null); const [videoUrl, setVideoUrl] = useState(""); const [target, setTarget] = useState("all"); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!title || !description) return; setSaving(true); setError(""); try { let finalVideoUrl = videoUrl; if (videoFile) { const formData = new FormData(); formData.append("file", videoFile); formData.append("subdir", "homework"); const up = await fetch("/api/upload", { method: "POST", body: formData }); if (!up.ok) throw new Error("Upload vidéo échoué"); const { url } = await up.json(); finalVideoUrl = url; } const res = await fetch("/api/homework", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title, description, videoUrl: finalVideoUrl, userId: target === "all" ? null : target, }), }); if (!res.ok) throw new Error("Enregistrement échoué"); setTitle(""); setDescription(""); setVideoFile(null); setVideoUrl(""); setTarget("all"); router.refresh(); } catch (err: any) { setError(err.message || "Erreur"); } finally { setSaving(false); } } async function deleteHomework(id: string) { if (!confirm("Supprimer cet exercice ?")) return; await fetch("/api/homework", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id }), }); router.refresh(); } return (

🏋️ Exercices à travailler

📝 Nouvel exercice

setTitle(e.target.value)} placeholder="Ex: 10 rounds de shadow box" required />