@@ -1,32 +1,44 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import InvestmentTerritory from "@/components/investment-territory";
|
||||
import { Management, Field, control } from "./shared";
|
||||
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 [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 [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" },
|
||||
const params = new URLSearchParams({
|
||||
kind,
|
||||
q,
|
||||
page: String(page),
|
||||
});
|
||||
if (!response.ok) throw new Error("Não foi possível carregar os investimentos.");
|
||||
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("");
|
||||
setError('');
|
||||
} catch (e) {
|
||||
if (!controller.signal.aborted)
|
||||
setError(e instanceof Error ? e.message : "Erro de ligação.");
|
||||
setError(
|
||||
e instanceof Error ? e.message : 'Erro de ligação.',
|
||||
);
|
||||
}
|
||||
}, 250);
|
||||
return () => {
|
||||
@@ -42,16 +54,20 @@ export default function TerritoryAssociations() {
|
||||
value={kind}
|
||||
onChange={(e) => {
|
||||
setKind(e.target.value);
|
||||
setId("");
|
||||
setId('');
|
||||
setRows([]);
|
||||
setQ("");
|
||||
setQ('');
|
||||
setPage(1);
|
||||
}}
|
||||
>
|
||||
<option value="projects">Projectos / projectos âncora</option>
|
||||
<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="zones">
|
||||
ZEE / ZFI / parques industriais
|
||||
</option>
|
||||
<option value="opportunities">Oportunidades</option>
|
||||
</select>
|
||||
</Field>
|
||||
@@ -62,12 +78,16 @@ export default function TerritoryAssociations() {
|
||||
onChange={(e) => {
|
||||
setQ(e.target.value);
|
||||
setPage(1);
|
||||
setId("");
|
||||
setId('');
|
||||
}}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Investimento">
|
||||
<select className={control} value={id} onChange={(e) => setId(e.target.value)}>
|
||||
<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}>
|
||||
@@ -77,13 +97,15 @@ export default function TerritoryAssociations() {
|
||||
</select>
|
||||
</Field>
|
||||
{error && <p role="alert">{error}</p>}
|
||||
{!error && rows.length === 0 && <p>Sem registos para esta pesquisa.</p>}
|
||||
{!error && rows.length === 0 && (
|
||||
<p>Sem registos para esta pesquisa.</p>
|
||||
)}
|
||||
<div className="flex gap-4">
|
||||
{page > 1 && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setId("");
|
||||
setId('');
|
||||
setPage(page - 1);
|
||||
}}
|
||||
>
|
||||
@@ -94,7 +116,7 @@ export default function TerritoryAssociations() {
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setId("");
|
||||
setId('');
|
||||
setPage(page + 1);
|
||||
}}
|
||||
>
|
||||
@@ -102,7 +124,13 @@ export default function TerritoryAssociations() {
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{id && <InvestmentTerritory key={`${kind}-${id}`} kind={kind} id={id} />}
|
||||
{id && (
|
||||
<InvestmentTerritory
|
||||
key={`${kind}-${id}`}
|
||||
kind={kind}
|
||||
id={id}
|
||||
/>
|
||||
)}
|
||||
</Management>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user