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(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 (
  • {editing ? (
    { event.preventDefault(); edit.transform((data) => ({ ...data, q: term })); edit.put(endpoint, { preserveScroll: true, onSuccess: () => setEditing(false), }); }} >
    ) : ( <> {archived ? (

    {row.name}

    ) : ( {row.name} )}

    Critério: {row.term}

    {!archived && ( )}
    )}
  • ); } 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 (

    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.

    setManaging(event.currentTarget.open)} className="rounded-lg border p-4" > Guardar, editar ou arquivar pesquisas
    { event.preventDefault(); create.transform((data) => ({ ...data, term, q: term, })); create.post('/compras/pesquisas-guardadas', { preserveScroll: true, onSuccess: () => create.reset('name'), }); }} >

    Pesquisa apresentada:{' '} {term || 'Introduza primeiro o texto a procurar.'}

    {saved.active.length}{' '} {saved.active.length === 1 ? 'pesquisa activa' : 'pesquisas activas'}{' '} · até {saved.max_active} por utilizador.

      {saved.active.map((row) => ( ))}
    {saved.archived.total > 0 && (
    Pesquisas arquivadas ({saved.archived.total})
      {saved.archived.data.map((row) => ( ))}
    )}
    ); }