109 lines
4.2 KiB
TypeScript
109 lines
4.2 KiB
TypeScript
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("");
|
|
useEffect(() => {
|
|
const controller = new AbortController();
|
|
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();
|
|
setRows(data.data);
|
|
setMore(Boolean(data.next_page_url));
|
|
setError("");
|
|
} catch (e) {
|
|
if (!controller.signal.aborted)
|
|
setError(e instanceof Error ? e.message : "Erro de ligação.");
|
|
}
|
|
}, 250);
|
|
return () => {
|
|
clearTimeout(timer);
|
|
controller.abort();
|
|
};
|
|
}, [kind, q, page]);
|
|
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>
|
|
<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>
|
|
);
|
|
}
|