import { useEffect, useId, useState } from 'react'; import { useForm } from '@inertiajs/react'; import { Button } from '@/components/ui/button'; import { FormErrors, controlClass } from '@/components/procurement'; export type GeographyReference = { country: string; catalogue: string; record_id: string; }; type Node = { id: string; name: string; level: number; parent_id: string | null; }; export type GeographyDescription = { reference: GeographyReference; path: Node[]; requires_review: boolean; } | null; const levels = [ 'Província', 'Distrito', 'Posto administrativo', 'Localidade', 'Bairro urbano', 'Aldeia / quarteirão', ]; export function AdministrativeLocation({ value, initial = null, onChange, endpoint = '/api/divisao-administrativa', maxLevel = 6, legend = 'Localização administrativa de Moçambique (opcional)', hint = 'Escolha apenas até ao nível conhecido. A morada mantém-se. Para endereços estrangeiros, deixe esta referência vazia.', }: { value: GeographyReference | null; initial?: GeographyDescription; endpoint?: string; maxLevel?: number; legend?: string; hint?: string; onChange: ( value: GeographyReference | null, description?: GeographyDescription, ) => void; }) { const id = useId(); const [path, setPath] = useState(initial?.path ?? []); const [options, setOptions] = useState([]); const [page, setPage] = useState(1); const [more, setMore] = useState(false); const [catalogue, setCatalogue] = useState(value?.catalogue ?? ''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const activePath = value ? path : []; const parent = activePath.at(-1)?.id ?? ''; const atLastLevel = (activePath.at(-1)?.level ?? 0) >= maxLevel; const queryPage = value ? page : 1; const [retry, setRetry] = useState(0); useEffect(() => { if (atLastLevel) { setOptions([]); setMore(false); setLoading(false); setError(''); return; } const controller = new AbortController(); setLoading(true); setError(''); fetch( `${endpoint}?${new URLSearchParams({ parent, page: String(queryPage) })}`, { headers: { Accept: 'application/json' }, signal: controller.signal, }, ) .then(async (response) => { if (!response.ok) throw new Error( 'Não foi possível carregar as localidades. Tente novamente.', ); return response.json() as Promise<{ data: Node[]; has_more: boolean; catalogue: string; }>; }) .then((result) => { if (controller.signal.aborted) return; setOptions((previous) => queryPage === 1 ? result.data : [...previous, ...result.data], ); setMore(result.has_more); setCatalogue(result.catalogue); }) .catch((failure: Error) => { if (!controller.signal.aborted) setError(failure.message); }) .finally(() => { if (!controller.signal.aborted) setLoading(false); }); return () => controller.abort(); }, [parent, queryPage, retry, endpoint, atLastLevel]); const select = (next: Node[]) => { setPath(next); setOptions([]); setPage(1); const node = next.at(-1); onChange( node ? { country: 'MZ', catalogue, record_id: node.id } : null, node ? { reference: { country: 'MZ', catalogue, record_id: node.id, }, path: next, requires_review: false, } : null, ); }; return (
{legend}

{hint}

{activePath.map((node) => node.name).join(' / ') || 'Sem referência administrativa'}

{initial?.requires_review && (

A referência anterior precisa de revisão. Escolha um percurso confirmado.

)} {error && ( )} {error && (

{error}

)}
{more && ( )} {activePath.length > 0 && ( )} {value && ( )}
); } export function CompanyAdministrativeLocation({ geography, }: { geography: GeographyDescription; }) { const form = useForm({ geography_reference: geography?.reference ?? (null as GeographyReference | null), expected_reference: geography?.reference ?? null, }); useEffect(() => { form.setData('expected_reference', geography?.reference ?? null); }, [geography]); return (
{ event.preventDefault(); form.post('/empresa/localizacao', { preserveScroll: true }); }} > form.setData('geography_reference', value)} /> ); }