import { useEffect, useState } from 'react'; import { useForm } from '@inertiajs/react'; import { portalRequest } from '@/lib/portal-api'; import { Field, button, control } from './shared'; export type SubjectField = { name: string; label: string; type: string; options: string[]; }; type Value = string | number | boolean | null; const names: Record = { draft: 'Rascunho', planned: 'Planeado', active: 'Activo', suspended: 'Suspenso', completed: 'Concluído', cancelled: 'Cancelado', inactive: 'Inactivo', proposed: 'Proposto', closed: 'Encerrado', input: 'Insumos', production: 'Produção', processing: 'Transformação', logistics: 'Logística', market: 'Mercado', supplies: 'Fornece', processes: 'Transforma', distributes: 'Distribui', supports: 'Apoia', official: 'Oficial', influence: 'Influência', proximity: 'Proximidade', potential: 'Potencial', historical: 'Histórica', anchor: 'Âncora', complementary: 'Complementar', supporting: 'Apoio', primary: 'Principal', beneficiary: 'Beneficiária', served: 'Servida', enabled: 'Viabilizada', dependent: 'Dependente', related: 'Relacionada', identified: 'Identificada', in_review: 'Em análise', resolved: 'Resolvida', withdrawn: 'Retirada', }; function Reference({ base, field, value, onChange, }: { base: string; field: SubjectField; value: Value; onChange: (value: Value) => void; }) { const [query, setQuery] = useState(''); const [items, setItems] = useState<{ id: string; name: string }[]>([]); const [message, setMessage] = useState(''); useEffect(() => { const abort = new AbortController(); const timer = setTimeout(() => { void portalRequest<{ data: { id: string; name: string }[] }>( `${base}/choices?${new URLSearchParams({ field: field.name, q: query })}`, { signal: abort.signal }, ) .then((data) => { setItems(data.data); setMessage(''); }) .catch((error) => { if (!abort.signal.aborted) setMessage( error instanceof Error ? error.message : 'Não foi possível consultar o catálogo.', ); }); }, 250); return () => { clearTimeout(timer); abort.abort(); }; }, [base, field.name, query]); return (
setQuery(event.target.value)} /> {items.length === 100 && (

Refine a pesquisa para consultar outros resultados.

)} {message &&

{message}

}
); } export default function SubjectForm({ base, revision, fields, initial, withdraw = false, }: { base: string; revision: number; fields: SubjectField[]; initial: Record; withdraw?: boolean; }) { const [open, setOpen] = useState(false); const form = useForm({ expected_revision: revision, withdraw, fields: Object.fromEntries( fields.map((field) => [field.name, initial[field.name] ?? null]), ) as Record, }); const set = (field: string, value: Value) => form.setData('fields', { ...form.data.fields, [field]: value }); return (
setOpen(event.currentTarget.open)} > Alterar proposta {open && (
{ event.preventDefault(); form.post(`${base}/drafts`, { preserveScroll: true }); }} >

Guarde uma nova edição. As decisões anteriores mantêm-se no histórico e não aprovam automaticamente esta alteração.

{fields.map((field) => ( )[ `fields.${field.name}` ] ?? (form.errors as Record)[ field.name ] } > {field.type === 'reference' ? ( set(field.name, value) } /> ) : field.type === 'enum' ? ( ) : field.type === 'boolean' ? ( set( field.name, event.target.checked, ) } /> ) : field.type === 'date' ? ( set( field.name, event.target.value || null, ) } /> ) : (