@@ -40,7 +40,10 @@ export function ProvinceSelect({
|
||||
const [error, setError] = useState('');
|
||||
const [retry, setRetry] = useState(0);
|
||||
const [search, setSearch] = useState('');
|
||||
const [geometrySelection, setGeometrySelection] = useState<{ value: string; id: string } | null>(null);
|
||||
const [geometrySelection, setGeometrySelection] = useState<{
|
||||
value: string;
|
||||
id: string;
|
||||
} | null>(null);
|
||||
const [geometryFeedback, setGeometryFeedback] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
@@ -49,19 +52,38 @@ export function ProvinceSelect({
|
||||
setGeometryFeedback('');
|
||||
if (!value) return;
|
||||
// Preserve historical text. Only one exact catalogue match supplies a map ID.
|
||||
void fetch(`${endpoint}?${new URLSearchParams({ type: 'province', page: '1', q: value })}`, {
|
||||
signal: controller.signal, headers: { Accept: 'application/json' },
|
||||
}).then(async (response) => {
|
||||
if (!response.ok) throw new Error('Não foi possível consultar a correspondência cartográfica.');
|
||||
const data = await response.json();
|
||||
const matches = (data.data as Option[]).filter((option) => option.name === value);
|
||||
if (controller.signal.aborted) return;
|
||||
if (matches.length === 1 && !data.next_page_url)
|
||||
setGeometrySelection({ value, id: matches[0].id });
|
||||
else setGeometryFeedback('Seleção guardada preservada; sem correspondência cartográfica inequívoca neste cadastro.');
|
||||
}).catch((cause: unknown) => {
|
||||
if (!controller.signal.aborted) setGeometryFeedback(cause instanceof Error ? cause.message : 'Erro de ligação.');
|
||||
});
|
||||
void fetch(
|
||||
`${endpoint}?${new URLSearchParams({ type: 'province', page: '1', q: value })}`,
|
||||
{
|
||||
signal: controller.signal,
|
||||
headers: { Accept: 'application/json' },
|
||||
},
|
||||
)
|
||||
.then(async (response) => {
|
||||
if (!response.ok)
|
||||
throw new Error(
|
||||
'Não foi possível consultar a correspondência cartográfica.',
|
||||
);
|
||||
const data = await response.json();
|
||||
const matches = (data.data as Option[]).filter(
|
||||
(option) => option.name === value,
|
||||
);
|
||||
if (controller.signal.aborted) return;
|
||||
if (matches.length === 1 && !data.next_page_url)
|
||||
setGeometrySelection({ value, id: matches[0].id });
|
||||
else
|
||||
setGeometryFeedback(
|
||||
'Seleção guardada preservada; sem correspondência cartográfica inequívoca neste cadastro.',
|
||||
);
|
||||
})
|
||||
.catch((cause: unknown) => {
|
||||
if (!controller.signal.aborted)
|
||||
setGeometryFeedback(
|
||||
cause instanceof Error
|
||||
? cause.message
|
||||
: 'Erro de ligação.',
|
||||
);
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [endpoint, value, retry]);
|
||||
|
||||
@@ -153,9 +175,17 @@ export function ProvinceSelect({
|
||||
)}
|
||||
{geometryFeedback && <p role="status">{geometryFeedback}</p>}
|
||||
<TerritoryGeometryPreview
|
||||
territoryId={geometrySelection && geometrySelection.value === value ? geometrySelection.id : undefined}
|
||||
territoryId={
|
||||
geometrySelection && geometrySelection.value === value
|
||||
? geometrySelection.id
|
||||
: undefined
|
||||
}
|
||||
management={endpoint.startsWith('/management/')}
|
||||
endpoint={endpoint.startsWith('/minhas-oportunidades/') ? '/minhas-oportunidades/territory-geometries' : undefined}
|
||||
endpoint={
|
||||
endpoint.startsWith('/minhas-oportunidades/')
|
||||
? '/minhas-oportunidades/territory-geometries'
|
||||
: undefined
|
||||
}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
@@ -211,7 +241,11 @@ export function TerritoryFields({
|
||||
<TerritoryGeometryPreview
|
||||
territoryId={path.at(-1)?.id}
|
||||
management={endpoint.startsWith('/management/')}
|
||||
endpoint={endpoint.startsWith('/minhas-oportunidades/') ? '/minhas-oportunidades/territory-geometries' : undefined}
|
||||
endpoint={
|
||||
endpoint.startsWith('/minhas-oportunidades/')
|
||||
? '/minhas-oportunidades/territory-geometries'
|
||||
: undefined
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user