266 lines
12 KiB
TypeScript
266 lines
12 KiB
TypeScript
import { Form, Head, Link, router } from '@inertiajs/react';
|
|
import { useState } from 'react';
|
|
import { PageTitle, Panel, controlClass } from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
type Client = {
|
|
id: number;
|
|
client_key: string;
|
|
code: string;
|
|
name: string;
|
|
tax_number: string | null;
|
|
address: string | null;
|
|
email: string | null;
|
|
active: boolean;
|
|
version: number;
|
|
};
|
|
function Fields({ client, prefix }: { client?: Client; prefix: string }) {
|
|
return (
|
|
<div className="grid gap-4 sm:grid-cols-2">
|
|
{(
|
|
[
|
|
['code', 'Código', 40],
|
|
['name', 'Nome da entidade cliente', 180],
|
|
['tax_number', 'Identificação fiscal', 40],
|
|
['email', 'Email institucional', 254],
|
|
] as const
|
|
).map(([field, label, max]) => (
|
|
<label
|
|
key={field}
|
|
className="grid gap-1 text-sm"
|
|
htmlFor={`${prefix}-${field}`}
|
|
>
|
|
{label}
|
|
<input
|
|
id={`${prefix}-${field}`}
|
|
className={controlClass}
|
|
name={field}
|
|
type={field === 'email' ? 'email' : 'text'}
|
|
defaultValue={client?.[field] ?? ''}
|
|
required={field === 'code' || field === 'name'}
|
|
maxLength={max}
|
|
/>
|
|
</label>
|
|
))}
|
|
<label
|
|
className="grid gap-1 text-sm sm:col-span-2"
|
|
htmlFor={`${prefix}-address`}
|
|
>
|
|
Morada
|
|
<textarea
|
|
id={`${prefix}-address`}
|
|
className={controlClass}
|
|
name="address"
|
|
defaultValue={client?.address ?? ''}
|
|
maxLength={2000}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1 text-sm" htmlFor={`${prefix}-active`}>
|
|
Situação
|
|
<select
|
|
id={`${prefix}-active`}
|
|
className={controlClass}
|
|
name="active"
|
|
defaultValue={client?.active === false ? '0' : '1'}
|
|
>
|
|
<option value="1">Activo</option>
|
|
<option value="0">Inactivo</option>
|
|
</select>
|
|
</label>
|
|
<label
|
|
className="grid gap-1 text-sm sm:col-span-2"
|
|
htmlFor={`${prefix}-reason`}
|
|
>
|
|
Fundamento
|
|
<textarea
|
|
id={`${prefix}-reason`}
|
|
className={controlClass}
|
|
name="reason"
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
/>
|
|
</label>
|
|
</div>
|
|
);
|
|
}
|
|
export default function Clients({
|
|
clients,
|
|
canManage,
|
|
filters,
|
|
}: {
|
|
clients: {
|
|
data: Client[];
|
|
current_page: number;
|
|
last_page: number;
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
canManage: boolean;
|
|
filters: { search?: string };
|
|
}) {
|
|
const [createKey, setCreateKey] = useState(() => crypto.randomUUID());
|
|
return (
|
|
<>
|
|
<Head title="Clientes" />
|
|
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6 p-5 lg:p-8">
|
|
<PageTitle
|
|
eyebrow="Cadastro da empresa"
|
|
title="Clientes"
|
|
description="Entidades clientes dos serviços de contratação e das operações comerciais. Cada processo conserva a identificação aceite na sua preparação."
|
|
/>
|
|
<Panel title="Entidades registadas">
|
|
<form
|
|
className="mb-5 flex flex-wrap gap-3"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const data = new FormData(event.currentTarget);
|
|
const search = data.get('search');
|
|
router.get('/empresa/clientes', {
|
|
search: typeof search === 'string' ? search : '',
|
|
});
|
|
}}
|
|
>
|
|
<input
|
|
name="search"
|
|
aria-label="Procurar cliente"
|
|
className={controlClass}
|
|
defaultValue={filters.search ?? ''}
|
|
placeholder="Código ou nome"
|
|
maxLength={180}
|
|
/>
|
|
<Button type="submit">Procurar</Button>
|
|
</form>
|
|
<div className="space-y-4">
|
|
{clients.data.map((client) => (
|
|
<details
|
|
key={`${client.id}-${client.version}`}
|
|
className="rounded-lg border p-4"
|
|
>
|
|
<summary className="cursor-pointer text-sm font-medium">
|
|
{client.code} · {client.name} ·{' '}
|
|
{client.active ? 'Activo' : 'Inactivo'}
|
|
</summary>
|
|
{canManage ? (
|
|
<Form
|
|
action={`/empresa/clientes/${client.id}`}
|
|
method="patch"
|
|
className="mt-5 space-y-4"
|
|
>
|
|
{({ errors, processing }) => (
|
|
<>
|
|
<input
|
|
type="hidden"
|
|
name="version"
|
|
value={client.version}
|
|
/>
|
|
{Object.values(errors).map(
|
|
(error, index) => (
|
|
<p
|
|
key={index}
|
|
role="alert"
|
|
className="text-sm text-red-700"
|
|
>
|
|
{String(error)}
|
|
</p>
|
|
),
|
|
)}
|
|
<Fields
|
|
client={client}
|
|
prefix={`client-${client.id}`}
|
|
/>
|
|
<Button
|
|
type="submit"
|
|
disabled={processing}
|
|
>
|
|
Guardar alterações
|
|
</Button>
|
|
</>
|
|
)}
|
|
</Form>
|
|
) : (
|
|
<dl className="mt-4 grid gap-2 text-sm">
|
|
{client.tax_number && (
|
|
<div>
|
|
<dt>Identificação fiscal</dt>
|
|
<dd>{client.tax_number}</dd>
|
|
</div>
|
|
)}
|
|
{client.address && (
|
|
<div>
|
|
<dt>Morada</dt>
|
|
<dd>{client.address}</dd>
|
|
</div>
|
|
)}
|
|
{client.email && (
|
|
<div>
|
|
<dt>Email institucional</dt>
|
|
<dd>{client.email}</dd>
|
|
</div>
|
|
)}
|
|
</dl>
|
|
)}
|
|
</details>
|
|
))}
|
|
{clients.data.length === 0 && (
|
|
<p className="text-sm text-muted-foreground">
|
|
Sem clientes neste resultado.
|
|
</p>
|
|
)}
|
|
</div>
|
|
<nav
|
|
aria-label="Páginas de clientes"
|
|
className="mt-5 flex flex-wrap items-center gap-4 text-sm"
|
|
>
|
|
{clients.prev_page_url && (
|
|
<Link href={clients.prev_page_url}>Anterior</Link>
|
|
)}
|
|
<span>
|
|
Página {clients.current_page} de {clients.last_page}
|
|
</span>
|
|
{clients.next_page_url && (
|
|
<Link href={clients.next_page_url}>Seguinte</Link>
|
|
)}
|
|
</nav>
|
|
</Panel>
|
|
{canManage && (
|
|
<Panel title="Registar cliente">
|
|
<Form
|
|
key={createKey}
|
|
action="/empresa/clientes"
|
|
method="post"
|
|
className="space-y-4"
|
|
onSuccess={() => setCreateKey(crypto.randomUUID())}
|
|
>
|
|
{({ errors, processing }) => (
|
|
<>
|
|
<input
|
|
type="hidden"
|
|
name="client_key"
|
|
value={createKey}
|
|
/>
|
|
{Object.values(errors).map(
|
|
(error, index) => (
|
|
<p
|
|
key={index}
|
|
role="alert"
|
|
className="text-sm text-red-700"
|
|
>
|
|
{String(error)}
|
|
</p>
|
|
),
|
|
)}
|
|
<Fields prefix="new-client" />
|
|
<Button type="submit" disabled={processing}>
|
|
Registar cliente
|
|
</Button>
|
|
</>
|
|
)}
|
|
</Form>
|
|
</Panel>
|
|
)}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|