import { useEffect, useState } from "preact/hooks"; type UE = { id: number; nom: string }; export default function AdminUEs() { const [ues, setUes] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [newNom, setNewNom] = useState(""); const [creating, setCreating] = useState(false); async function load() { try { const res = await fetch("/notes/api/ues"); if (!res.ok) throw new Error("Impossible de charger les UEs"); setUes(await res.json()); } catch (e) { setError(e instanceof Error ? e.message : "Erreur"); } finally { setLoading(false); } } useEffect(() => { load(); }, []); async function createUE() { if (!newNom.trim()) return; setCreating(true); try { const res = await fetch("/notes/api/ues", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ nom: newNom.trim() }), }); if (!res.ok) throw new Error("Création échouée"); setNewNom(""); await load(); } catch (e) { setError(e instanceof Error ? e.message : "Erreur"); } finally { setCreating(false); } } async function deleteUE(id: number) { if (!confirm("Supprimer cette UE ?")) return; try { const res = await fetch(`/notes/api/ues/${id}`, { method: "DELETE" }); if (!res.ok) throw new Error("Suppression échouée"); await load(); } catch (e) { setError(e instanceof Error ? e.message : "Erreur"); } } return (

Gestion des UEs

{error &&

{error}

}
setNewNom((e.target as HTMLInputElement).value)} onKeyDown={(e) => e.key === "Enter" && createUE()} />
{loading ?

Chargement…

: (
{ues.length === 0 ? ( ) : ues.map((ue) => ( ))}
ID Nom Action
Aucune UE enregistrée
{ue.id} {ue.nom}
)}
); }