import { useEffect, useState } from "preact/hooks"; type Promotion = { id: string; annee: string | null }; export default function AdminPromotions() { const [promos, setPromos] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [newId, setNewId] = useState(""); const [newAnnee, setNewAnnee] = useState(""); const [creating, setCreating] = useState(false); async function load() { try { const res = await fetch("/students/api/promotions"); if (!res.ok) throw new Error("Impossible de charger les promotions"); setPromos(await res.json()); } catch (e) { setError(e instanceof Error ? e.message : "Erreur"); } finally { setLoading(false); } } useEffect(() => { load(); }, []); async function createPromo() { if (!newId.trim()) return; setCreating(true); try { const res = await fetch("/students/api/promotions", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ idPromo: newId.trim(), annee: newAnnee.trim() || null, }), }); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body.error ?? "Création échouée"); } setNewId(""); setNewAnnee(""); await load(); } catch (e) { setError(e instanceof Error ? e.message : "Erreur"); } finally { setCreating(false); } } async function deletePromo(id: string) { if (!confirm(`Supprimer la promotion ${id} ?`)) return; try { const res = await fetch( `/students/api/promotions/${encodeURIComponent(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 Promotions

{error &&

{error}

}
setNewId((e.target as HTMLInputElement).value)} /> setNewAnnee((e.target as HTMLInputElement).value)} style="min-width: 14rem" />
{loading ?

Chargement…

: (
{promos.length === 0 ? ( ) : promos.map((p) => ( ))}
Identifiant Année Action
Aucune promotion enregistrée
{p.id} {p.annee ?? "—"}
)}
); }