commit16
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import InteractiveInvestmentMap, {
|
||||
type InvestmentMapArea,
|
||||
} from './interactive-investment-map';
|
||||
import InteractiveInvestmentMap from './interactive-investment-map';
|
||||
import {
|
||||
useTerritoryGeometry,
|
||||
type TerritoryGeometryContext,
|
||||
} from './use-territory-geometry';
|
||||
|
||||
const noPoints: [] = [];
|
||||
type Context = {
|
||||
selected_geometry_available: boolean;
|
||||
context_geometry_count: number;
|
||||
areas: InvestmentMapArea[];
|
||||
truncated: boolean;
|
||||
};
|
||||
export default function TerritoryGeometryPreview({
|
||||
territoryId,
|
||||
management = false,
|
||||
@@ -21,44 +16,37 @@ export default function TerritoryGeometryPreview({
|
||||
endpoint?: string;
|
||||
label?: string;
|
||||
}) {
|
||||
const [context, setContext] = useState<Context | null>(null);
|
||||
const [error, setError] = useState('');
|
||||
useEffect(() => {
|
||||
setContext(null);
|
||||
setError('');
|
||||
if (!territoryId) return;
|
||||
const controller = new AbortController();
|
||||
const endpoint =
|
||||
suppliedEndpoint ??
|
||||
const { context, error } = useTerritoryGeometry(
|
||||
territoryId,
|
||||
suppliedEndpoint ??
|
||||
(management
|
||||
? '/management/territory-geometries'
|
||||
: '/api/v1/investment/territory-geometries');
|
||||
void fetch(
|
||||
`${endpoint}?${new URLSearchParams({ territory_id: territoryId })}`,
|
||||
{
|
||||
signal: controller.signal,
|
||||
headers: { Accept: 'application/json' },
|
||||
},
|
||||
)
|
||||
.then(async (response) => {
|
||||
if (!response.ok)
|
||||
throw new Error(
|
||||
'Não foi possível consultar os limites administrativos.',
|
||||
);
|
||||
return response.json() as Promise<Context>;
|
||||
})
|
||||
.then((data) => {
|
||||
if (!controller.signal.aborted) setContext(data);
|
||||
})
|
||||
.catch((e: unknown) => {
|
||||
if (!controller.signal.aborted)
|
||||
setError(
|
||||
e instanceof Error ? e.message : 'Erro de ligação.',
|
||||
);
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [territoryId, management, suppliedEndpoint]);
|
||||
: '/api/v1/investment/territory-geometries'),
|
||||
);
|
||||
if (!territoryId) return null;
|
||||
return (
|
||||
<TerritoryGeometryDetails
|
||||
context={context}
|
||||
error={error}
|
||||
label={label}
|
||||
renderMap
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export function TerritoryGeometryDetails({
|
||||
context,
|
||||
error,
|
||||
label = 'Cobertura cartográfica da selecção',
|
||||
renderMap = false,
|
||||
showSources = true,
|
||||
}: {
|
||||
context: TerritoryGeometryContext | null;
|
||||
error: string;
|
||||
label?: string;
|
||||
renderMap?: boolean;
|
||||
showSources?: boolean;
|
||||
}) {
|
||||
return (
|
||||
<section className="space-y-2 md:col-span-full" aria-label={label}>
|
||||
<p>Território selecionado no cadastro.</p>
|
||||
@@ -91,26 +79,28 @@ export default function TerritoryGeometryPreview({
|
||||
incompleta.
|
||||
</p>
|
||||
)}
|
||||
{context.areas.length > 0 && (
|
||||
{renderMap && context.areas.length > 0 && (
|
||||
<InteractiveInvestmentMap
|
||||
points={noPoints}
|
||||
areas={context.areas}
|
||||
contextOnly
|
||||
/>
|
||||
)}
|
||||
<ul>
|
||||
{context.areas.map((area, index) => (
|
||||
<li key={index}>
|
||||
{area.title} ·{' '}
|
||||
{area.selected_territory
|
||||
? 'Território seleccionado'
|
||||
: 'Contexto de nível superior'}{' '}
|
||||
· Fonte:{' '}
|
||||
{area.source_reference ?? 'Não indicada'} ·
|
||||
Edição: {area.source_date ?? 'Não indicada'}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
{showSources && (
|
||||
<ul>
|
||||
{context.areas.map((area, index) => (
|
||||
<li key={index}>
|
||||
{area.title} ·{' '}
|
||||
{area.selected_territory
|
||||
? 'Território seleccionado'
|
||||
: 'Contexto de nível superior'}{' '}
|
||||
· Fonte:{' '}
|
||||
{area.source_reference ?? 'Não indicada'} ·
|
||||
Edição: {area.source_date ?? 'Não indicada'}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user