fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -100,34 +100,80 @@ export default function Zones({
|
||||
const pageUrl = usePage().url;
|
||||
const [error, setError] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
const searchForm=useRef<HTMLFormElement|null>(null);
|
||||
const searchTimer=useRef<ReturnType<typeof setTimeout>|undefined>(undefined);
|
||||
const cancelSearch=useRef<(()=>void)|undefined>(undefined);
|
||||
const searchSequence=useRef(0);
|
||||
const [searching,setSearching]=useState(false);
|
||||
const [searchError,setSearchError]=useState('');
|
||||
const appliedKey=JSON.stringify([filters,territorySelection]);
|
||||
useEffect(()=>{searchForm.current?.reset();},[appliedKey]);
|
||||
useEffect(()=>()=>{clearTimeout(searchTimer.current);cancelSearch.current?.();},[]);
|
||||
const runSearch=(path?:TerritoryOption[])=>{
|
||||
clearTimeout(searchTimer.current);cancelSearch.current?.();
|
||||
if(!searchForm.current)return;
|
||||
const sequence=++searchSequence.current;
|
||||
const params=territorialSearchParams(new FormData(searchForm.current),path);
|
||||
setSearching(true);setSearchError('');
|
||||
router.get(`${management?'/management/investment/zones':'/zonas'}?${params}`,{}, {
|
||||
preserveState:true,preserveScroll:true,replace:true,
|
||||
onCancelToken:token=>{cancelSearch.current=()=>token.cancel();},
|
||||
onError:()=>{if(sequence===searchSequence.current)setSearchError('A pesquisa não foi concluída. Confira os filtros e tente novamente.');},
|
||||
onNetworkError:()=>{if(sequence===searchSequence.current)setSearchError('Ligação indisponível. Os filtros escolhidos foram preservados.');return false;},
|
||||
onHttpException:()=>{if(sequence===searchSequence.current)setSearchError('Não foi possível consultar esta selecção.');return false;},
|
||||
onFinish:()=>{if(sequence===searchSequence.current)setSearching(false);},
|
||||
});
|
||||
const searchForm = useRef<HTMLFormElement | null>(null);
|
||||
const searchTimer = useRef<ReturnType<typeof setTimeout> | undefined>(
|
||||
undefined,
|
||||
);
|
||||
const cancelSearch = useRef<(() => void) | undefined>(undefined);
|
||||
const searchSequence = useRef(0);
|
||||
const [searching, setSearching] = useState(false);
|
||||
const [searchError, setSearchError] = useState('');
|
||||
const appliedKey = JSON.stringify([filters, territorySelection]);
|
||||
useEffect(() => {
|
||||
searchForm.current?.reset();
|
||||
}, [appliedKey]);
|
||||
useEffect(
|
||||
() => () => {
|
||||
clearTimeout(searchTimer.current);
|
||||
cancelSearch.current?.();
|
||||
},
|
||||
[],
|
||||
);
|
||||
const runSearch = (path?: TerritoryOption[]) => {
|
||||
clearTimeout(searchTimer.current);
|
||||
cancelSearch.current?.();
|
||||
if (!searchForm.current) return;
|
||||
const sequence = ++searchSequence.current;
|
||||
const params = territorialSearchParams(
|
||||
new FormData(searchForm.current),
|
||||
path,
|
||||
);
|
||||
setSearching(true);
|
||||
setSearchError('');
|
||||
router.get(
|
||||
`${management ? '/management/investment/zones' : '/zonas'}?${params}`,
|
||||
{},
|
||||
{
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
replace: true,
|
||||
onCancelToken: (token) => {
|
||||
cancelSearch.current = () => token.cancel();
|
||||
},
|
||||
onError: () => {
|
||||
if (sequence === searchSequence.current)
|
||||
setSearchError(
|
||||
'A pesquisa não foi concluída. Confira os filtros e tente novamente.',
|
||||
);
|
||||
},
|
||||
onNetworkError: () => {
|
||||
if (sequence === searchSequence.current)
|
||||
setSearchError(
|
||||
'Ligação indisponível. Os filtros escolhidos foram preservados.',
|
||||
);
|
||||
return false;
|
||||
},
|
||||
onHttpException: () => {
|
||||
if (sequence === searchSequence.current)
|
||||
setSearchError(
|
||||
'Não foi possível consultar esta selecção.',
|
||||
);
|
||||
return false;
|
||||
},
|
||||
onFinish: () => {
|
||||
if (sequence === searchSequence.current)
|
||||
setSearching(false);
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
const exportParams = new URLSearchParams();
|
||||
['province_id', 'district_id', 'admin_post_id', 'locality_id'].forEach((field, index) => {
|
||||
if (territorySelection[index]) exportParams.set(field, territorySelection[index].id);
|
||||
});
|
||||
['province_id', 'district_id', 'admin_post_id', 'locality_id'].forEach(
|
||||
(field, index) => {
|
||||
if (territorySelection[index])
|
||||
exportParams.set(field, territorySelection[index].id);
|
||||
},
|
||||
);
|
||||
for (const key of [
|
||||
'q',
|
||||
'category',
|
||||
@@ -183,9 +229,28 @@ export default function Zones({
|
||||
>
|
||||
<Link href="/clusters">Por Cluster</Link>
|
||||
<Link href="/corredores">Por Corredor</Link>
|
||||
<Link href={searchTerritoryHref('/zonas?category=zee', pageUrl)}>Por ZEE</Link>
|
||||
<Link href={searchTerritoryHref('/zonas?category=zfi', pageUrl)}>Por ZFI</Link>
|
||||
<Link href={searchTerritoryHref('/zonas?category=industrial_park', pageUrl)}>
|
||||
<Link
|
||||
href={searchTerritoryHref(
|
||||
'/zonas?category=zee',
|
||||
pageUrl,
|
||||
)}
|
||||
>
|
||||
Por ZEE
|
||||
</Link>
|
||||
<Link
|
||||
href={searchTerritoryHref(
|
||||
'/zonas?category=zfi',
|
||||
pageUrl,
|
||||
)}
|
||||
>
|
||||
Por ZFI
|
||||
</Link>
|
||||
<Link
|
||||
href={searchTerritoryHref(
|
||||
'/zonas?category=industrial_park',
|
||||
pageUrl,
|
||||
)}
|
||||
>
|
||||
Parques industriais
|
||||
</Link>
|
||||
</nav>
|
||||
@@ -194,10 +259,14 @@ export default function Zones({
|
||||
<form
|
||||
ref={searchForm}
|
||||
method="get"
|
||||
onChange={(event)=>{
|
||||
if(!event.target.getAttribute('name'))return;
|
||||
clearTimeout(searchTimer.current);cancelSearch.current?.();searchSequence.current++;
|
||||
setSearching(true);setSearchError('');searchTimer.current=setTimeout(()=>runSearch(),350);
|
||||
onChange={(event) => {
|
||||
if (!event.target.getAttribute('name')) return;
|
||||
clearTimeout(searchTimer.current);
|
||||
cancelSearch.current?.();
|
||||
searchSequence.current++;
|
||||
setSearching(true);
|
||||
setSearchError('');
|
||||
searchTimer.current = setTimeout(() => runSearch(), 350);
|
||||
}}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -333,13 +402,35 @@ export default function Zones({
|
||||
)}
|
||||
</div>
|
||||
</details>
|
||||
<TerritoryFields initial={territorySelection} onChange={path=>runSearch(path)} />
|
||||
<button type="button" className="underline" onClick={()=>runSearch([])}>Todo o território (remover localização)</button>
|
||||
<TerritoryFields
|
||||
initial={territorySelection}
|
||||
onChange={(path) => runSearch(path)}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="underline"
|
||||
onClick={() => runSearch([])}
|
||||
>
|
||||
Todo o território (remover localização)
|
||||
</button>
|
||||
<button className={button}>Pesquisar</button>
|
||||
</form>
|
||||
<h2 className="text-xl font-semibold">{searching?'A pesquisar a selecção…':searchError?'Pesquisa por concluir':`${records.total} registos disponíveis · ${territorySelection.length?territorySelection.map(node=>node.name).join(' / '):'Todo o território'}`}</h2>
|
||||
{!searching&&!searchError&&records.total>0&&<p>A mostrar {records.from??1} a {records.to??records.data.length} de {records.total}. Página {records.current_page??1} de {records.last_page??1}.</p>}
|
||||
{searchError&&<p role="alert">{searchError}</p>}
|
||||
<h2 className="text-xl font-semibold">
|
||||
{searching
|
||||
? 'A pesquisar a selecção…'
|
||||
: searchError
|
||||
? 'Pesquisa por concluir'
|
||||
: `${records.total} registos disponíveis · ${territorySelection.length ? territorySelection.map((node) => node.name).join(' / ') : 'Todo o território'}`}
|
||||
</h2>
|
||||
{!searching && !searchError && records.total > 0 && (
|
||||
<p>
|
||||
A mostrar {records.from ?? 1} a{' '}
|
||||
{records.to ?? records.data.length} de {records.total}.
|
||||
Página {records.current_page ?? 1} de{' '}
|
||||
{records.last_page ?? 1}.
|
||||
</p>
|
||||
)}
|
||||
{searchError && <p role="alert">{searchError}</p>}
|
||||
{!management && !searching && !searchError && (
|
||||
<a
|
||||
className="underline"
|
||||
@@ -353,9 +444,21 @@ export default function Zones({
|
||||
Não foram encontrados resultados para esta pesquisa.
|
||||
</p>
|
||||
)}
|
||||
{!searching && !searchError && records.data.map((zone) => (
|
||||
<article className={card} key={zone.id}>
|
||||
<h2 className="text-xl font-bold">
|
||||
{!searching &&
|
||||
!searchError &&
|
||||
records.data.map((zone) => (
|
||||
<article className={card} key={zone.id}>
|
||||
<h2 className="text-xl font-bold">
|
||||
<Link
|
||||
className="underline"
|
||||
href={catalogueDetailHref(
|
||||
`${management ? '/management/investment/zones' : '/zonas'}/${zone.id}`,
|
||||
exportParams.toString(),
|
||||
)}
|
||||
>
|
||||
{zone.name}
|
||||
</Link>
|
||||
</h2>
|
||||
<Link
|
||||
className="underline"
|
||||
href={catalogueDetailHref(
|
||||
@@ -363,123 +466,121 @@ export default function Zones({
|
||||
exportParams.toString(),
|
||||
)}
|
||||
>
|
||||
{zone.name}
|
||||
Consultar ficha
|
||||
</Link>
|
||||
</h2>
|
||||
<Link
|
||||
className="underline"
|
||||
href={catalogueDetailHref(
|
||||
`${management ? '/management/investment/zones' : '/zonas'}/${zone.id}`,
|
||||
exportParams.toString(),
|
||||
{!management && (
|
||||
<Link
|
||||
className="ml-4 underline"
|
||||
href={searchTerritoryHref(
|
||||
`/oportunidades?${new URLSearchParams({ [zone.category === 'zee' ? 'zee_id' : zone.category === 'zfi' ? 'zfi_id' : 'park_id']: zone.id })}`,
|
||||
pageUrl,
|
||||
)}
|
||||
>
|
||||
Ver oportunidades associadas
|
||||
</Link>
|
||||
)}
|
||||
>
|
||||
Consultar ficha
|
||||
</Link>
|
||||
{!management && (
|
||||
<Link
|
||||
className="ml-4 underline"
|
||||
href={searchTerritoryHref(`/oportunidades?${new URLSearchParams({ [zone.category === 'zee' ? 'zee_id' : zone.category === 'zfi' ? 'zfi_id' : 'park_id']: zone.id })}`, pageUrl)}
|
||||
>
|
||||
Ver oportunidades associadas
|
||||
</Link>
|
||||
)}
|
||||
<p>
|
||||
{zone.code} ·{' '}
|
||||
{zone.category === 'industrial_park'
|
||||
? 'Parque industrial'
|
||||
: zone.category.toUpperCase()}{' '}
|
||||
·{' '}
|
||||
{[zone.district, zone.province]
|
||||
.filter(Boolean)
|
||||
.join(', ') || 'Localização por confirmar'}
|
||||
</p>
|
||||
{zone.legal_regime && (
|
||||
<p>
|
||||
Regime jurídico:{' '}
|
||||
{parkLabels[zone.legal_regime] ??
|
||||
(['zee', 'zfi'].includes(zone.legal_regime)
|
||||
? zone.legal_regime.toUpperCase()
|
||||
: 'Por confirmar')}
|
||||
{zone.code} ·{' '}
|
||||
{zone.category === 'industrial_park'
|
||||
? 'Parque industrial'
|
||||
: zone.category.toUpperCase()}{' '}
|
||||
·{' '}
|
||||
{[zone.district, zone.province]
|
||||
.filter(Boolean)
|
||||
.join(', ') || 'Localização por confirmar'}
|
||||
</p>
|
||||
)}
|
||||
{zoneExtensionsAvailable && zone.development_status && (
|
||||
<p>
|
||||
Desenvolvimento:{' '}
|
||||
{parkLabels[zone.development_status] ??
|
||||
'Por confirmar'}
|
||||
</p>
|
||||
)}
|
||||
{zoneExtensionsAvailable && zone.availability_status && (
|
||||
<p>
|
||||
Disponibilidade para instalação:{' '}
|
||||
{parkLabels[zone.availability_status] ??
|
||||
'Não informada'}
|
||||
</p>
|
||||
)}
|
||||
{zone.area_value && (
|
||||
<p>
|
||||
Área indicada: {zone.area_value} {zone.area_unit}
|
||||
</p>
|
||||
)}
|
||||
{zone.locations?.length ? (
|
||||
<p>
|
||||
Abrangência:{' '}
|
||||
{zone.locations
|
||||
.map((location) => location.name)
|
||||
.join(' · ')}
|
||||
</p>
|
||||
) : null}
|
||||
{zone.activities?.length ? (
|
||||
<p>
|
||||
Actividades:{' '}
|
||||
{zone.activities
|
||||
.map(
|
||||
(activity) =>
|
||||
`${activity.name} (${({ potential: 'Potencial referido pela fonte', proposed: 'Proposta', authorized: 'Autorizada por documento', conditional: 'Condicionada', excluded: 'Excluída' } as Record<string, string>)[activity.status] ?? 'Por confirmar'})`,
|
||||
)
|
||||
.join(' · ')}
|
||||
</p>
|
||||
) : null}
|
||||
{!!zone.source_evidence?.length && (
|
||||
<p>
|
||||
Fonte:{' '}
|
||||
{zone.source_evidence.map((source, index) => (
|
||||
<span key={source.url}>
|
||||
{index > 0 ? ' · ' : ''}
|
||||
<a
|
||||
className="underline"
|
||||
href={source.url}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{source.title}
|
||||
</a>
|
||||
{source.source_date
|
||||
? ` (${source.source_date})`
|
||||
: ' (data da fonte não informada)'}{' '}
|
||||
— {source.supports}
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
)}
|
||||
{zone.category === 'industrial_park' && (
|
||||
<p>
|
||||
{zone.resources?.length
|
||||
? `${zone.resources.length} recursos descritos; consulte o âmbito e a fonte na ficha.`
|
||||
: 'Infraestruturas, serviços, acessos e capacidades: não informados.'}{' '}
|
||||
{!zone.activities?.length &&
|
||||
'Atividades: não informadas.'}
|
||||
</p>
|
||||
)}
|
||||
<p>{zone.description}</p>
|
||||
{management && <p>Validação: {zone.validation_status}</p>}
|
||||
{management && (
|
||||
<p>
|
||||
Versão {zone.version_no} · {zone.publication_status}
|
||||
</p>
|
||||
)}
|
||||
</article>
|
||||
))}
|
||||
{zone.legal_regime && (
|
||||
<p>
|
||||
Regime jurídico:{' '}
|
||||
{parkLabels[zone.legal_regime] ??
|
||||
(['zee', 'zfi'].includes(zone.legal_regime)
|
||||
? zone.legal_regime.toUpperCase()
|
||||
: 'Por confirmar')}
|
||||
</p>
|
||||
)}
|
||||
{zoneExtensionsAvailable && zone.development_status && (
|
||||
<p>
|
||||
Desenvolvimento:{' '}
|
||||
{parkLabels[zone.development_status] ??
|
||||
'Por confirmar'}
|
||||
</p>
|
||||
)}
|
||||
{zoneExtensionsAvailable &&
|
||||
zone.availability_status && (
|
||||
<p>
|
||||
Disponibilidade para instalação:{' '}
|
||||
{parkLabels[zone.availability_status] ??
|
||||
'Não informada'}
|
||||
</p>
|
||||
)}
|
||||
{zone.area_value && (
|
||||
<p>
|
||||
Área indicada: {zone.area_value}{' '}
|
||||
{zone.area_unit}
|
||||
</p>
|
||||
)}
|
||||
{zone.locations?.length ? (
|
||||
<p>
|
||||
Abrangência:{' '}
|
||||
{zone.locations
|
||||
.map((location) => location.name)
|
||||
.join(' · ')}
|
||||
</p>
|
||||
) : null}
|
||||
{zone.activities?.length ? (
|
||||
<p>
|
||||
Actividades:{' '}
|
||||
{zone.activities
|
||||
.map(
|
||||
(activity) =>
|
||||
`${activity.name} (${({ potential: 'Potencial referido pela fonte', proposed: 'Proposta', authorized: 'Autorizada por documento', conditional: 'Condicionada', excluded: 'Excluída' } as Record<string, string>)[activity.status] ?? 'Por confirmar'})`,
|
||||
)
|
||||
.join(' · ')}
|
||||
</p>
|
||||
) : null}
|
||||
{!!zone.source_evidence?.length && (
|
||||
<p>
|
||||
Fonte:{' '}
|
||||
{zone.source_evidence.map((source, index) => (
|
||||
<span key={source.url}>
|
||||
{index > 0 ? ' · ' : ''}
|
||||
<a
|
||||
className="underline"
|
||||
href={source.url}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{source.title}
|
||||
</a>
|
||||
{source.source_date
|
||||
? ` (${source.source_date})`
|
||||
: ' (data da fonte não informada)'}{' '}
|
||||
— {source.supports}
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
)}
|
||||
{zone.category === 'industrial_park' && (
|
||||
<p>
|
||||
{zone.resources?.length
|
||||
? `${zone.resources.length} recursos descritos; consulte o âmbito e a fonte na ficha.`
|
||||
: 'Infraestruturas, serviços, acessos e capacidades: não informados.'}{' '}
|
||||
{!zone.activities?.length &&
|
||||
'Atividades: não informadas.'}
|
||||
</p>
|
||||
)}
|
||||
<p>{zone.description}</p>
|
||||
{management && (
|
||||
<p>Validação: {zone.validation_status}</p>
|
||||
)}
|
||||
{management && (
|
||||
<p>
|
||||
Versão {zone.version_no} ·{' '}
|
||||
{zone.publication_status}
|
||||
</p>
|
||||
)}
|
||||
</article>
|
||||
))}
|
||||
<nav aria-label="Paginação" className="flex gap-4 underline">
|
||||
{!searching && !searchError && records.prev_page_url && (
|
||||
<Link href={records.prev_page_url}>Anterior</Link>
|
||||
|
||||
Reference in New Issue
Block a user