324 lines
12 KiB
TypeScript
324 lines
12 KiB
TypeScript
import { Link, router, useForm, usePage, useRemember } from '@inertiajs/react';
|
|
import { useEffect, useRef } from 'react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { FormErrors, Panel, controlClass } from '@/components/procurement';
|
|
|
|
type SavedSearch = {
|
|
id: number;
|
|
name: string;
|
|
term: string;
|
|
version: number;
|
|
href: string;
|
|
};
|
|
export type SavedSearches = {
|
|
max_active: number;
|
|
active: SavedSearch[];
|
|
archived: {
|
|
data: SavedSearch[];
|
|
total: number;
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
};
|
|
|
|
function usePrivatePreparation(dirty: boolean) {
|
|
const ref = useRef<HTMLFormElement>(null);
|
|
useEffect(() => {
|
|
const form = ref.current;
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', { detail: { form, dirty } }),
|
|
);
|
|
return () => {
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', {
|
|
detail: { form, dirty: false },
|
|
}),
|
|
);
|
|
};
|
|
}, [dirty]);
|
|
return ref;
|
|
}
|
|
|
|
function SavedSearchRow({
|
|
row,
|
|
term,
|
|
archived = false,
|
|
}: {
|
|
row: SavedSearch;
|
|
term: string;
|
|
archived?: boolean;
|
|
}) {
|
|
const actor = usePage().props.auth?.user;
|
|
const historyKey = `saved-search-edit:${actor?.tenant_id ?? 'none'}:${actor?.id ?? 'none'}:${row.id}:${row.version}`;
|
|
const [editing, setEditing] = useRemember(false, `${historyKey}:editing`);
|
|
const edit = useForm(historyKey, {
|
|
name: row.name,
|
|
term: row.term,
|
|
version: row.version,
|
|
q: term,
|
|
});
|
|
const action = useForm({ version: row.version, q: term });
|
|
const ref = usePrivatePreparation(editing && edit.isDirty);
|
|
const endpoint = `/compras/pesquisas-guardadas/${row.id}`;
|
|
return (
|
|
<li className="grid min-w-0 grid-cols-1 gap-3 border-b py-4 last:border-0">
|
|
{editing ? (
|
|
<form
|
|
ref={ref}
|
|
className="grid gap-3"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
edit.transform((data) => ({ ...data, q: term }));
|
|
edit.put(endpoint, {
|
|
preserveScroll: true,
|
|
onSuccess: () => setEditing(false),
|
|
});
|
|
}}
|
|
>
|
|
<label className="grid gap-2 text-sm">
|
|
Nome da pesquisa
|
|
<input
|
|
className={controlClass}
|
|
value={edit.data.name}
|
|
maxLength={100}
|
|
required
|
|
onChange={(event) =>
|
|
edit.setData('name', event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-2 text-sm">
|
|
Texto a procurar
|
|
<input
|
|
className={controlClass}
|
|
value={edit.data.term}
|
|
minLength={2}
|
|
maxLength={100}
|
|
required
|
|
onChange={(event) =>
|
|
edit.setData('term', event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<FormErrors errors={edit.errors} />
|
|
<div className="flex flex-wrap gap-2">
|
|
<Button disabled={edit.processing}>
|
|
Guardar alterações
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => {
|
|
edit.reset();
|
|
setEditing(false);
|
|
}}
|
|
>
|
|
Cancelar edição
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
) : (
|
|
<>
|
|
{archived ? (
|
|
<p className="font-medium break-words">{row.name}</p>
|
|
) : (
|
|
<Link
|
|
className="font-medium break-words underline"
|
|
href={row.href}
|
|
>
|
|
{row.name}
|
|
</Link>
|
|
)}
|
|
<p className="text-sm break-words text-muted-foreground">
|
|
Critério: {row.term}
|
|
</p>
|
|
<div className="flex flex-wrap gap-2">
|
|
{!archived && (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => setEditing(true)}
|
|
aria-label={`Editar pesquisa ${row.name}`}
|
|
>
|
|
Editar
|
|
</Button>
|
|
)}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={action.processing}
|
|
aria-label={`${archived ? 'Restaurar' : 'Arquivar'} pesquisa ${row.name}`}
|
|
onClick={() => {
|
|
action.transform((data) => ({
|
|
...data,
|
|
q: term,
|
|
}));
|
|
action.post(
|
|
`${endpoint}/${archived ? 'restaurar' : 'arquivar'}`,
|
|
{ preserveScroll: true },
|
|
);
|
|
}}
|
|
>
|
|
{archived ? 'Restaurar' : 'Arquivar'}
|
|
</Button>
|
|
</div>
|
|
<FormErrors errors={action.errors} />
|
|
</>
|
|
)}
|
|
</li>
|
|
);
|
|
}
|
|
|
|
export function SavedProcurementSearches({
|
|
saved,
|
|
term,
|
|
}: {
|
|
saved: SavedSearches;
|
|
term: string;
|
|
}) {
|
|
const actor = usePage().props.auth?.user;
|
|
const [managing, setManaging] = useRemember(
|
|
false,
|
|
`saved-search-management:${actor?.tenant_id ?? 'none'}:${actor?.id ?? 'none'}`,
|
|
);
|
|
const create = useForm(
|
|
`saved-search:${actor?.tenant_id ?? 'none'}:${actor?.id ?? 'none'}:${term}`,
|
|
{ name: '', term, q: term },
|
|
);
|
|
const ref = usePrivatePreparation(create.data.name !== '');
|
|
return (
|
|
<Panel title="As minhas pesquisas guardadas">
|
|
<p className="text-sm text-muted-foreground">
|
|
Guarde apenas os critérios. Ao reabrir, os resultados são
|
|
consultados de novo com o seu acesso actual. Estas pesquisas são
|
|
privadas; arquivar conserva os critérios e permite restaurá-los.
|
|
</p>
|
|
<label
|
|
className="my-4 grid gap-2 text-sm"
|
|
htmlFor="saved-search-open"
|
|
>
|
|
Reabrir uma pesquisa
|
|
<select
|
|
id="saved-search-open"
|
|
className={controlClass}
|
|
value=""
|
|
disabled={saved.active.length === 0}
|
|
onChange={(event) => {
|
|
const row = saved.active.find(
|
|
(item) => String(item.id) === event.target.value,
|
|
);
|
|
if (row) router.get(row.href);
|
|
}}
|
|
>
|
|
<option value="">Seleccione uma pesquisa guardada</option>
|
|
{saved.active.map((row) => (
|
|
<option key={row.id} value={row.id}>
|
|
{row.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<details
|
|
open={managing}
|
|
onToggle={(event) => setManaging(event.currentTarget.open)}
|
|
className="rounded-lg border p-4"
|
|
>
|
|
<summary className="cursor-pointer">
|
|
Guardar, editar ou arquivar pesquisas
|
|
</summary>
|
|
<form
|
|
ref={ref}
|
|
className="my-4 grid gap-3"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
create.transform((data) => ({
|
|
...data,
|
|
term,
|
|
q: term,
|
|
}));
|
|
create.post('/compras/pesquisas-guardadas', {
|
|
preserveScroll: true,
|
|
onSuccess: () => create.reset('name'),
|
|
});
|
|
}}
|
|
>
|
|
<p className="text-sm break-words">
|
|
Pesquisa apresentada:{' '}
|
|
{term || 'Introduza primeiro o texto a procurar.'}
|
|
</p>
|
|
<label className="grid gap-2 text-sm">
|
|
Nome para guardar esta pesquisa
|
|
<input
|
|
className={controlClass}
|
|
maxLength={100}
|
|
value={create.data.name}
|
|
onChange={(event) =>
|
|
create.setData('name', event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<FormErrors errors={create.errors} />
|
|
<Button
|
|
className="justify-self-start"
|
|
disabled={
|
|
create.processing ||
|
|
!create.data.name.trim() ||
|
|
term.trim().length < 2
|
|
}
|
|
>
|
|
Guardar pesquisa
|
|
</Button>
|
|
</form>
|
|
<p className="text-sm">
|
|
{saved.active.length}{' '}
|
|
{saved.active.length === 1
|
|
? 'pesquisa activa'
|
|
: 'pesquisas activas'}{' '}
|
|
· até {saved.max_active} por utilizador.
|
|
</p>
|
|
<ul>
|
|
{saved.active.map((row) => (
|
|
<SavedSearchRow
|
|
key={`${row.id}:${row.version}`}
|
|
row={row}
|
|
term={term}
|
|
/>
|
|
))}
|
|
</ul>
|
|
{saved.archived.total > 0 && (
|
|
<details className="mt-4 rounded-lg border p-4">
|
|
<summary className="cursor-pointer">
|
|
Pesquisas arquivadas ({saved.archived.total})
|
|
</summary>
|
|
<ul>
|
|
{saved.archived.data.map((row) => (
|
|
<SavedSearchRow
|
|
key={`${row.id}:${row.version}`}
|
|
row={row}
|
|
term={term}
|
|
archived
|
|
/>
|
|
))}
|
|
</ul>
|
|
<nav
|
|
aria-label="Pesquisas arquivadas"
|
|
className="flex flex-wrap gap-4"
|
|
>
|
|
{saved.archived.prev_page_url && (
|
|
<Link href={saved.archived.prev_page_url}>
|
|
Anteriores
|
|
</Link>
|
|
)}
|
|
{saved.archived.next_page_url && (
|
|
<Link href={saved.archived.next_page_url}>
|
|
Seguintes
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
</details>
|
|
)}
|
|
</details>
|
|
</Panel>
|
|
);
|
|
}
|