150 lines
5.3 KiB
TypeScript
150 lines
5.3 KiB
TypeScript
import SearchState from '@/components/search-state';
|
|
import { useEffect, useState } from 'react';
|
|
import InvestmentTerritory from '@/components/investment-territory';
|
|
import { Management, Field, control } from './shared';
|
|
|
|
export default function TerritoryAssociations() {
|
|
const [kind, setKind] = useState('projects');
|
|
const [id, setId] = useState('');
|
|
const [q, setQ] = useState('');
|
|
const [page, setPage] = useState(1);
|
|
const [rows, setRows] = useState<{ id: string; name: string }[]>([]);
|
|
const [more, setMore] = useState(false);
|
|
const [error, setError] = useState('');
|
|
const [loading, setLoading] = useState(true);
|
|
const [retry, setRetry] = useState(0);
|
|
useEffect(() => {
|
|
const controller = new AbortController();
|
|
setLoading(true);
|
|
setError('');
|
|
setRows([]);
|
|
const timer = setTimeout(async () => {
|
|
try {
|
|
const params = new URLSearchParams({
|
|
kind,
|
|
q,
|
|
page: String(page),
|
|
});
|
|
const response = await fetch(
|
|
`/management/territory-subjects?${params}`,
|
|
{
|
|
signal: controller.signal,
|
|
headers: { Accept: 'application/json' },
|
|
},
|
|
);
|
|
if (!response.ok)
|
|
throw new Error(
|
|
'Não foi possível carregar os investimentos.',
|
|
);
|
|
const data = await response.json();
|
|
if (controller.signal.aborted) return;
|
|
setRows(data.data);
|
|
setMore(Boolean(data.next_page_url));
|
|
setError('');
|
|
} catch {
|
|
if (!controller.signal.aborted)
|
|
setError('Não foi possível concluir a pesquisa.');
|
|
} finally {
|
|
if (!controller.signal.aborted) setLoading(false);
|
|
}
|
|
}, 250);
|
|
return () => {
|
|
clearTimeout(timer);
|
|
controller.abort();
|
|
};
|
|
}, [kind, q, page, retry]);
|
|
return (
|
|
<Management title="Localizações dos investimentos">
|
|
<Field label="Tipo de investimento">
|
|
<select
|
|
className={control}
|
|
value={kind}
|
|
onChange={(e) => {
|
|
setKind(e.target.value);
|
|
setId('');
|
|
setRows([]);
|
|
setQ('');
|
|
setPage(1);
|
|
}}
|
|
>
|
|
<option value="projects">
|
|
Projectos / projectos âncora
|
|
</option>
|
|
<option value="clusters">Clusters</option>
|
|
<option value="corridors">Corredores</option>
|
|
<option value="zones">
|
|
ZEE / ZFI / parques industriais
|
|
</option>
|
|
<option value="opportunities">Oportunidades</option>
|
|
</select>
|
|
</Field>
|
|
<SearchState
|
|
loading={loading}
|
|
error={error}
|
|
total={rows.length}
|
|
retry={() => setRetry((value) => value + 1)}
|
|
/>
|
|
<Field label="Pesquisar investimento">
|
|
<input
|
|
className={control}
|
|
value={q}
|
|
onChange={(e) => {
|
|
setQ(e.target.value);
|
|
setPage(1);
|
|
setId('');
|
|
}}
|
|
/>
|
|
</Field>
|
|
<Field label="Investimento">
|
|
<select
|
|
className={control}
|
|
value={id}
|
|
onChange={(e) => setId(e.target.value)}
|
|
>
|
|
<option value="">Seleccione</option>
|
|
{rows.map((row) => (
|
|
<option value={row.id} key={row.id}>
|
|
{row.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
{error && <p role="alert">{error}</p>}
|
|
{!error && rows.length === 0 && (
|
|
<p>Sem registos para esta pesquisa.</p>
|
|
)}
|
|
<div className="flex gap-4">
|
|
{page > 1 && (
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setId('');
|
|
setPage(page - 1);
|
|
}}
|
|
>
|
|
Anterior
|
|
</button>
|
|
)}
|
|
{more && (
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setId('');
|
|
setPage(page + 1);
|
|
}}
|
|
>
|
|
Mais investimentos
|
|
</button>
|
|
)}
|
|
</div>
|
|
{id && (
|
|
<InvestmentTerritory
|
|
key={`${kind}-${id}`}
|
|
kind={kind}
|
|
id={id}
|
|
/>
|
|
)}
|
|
</Management>
|
|
);
|
|
}
|