266 lines
9.4 KiB
TypeScript
266 lines
9.4 KiB
TypeScript
import { Link } from '@inertiajs/react';
|
|
import { ArrowUpRight, ClipboardList } from 'lucide-react';
|
|
import { useId, type ReactNode } from 'react';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import { openJourneyHelp } from '@/components/journey-help';
|
|
import { recoveryHints } from '@/lib/journey-help';
|
|
import { date, money, statusLabels } from '@/types/procurement';
|
|
import type { Requisition } from '@/types/procurement';
|
|
|
|
export const controlClass =
|
|
'h-10 w-full min-w-0 rounded-lg border border-input bg-background px-3 text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20';
|
|
|
|
/** Shared layout only: callers retain their actions, permissions and submit behaviour. */
|
|
export function ActionBar({
|
|
children,
|
|
align = 'start',
|
|
label = 'Acções disponíveis',
|
|
className = '',
|
|
}: {
|
|
children: ReactNode;
|
|
align?: 'start' | 'end';
|
|
label?: string;
|
|
className?: string;
|
|
}) {
|
|
return (
|
|
<div
|
|
role="group"
|
|
aria-label={label}
|
|
className={`sgc-action-bar flex flex-col gap-3 sm:flex-row sm:flex-wrap sm:items-center ${align === 'end' ? 'sm:justify-end' : ''} print:hidden ${className}`}
|
|
>
|
|
{children}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Preserve table semantics and make wide results reachable with a keyboard. */
|
|
export function TableViewport({
|
|
children,
|
|
label = 'Tabela de resultados',
|
|
}: {
|
|
children: ReactNode;
|
|
label?: string;
|
|
}) {
|
|
return (
|
|
<div
|
|
role="region"
|
|
aria-label={label}
|
|
tabIndex={0}
|
|
className="sgc-table-viewport max-w-full min-w-0 overflow-x-auto rounded-lg focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring"
|
|
>
|
|
{children}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Print all already authorised details without changing their on-screen expansion state. */
|
|
export function PrintableDetails({
|
|
summary,
|
|
children,
|
|
}: {
|
|
summary: ReactNode;
|
|
children: ReactNode;
|
|
}) {
|
|
return (
|
|
<>
|
|
<details className="print:hidden">
|
|
<summary className="cursor-pointer underline">
|
|
{summary}
|
|
</summary>
|
|
{children}
|
|
</details>
|
|
<div className="hidden print:block">
|
|
<p className="font-medium">{summary}</p>
|
|
{children}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
export function PageTitle({
|
|
eyebrow,
|
|
title,
|
|
description,
|
|
action,
|
|
}: {
|
|
eyebrow: string;
|
|
title: string;
|
|
description: string;
|
|
action?: ReactNode;
|
|
}) {
|
|
return (
|
|
<header className="sgc-page-title flex min-w-0 flex-col gap-4 sm:flex-row sm:flex-wrap sm:items-end sm:justify-between">
|
|
<div className="min-w-0">
|
|
<p className="mb-2 text-xs font-semibold tracking-widest text-teal-700 uppercase dark:text-teal-400">
|
|
{eyebrow}
|
|
</p>
|
|
<h1 className="text-2xl font-semibold tracking-tight [overflow-wrap:anywhere] sm:text-3xl">
|
|
{title}
|
|
</h1>
|
|
<p className="mt-2 max-w-2xl text-sm text-muted-foreground">
|
|
{description}
|
|
</p>
|
|
</div>
|
|
{action && (
|
|
<div className="sgc-page-actions min-w-0 print:hidden">
|
|
{action}
|
|
</div>
|
|
)}
|
|
</header>
|
|
);
|
|
}
|
|
export function Panel({
|
|
id,
|
|
title,
|
|
description,
|
|
children,
|
|
}: {
|
|
id?: string;
|
|
title: string;
|
|
description?: string;
|
|
children: ReactNode;
|
|
}) {
|
|
const headingId = useId();
|
|
return (
|
|
<section
|
|
id={id}
|
|
aria-labelledby={headingId}
|
|
className="sgc-panel min-w-0 scroll-mt-24 rounded-xl border bg-card p-4 shadow-xs sm:p-5 print:rounded-none print:shadow-none"
|
|
>
|
|
<h2
|
|
id={headingId}
|
|
className="font-semibold [overflow-wrap:anywhere]"
|
|
>
|
|
{title}
|
|
</h2>
|
|
{description && (
|
|
<p className="mt-1 text-sm text-muted-foreground">
|
|
{description}
|
|
</p>
|
|
)}
|
|
<div className="mt-5">{children}</div>
|
|
</section>
|
|
);
|
|
}
|
|
export function Status({ value }: { value: string }) {
|
|
return (
|
|
<Badge
|
|
variant="outline"
|
|
className={
|
|
value === 'approved'
|
|
? 'border-teal-200 bg-teal-50 text-teal-800 dark:bg-teal-950 dark:text-teal-200'
|
|
: value === 'pending'
|
|
? 'border-amber-200 bg-amber-50 text-amber-800 dark:bg-amber-950 dark:text-amber-200'
|
|
: value === 'rejected'
|
|
? 'border-rose-200 bg-rose-50 text-rose-800 dark:bg-rose-950 dark:text-rose-200'
|
|
: ''
|
|
}
|
|
>
|
|
{statusLabels[value] ?? value}
|
|
</Badge>
|
|
);
|
|
}
|
|
export function RequisitionTable({ rows }: { rows: Requisition[] }) {
|
|
if (!rows.length)
|
|
return (
|
|
<div className="py-12 text-center">
|
|
<ClipboardList className="mx-auto mb-3 size-9 text-muted-foreground/50" />
|
|
<p className="font-medium">Sem requisições para apresentar</p>
|
|
<p className="mt-1 text-sm text-muted-foreground">
|
|
Os pedidos de compra aparecem aqui depois de criados.
|
|
</p>
|
|
</div>
|
|
);
|
|
return (
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left text-sm">
|
|
<thead className="border-b text-xs text-muted-foreground">
|
|
<tr>
|
|
<th className="pb-3 font-medium">Requisição</th>
|
|
<th className="pb-3 font-medium">Departamento</th>
|
|
<th className="pb-3 font-medium">Data</th>
|
|
<th className="pb-3 font-medium">Estado</th>
|
|
<th className="pb-3 text-right font-medium">
|
|
Total com IVA
|
|
</th>
|
|
<th>
|
|
<span className="sr-only">Abrir</span>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{rows.map((r) => (
|
|
<tr key={r.id} className="border-b last:border-0">
|
|
<td className="py-4 pr-4">
|
|
<Link
|
|
className="font-semibold hover:text-teal-700"
|
|
href={`/requisicoes/${r.id}`}
|
|
>
|
|
{r.number}
|
|
</Link>
|
|
<div className="mt-1 text-xs text-muted-foreground">
|
|
{r.requester?.name}
|
|
</div>
|
|
</td>
|
|
<td className="pr-4">
|
|
{r.department?.name ?? '—'}
|
|
</td>
|
|
<td className="pr-4 whitespace-nowrap">
|
|
{date(r.created_at)}
|
|
</td>
|
|
<td className="pr-4">
|
|
<Status value={r.status} />
|
|
</td>
|
|
<td className="text-right font-medium whitespace-nowrap">
|
|
{money(r.total_minor)}
|
|
</td>
|
|
<td className="pl-3">
|
|
<Link
|
|
href={`/requisicoes/${r.id}`}
|
|
aria-label={`Abrir ${r.number}`}
|
|
>
|
|
<ArrowUpRight className="size-4" />
|
|
</Link>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
);
|
|
}
|
|
export function FormErrors({ errors }: { errors: Record<string, string> }) {
|
|
if (!Object.keys(errors).length) return null;
|
|
const hints = recoveryHints(Object.keys(errors));
|
|
return (
|
|
<div
|
|
role="alert"
|
|
className="rounded-lg border border-destructive/30 bg-destructive/5 p-4 text-sm text-destructive"
|
|
>
|
|
<p className="mb-1 font-medium">Verifique os dados:</p>
|
|
<ul className="list-inside list-disc">
|
|
{Object.entries(errors).map(([key, value]) => (
|
|
<li key={key}>{value}</li>
|
|
))}
|
|
</ul>
|
|
{hints.length > 0 && (
|
|
<div
|
|
className="mt-3 space-y-2 text-foreground"
|
|
role="note"
|
|
aria-label="Orientação para corrigir"
|
|
>
|
|
{hints.map((hint) => (
|
|
<p key={hint}>{hint}</p>
|
|
))}
|
|
</div>
|
|
)}
|
|
<button
|
|
type="button"
|
|
className="mt-3 underline focus-visible:ring-2 focus-visible:ring-ring"
|
|
onClick={openJourneyHelp}
|
|
>
|
|
Consultar ajuda para corrigir e continuar
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|