import { useCallback, useEffect, useState, type FormEvent } from "react"; import { Button, Card, CardContent, CardHeader, CardTitle, Input, Skeleton, Spinner } from "@cohaise/ui"; import { ApiError, api, formatDate, type Country, type Organization, type OrganizationInput, } from "../lib/api.js"; const EMPTY: OrganizationInput = { name: "", website: null, industry: null, phone: null, email: null, addressLine1: null, addressLine2: null, postalCode: null, city: null, countryId: null, description: null, }; const text = (value: string): string | null => (value.trim() ? value.trim() : null); export const Organizations = () => { const [rows, setRows] = useState([]); const [countries, setCountries] = useState([]); const [total, setTotal] = useState(0); const [search, setSearch] = useState(""); const [loading, setLoading] = useState(true); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [editing, setEditing] = useState(null); const [form, setForm] = useState(EMPTY); // Un etat explicite : comparer form a EMPTY ne fonctionnerait pas, car // saisir puis effacer recrée un objet different de la constante. const [open, setOpen] = useState(false); const load = useCallback(async () => { setLoading(true); setError(null); try { const result = await api.organizations(search || undefined); setRows(result.organizations); setTotal(result.total); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Chargement impossible."); } finally { setLoading(false); } }, [search]); useEffect(() => { void load(); }, [load]); useEffect(() => { void api .countries() .then((result) => setCountries(result.countries)) .catch(() => undefined); }, []); const openCreate = () => { setEditing(null); setForm(EMPTY); setError(null); setOpen(true); }; const openEdit = (organization: Organization) => { setOpen(true); setEditing(organization); setForm({ name: organization.name, website: organization.website, industry: organization.industry, phone: organization.phone, email: organization.email, addressLine1: organization.address_line1, addressLine2: organization.address_line2, postalCode: organization.postal_code, city: organization.city, countryId: organization.country_id, description: organization.description, }); setError(null); }; const submit = async (event: FormEvent) => { event.preventDefault(); const name = text(form.name ?? ""); if (!name) { setError("Le nom de l'entreprise est obligatoire."); return; } const payload: OrganizationInput = { name, website: text(form.website ?? ""), industry: text(form.industry ?? ""), phone: text(form.phone ?? ""), email: text(form.email ?? ""), addressLine1: text(form.addressLine1 ?? ""), addressLine2: text(form.addressLine2 ?? ""), postalCode: text(form.postalCode ?? ""), city: text(form.city ?? ""), countryId: form.countryId ?? null, description: text(form.description ?? ""), }; setBusy(true); setError(null); try { if (editing) await api.updateOrganization(editing.id, payload); else await api.createOrganization(payload); setEditing(null); setForm(EMPTY); setOpen(false); await load(); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Enregistrement impossible."); } finally { setBusy(false); } }; const remove = async (organization: Organization) => { if (!window.confirm(`Supprimer « ${organization.name} » ? Cette action est réversible par un administrateur.`)) { return; } setBusy(true); setError(null); try { await api.deleteOrganization(organization.id); await load(); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Suppression impossible."); } finally { setBusy(false); } }; const set = (key: keyof OrganizationInput) => (value: string | number | null) => { setForm((current) => ({ ...current, [key]: key === "countryId" ? (value === null ? null : Number(value)) : text(String(value ?? "")), })); }; return (
setSearch(event.target.value)} />
{open ? ( {editing ? "Modifier l'entreprise" : "Nouvelle entreprise"}
void submit(event)}> set("name")(event.target.value)} /> set("industry")(event.target.value)} /> set("website")(event.target.value)} /> set("email")(event.target.value)} /> set("phone")(event.target.value)} /> set("addressLine1")(event.target.value)} /> set("addressLine2")(event.target.value)} /> set("postalCode")(event.target.value)} /> set("city")(event.target.value)} />
set("description")(event.target.value)} />
{error ? (

{error}

) : null}
) : null} {loading ? (
) : error && rows.length === 0 ? (

{error}

) : rows.length === 0 ? (

Aucune entreprise. Créez la première pour commencer.

) : (
{rows.map((organization) => ( ))}
Nom Ville Pays Secteur Contact Créée le Actions
{organization.name} {organization.city ?? "—"} {organization.country_code ?? "—"} {organization.industry ?? "—"} {organization.email ?? organization.phone ?? "—"} {formatDate(organization.created_at)}
{ }

{total} entreprise{total > 1 ? "s" : ""} {loading ? : null}

)}
); };