import { Link, useForm } from '@inertiajs/react'; import { useEffect, useRef } from 'react'; import { FormErrors, Panel } from '@/components/procurement'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; type Selection = { id: number; version: number; data: { role: string; supplier_id: number; instruction_document_id: number; }; snapshot: { supplier: { name: string; nuit: string; version: number }; instructions: { data: { reference: string } }; }; reason: string; issues?: string[]; }; type Page = { data: T[]; prev_page_url: string | null; next_page_url: string | null; }; export type ImportPartyWorkspace = { roles: Record; canSelect: boolean; search: string; suppliers: Page<{ id: number; name: string; nuit: string; version: number; }>; instructions: Page<{ id: number; version: number; data: { reference: string; issuer_reference: string }; }>; current: Selection[]; history: Page; }; function Pages({ page, }: { page: { prev_page_url: string | null; next_page_url: string | null }; }) { return (
{page.prev_page_url && ( Anterior )} {page.next_page_url && ( Seguinte )}
); } export default function ImportPartySelection({ workspace, importId, importKey, actorId, tenantId, }: { workspace: ImportPartyWorkspace; importId: number; importKey: string; actorId: number; tenantId: number; }) { const current = workspace.current.find((s) => s.data.role === 'broker'); const form = useForm(`import-parties:${tenantId}:${actorId}:${importKey}`, { operation_key: crypto.randomUUID(), role: 'broker', version: current?.version ?? 0, supplier_id: current ? String(current.data.supplier_id) : '', supplier_version: current?.snapshot.supplier.version ?? 0, supplier_label: current?.snapshot.supplier.name ?? '', instruction_document_id: current ? String(current.data.instruction_document_id) : '', instruction_label: current?.snapshot.instructions.data.reference ?? '', reason: '', no_conflict: false, }); const ref = useRef(null); useEffect(() => { const node = ref.current; window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form: node, dirty: form.isDirty }, }), ); return () => { window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form: node, dirty: false }, }), ); }; }, [form.isDirty]); const loadRole = (role: string) => { const selected = workspace.current.find((s) => s.data.role === role); form.setData({ operation_key: crypto.randomUUID(), role, version: selected?.version ?? 0, supplier_id: selected ? String(selected.data.supplier_id) : '', supplier_version: selected?.snapshot.supplier.version ?? 0, supplier_label: selected?.snapshot.supplier.name ?? '', instruction_document_id: selected ? String(selected.data.instruction_document_id) : '', instruction_label: selected?.snapshot.instructions.data.reference ?? '', reason: '', no_conflict: false, }); }; return (

A escolha reutiliza uma entidade elegível do cadastro e as instruções escritas deste dossiê. O comprador deve ser distinto dos requisitantes; os conflitos são novamente conferidos antes de continuar o trânsito ou a libertação.

{workspace.current.length === 0 ? (

Sem selecção registada.

) : ( workspace.current.map((selection) => (

{workspace.roles[selection.data.role]} ·{' '} {selection.snapshot.supplier.name}

Versão {selection.version} · Instruções:{' '} {selection.snapshot.instructions.data.reference}

{selection.issues?.map((issue) => (

{issue}

))}
)) )}
{workspace.canSelect && (
{ event.preventDefault(); form.transform( ({ supplier_label: _supplierLabel, instruction_label: _instructionLabel, ...data }) => data, ); form.post( `/compras/importacoes/${importId}/intervenientes`, ); }} >

Entidade seleccionada:{' '} {form.data.supplier_label || 'Por seleccionar'}

Instruções seleccionadas:{' '} {form.data.instruction_label || 'Por seleccionar'}

Versão da selecção em conferência:{' '} {form.data.version}