@@ -0,0 +1,140 @@
|
||||
import { Link, useForm } from '@inertiajs/react';
|
||||
import { Management, Pagination, button, control, states } from './shared';
|
||||
import type { Paginated } from './shared';
|
||||
|
||||
type Record = {
|
||||
id: number;
|
||||
name?: string;
|
||||
title?: string;
|
||||
reference?: string;
|
||||
slug: string;
|
||||
is_active?: boolean;
|
||||
status?: string;
|
||||
};
|
||||
export default function Catalog({
|
||||
resource,
|
||||
records,
|
||||
filters,
|
||||
sectors,
|
||||
}: {
|
||||
resource: 'sectors' | 'opportunities';
|
||||
records: Paginated<Record>;
|
||||
filters: { q?: string; status?: string; sector_id?: number | string };
|
||||
sectors: { id: number; name: string }[];
|
||||
}) {
|
||||
const form = useForm({
|
||||
q: filters.q ?? '',
|
||||
status: filters.status ?? '',
|
||||
sector_id: String(filters.sector_id ?? ''),
|
||||
});
|
||||
return (
|
||||
<Management
|
||||
title={resource === 'sectors' ? 'Sectores' : 'Oportunidades'}
|
||||
>
|
||||
<Link className={button} href={`/management/${resource}/create`}>
|
||||
Criar {resource === 'sectors' ? 'sector' : 'oportunidade'}
|
||||
</Link>
|
||||
<form
|
||||
className="flex flex-wrap gap-3"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.get(`/management/${resource}`, {
|
||||
preserveState: false,
|
||||
});
|
||||
}}
|
||||
>
|
||||
<input
|
||||
aria-label="Pesquisar catálogo"
|
||||
placeholder="Nome, título, referência ou slug"
|
||||
className={`${control} max-w-sm`}
|
||||
value={form.data.q}
|
||||
onChange={(event) => form.setData('q', event.target.value)}
|
||||
maxLength={150}
|
||||
/>
|
||||
<select
|
||||
aria-label="Estado"
|
||||
className={`${control} max-w-xs`}
|
||||
value={form.data.status}
|
||||
onChange={(event) =>
|
||||
form.setData('status', event.target.value)
|
||||
}
|
||||
>
|
||||
<option value="">Todos os estados</option>
|
||||
{(resource === 'sectors'
|
||||
? ['active', 'inactive']
|
||||
: ['draft', 'published', 'archived']
|
||||
).map((status) => (
|
||||
<option key={status} value={status}>
|
||||
{status === 'active'
|
||||
? 'Activo'
|
||||
: status === 'inactive'
|
||||
? 'Inactivo'
|
||||
: states[status]}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{resource === 'opportunities' && (
|
||||
<select
|
||||
aria-label="Sector"
|
||||
className={`${control} max-w-xs`}
|
||||
value={form.data.sector_id}
|
||||
onChange={(event) =>
|
||||
form.setData('sector_id', event.target.value)
|
||||
}
|
||||
>
|
||||
<option value="">Todos os sectores</option>
|
||||
{sectors.map((sector) => (
|
||||
<option key={sector.id} value={sector.id}>
|
||||
{sector.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
<button className={button} disabled={form.processing}>
|
||||
Pesquisar
|
||||
</button>
|
||||
<Link
|
||||
className="p-2 underline"
|
||||
href={`/management/${resource}`}
|
||||
>
|
||||
Limpar filtros
|
||||
</Link>
|
||||
{Object.values(form.errors).map((error) => (
|
||||
<p key={error} role="alert" className="w-full text-red-600">
|
||||
{error}
|
||||
</p>
|
||||
))}
|
||||
</form>
|
||||
<div className="space-y-3">
|
||||
{records.data.map((record) => (
|
||||
<article
|
||||
key={record.id}
|
||||
className="flex flex-wrap items-center justify-between gap-3 rounded border p-4"
|
||||
>
|
||||
<div>
|
||||
<h2 className="font-semibold">
|
||||
{record.name ?? record.title}
|
||||
</h2>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{record.reference ?? record.slug} ·{' '}
|
||||
{resource === 'sectors'
|
||||
? record.is_active
|
||||
? 'Activo'
|
||||
: 'Inactivo'
|
||||
: states[record.status ?? 'draft']}
|
||||
</p>
|
||||
</div>
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/management/${resource}/${record.id}/edit`}
|
||||
>
|
||||
Editar
|
||||
</Link>
|
||||
</article>
|
||||
))}
|
||||
</div>
|
||||
{!records.data.length && <p>Nenhum registo encontrado.</p>}
|
||||
<Pagination page={records} />
|
||||
</Management>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user