Files
CoHaiSe/frontend/crm/src/pages/Organizations.tsx
T
Esiah 858d862fed
deploy / deploy (push) Canceled after 0s
feat: init repo
2026-11-10 19:56:03 +01:00

345 lines
12 KiB
TypeScript

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<Organization[]>([]);
const [countries, setCountries] = useState<Country[]>([]);
const [total, setTotal] = useState(0);
const [search, setSearch] = useState("");
const [loading, setLoading] = useState(true);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [editing, setEditing] = useState<Organization | null>(null);
const [form, setForm] = useState<OrganizationInput>(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<HTMLFormElement>) => {
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 (
<div className="flex min-h-0 flex-1 flex-col gap-6">
<div className="flex flex-wrap items-end justify-between gap-4">
<div className="w-full max-w-xs">
<Input
label="Rechercher"
type="search"
placeholder="Nom, ville, secteur…"
value={search}
onChange={(event) => setSearch(event.target.value)}
/>
</div>
<Button onClick={openCreate}>Nouvelle entreprise</Button>
</div>
{open ? (
<Card>
<CardHeader>
<CardTitle>{editing ? "Modifier l'entreprise" : "Nouvelle entreprise"}</CardTitle>
</CardHeader>
<CardContent>
<form className="grid gap-4 sm:grid-cols-2" onSubmit={(event) => void submit(event)}>
<Input
label="Nom *"
required
value={form.name ?? ""}
onChange={(event) => set("name")(event.target.value)}
/>
<Input
label="Secteur"
value={form.industry ?? ""}
onChange={(event) => set("industry")(event.target.value)}
/>
<Input
label="Site web"
type="url"
value={form.website ?? ""}
onChange={(event) => set("website")(event.target.value)}
/>
<Input
label="E-mail"
type="email"
value={form.email ?? ""}
onChange={(event) => set("email")(event.target.value)}
/>
<Input
label="Téléphone"
value={form.phone ?? ""}
onChange={(event) => set("phone")(event.target.value)}
/>
<label className="flex flex-col gap-2 text-sm font-medium text-foreground">
Pays
<select
className="h-10 rounded-md border border-border-control bg-background px-3 text-sm"
value={form.countryId ?? ""}
onChange={(event) =>
set("countryId")(event.target.value ? Number(event.target.value) : null)
}
>
<option value="">— Aucun —</option>
{countries.map((country) => (
<option key={country.id} value={country.id}>
{country.name} ({country.code})
</option>
))}
</select>
</label>
<Input
label="Adresse"
value={form.addressLine1 ?? ""}
onChange={(event) => set("addressLine1")(event.target.value)}
/>
<Input
label="Complément"
value={form.addressLine2 ?? ""}
onChange={(event) => set("addressLine2")(event.target.value)}
/>
<Input
label="Code postal"
value={form.postalCode ?? ""}
onChange={(event) => set("postalCode")(event.target.value)}
/>
<Input
label="Ville"
value={form.city ?? ""}
onChange={(event) => set("city")(event.target.value)}
/>
<div className="sm:col-span-2">
<Input
label="Notes"
value={form.description ?? ""}
onChange={(event) => set("description")(event.target.value)}
/>
</div>
{error ? (
<p className="text-sm text-destructive sm:col-span-2" role="alert">
{error}
</p>
) : null}
<div className="flex gap-3 sm:col-span-2">
<Button type="submit" isLoading={busy}>
{editing ? "Enregistrer" : "Créer"}
</Button>
<Button
type="button"
variant="secondary"
onClick={() => {
setEditing(null);
setForm(EMPTY);
setError(null);
setOpen(false);
}}
>
Annuler
</Button>
</div>
</form>
</CardContent>
</Card>
) : null}
{loading ? (
<div className="flex flex-col gap-3">
<Skeleton className="h-10" />
<Skeleton className="h-10" />
<Skeleton className="h-10" />
</div>
) : error && rows.length === 0 ? (
<p className="text-sm text-destructive">{error}</p>
) : rows.length === 0 ? (
<Card>
<CardContent>
<p className="text-sm text-muted-foreground">
Aucune entreprise. Créez la première pour commencer.
</p>
</CardContent>
</Card>
) : (
<div className="min-h-0 flex-1 overflow-y-auto overflow-x-auto">
<table className="w-full border-collapse text-sm">
<thead>
<tr className="border-b border-border-control text-left text-xs uppercase tracking-label text-subtle-foreground">
<th className="py-2 pr-4">Nom</th>
<th className="py-2 pr-4">Ville</th>
<th className="py-2 pr-4">Pays</th>
<th className="py-2 pr-4">Secteur</th>
<th className="py-2 pr-4">Contact</th>
<th className="py-2 pr-4">Créée le</th>
<th className="py-2 pr-4 text-right">Actions</th>
</tr>
</thead>
<tbody>
{rows.map((organization) => (
<tr key={organization.id} className="border-b border-border-control/60">
<td className="py-3 pr-4 font-medium">{organization.name}</td>
<td className="py-3 pr-4">{organization.city ?? "—"}</td>
<td className="py-3 pr-4">{organization.country_code ?? "—"}</td>
<td className="py-3 pr-4">{organization.industry ?? "—"}</td>
<td className="py-3 pr-4">
{organization.email ?? organization.phone ?? "—"}
</td>
<td className="py-3 pr-4 text-subtle-foreground">
{formatDate(organization.created_at)}
</td>
<td className="py-3 pr-4">
<div className="flex justify-end gap-2">
<Button size="sm" variant="secondary" onClick={() => openEdit(organization)}>
Modifier
</Button>
{
<Button size="sm" variant="destructive" onClick={() => void remove(organization)}>
Supprimer
</Button>
}
</div>
</td>
</tr>
))}
</tbody>
</table>
<p className="mt-3 text-xs text-subtle-foreground">
{total} entreprise{total > 1 ? "s" : ""}
{loading ? <Spinner size="xs" /> : null}
</p>
</div>
)}
</div>
);
};