import { useCallback, useEffect, useMemo, useState, type FormEvent } from "react"; import { Button, Input } from "@cohaise/ui"; import { ApiError, api, type Country } from "../lib/api.js"; import { DataTable, type DataTableColumn } from "../components/DataTable.js"; import { RecordDialog } from "../components/RecordDialog.js"; import { RowActionButton } from "../components/RowActionButton.js"; import { PenMaskIcon, TrashMaskIcon } from "../components/icons.js"; interface Form { code: string; name: string; phoneCode: string; } const EMPTY: Form = { code: "", name: "", phoneCode: "" }; export const Countries = () => { const [rows, setRows] = useState([]); const [search, setSearch] = useState(""); const [loading, setLoading] = useState(true); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [formError, setFormError] = useState(null); const [open, setOpen] = useState(false); const [editing, setEditing] = useState(null); const [form, setForm] = useState
(EMPTY); const load = useCallback(async () => { setLoading(true); setError(null); try { const result = await api.countries(); setRows(result.countries); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Chargement impossible."); } finally { setLoading(false); } }, []); useEffect(() => { void load(); }, [load]); // Le referentiel est court : le filtrage reste local, sans requete par frappe. const filtered = useMemo(() => { const query = search.trim().toLowerCase(); if (!query) return rows; return rows.filter((country) => [country.code, country.name, country.phone_code ?? ""] .join(" ") .toLowerCase() .includes(query), ); }, [rows, search]); const close = () => { setEditing(null); setForm(EMPTY); setFormError(null); setOpen(false); }; const openCreate = () => { setEditing(null); setForm(EMPTY); setFormError(null); setOpen(true); }; const openEdit = (country: Country) => { setEditing(country); setForm({ code: country.code, name: country.name, phoneCode: country.phone_code ?? "", }); setFormError(null); setOpen(true); }; const submit = async (event: FormEvent) => { event.preventDefault(); const code = form.code.trim().toUpperCase(); const name = form.name.trim(); if (!/^[A-Z]{2}$/.test(code)) { setFormError("Le code doit contenir exactement deux lettres."); return; } if (!name) { setFormError("Le nom est obligatoire."); return; } setBusy(true); setFormError(null); try { const payload = { code, name, phoneCode: form.phoneCode.trim() || null }; if (editing) await api.updateCountry(editing.id, payload); else await api.createCountry(payload); close(); await load(); } catch (caught) { setFormError(caught instanceof ApiError ? caught.message : "Enregistrement impossible."); } finally { setBusy(false); } }; const remove = async (country: Country) => { if (!window.confirm(`Supprimer « ${country.name} » ?`)) return; setBusy(true); setError(null); try { await api.deleteCountry(country.id); await load(); } catch (caught) { setError(caught instanceof ApiError ? caught.message : "Suppression impossible."); } finally { setBusy(false); } }; const columns: DataTableColumn[] = [ { id: "code", header: "Code", sortable: true, sortValue: (row) => row.code, cell: (row) => ( {row.code} ), }, { id: "name", header: "Nom", sortable: true, sortValue: (row) => row.name, cell: (row) => {row.name}, }, { id: "phone", header: "Indicatif", sortable: true, sortValue: (row) => row.phone_code, cell: (row) => ( {row.phone_code ?? "—"} ), }, ]; return ( <> country.id} loading={loading} error={error} emptyMessage={search ? "Aucun pays correspondant" : "Aucun pays"} emptyHint={ search ? "Essayez un autre code, un autre nom ou un indicatif." : "Ajoutez le premier pays du référentiel." } search={{ value: search, onChange: setSearch, placeholder: "Code, nom, indicatif…", }} toolbarStart={

Référentiel ISO 3166 utilisé par les entreprises et les prospects.

} actions={} rowActions={(country) => (
openEdit(country)} > void remove(country)} >
)} footer={ {filtered.length === rows.length ? `${rows.length} pays` : `${filtered.length} sur ${rows.length} pays`} } initialSort={{ column: "name", direction: "asc" }} /> void submit(event)}> setForm((current) => ({ ...current, code: event.target.value.toUpperCase() })) } /> setForm((current) => ({ ...current, name: event.target.value }))} /> setForm((current) => ({ ...current, phoneCode: event.target.value })) } /> {formError ? (

{formError}

) : null}
); };