245 lines
11 KiB
TypeScript
245 lines
11 KiB
TypeScript
import { Field, input, button, card } from "./shared";
|
|
|
|
export type ZoneTargets = {
|
|
opportunities: { id: number; title: string }[];
|
|
dossiers: {
|
|
id: string;
|
|
title: string;
|
|
project_id: string | null;
|
|
cluster_id: string | null;
|
|
corridor_id: string | null;
|
|
}[];
|
|
};
|
|
type Review = {
|
|
id: string;
|
|
version_no: number;
|
|
validation_status: string;
|
|
association_type?: string;
|
|
purpose?: string;
|
|
};
|
|
type Props = {
|
|
id: string;
|
|
targets: ZoneTargets;
|
|
sources: { id: string; title: string }[];
|
|
relations: Review[];
|
|
geometries: Review[];
|
|
busy: boolean;
|
|
action: (url: string, data: unknown) => Promise<void>;
|
|
};
|
|
export default function ZoneRelations({
|
|
id,
|
|
targets,
|
|
sources,
|
|
relations,
|
|
geometries,
|
|
busy,
|
|
action,
|
|
}: Props) {
|
|
return (
|
|
<>
|
|
<form
|
|
className={card + " grid gap-4 md:grid-cols-2"}
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
const data = Object.fromEntries(new FormData(e.currentTarget));
|
|
if (typeof data.target !== "string") return;
|
|
const [kind, target] = data.target.split(":");
|
|
delete data.target;
|
|
void action(
|
|
`/api/v2/investment/admin/zones/${id}/${kind === "opportunity_id" ? "links" : "relations"}`,
|
|
{ ...data, [kind]: target },
|
|
);
|
|
}}
|
|
>
|
|
<h2 className="text-xl font-bold md:col-span-2">
|
|
Associar investimento ou território
|
|
</h2>
|
|
<Field label="Registo existente">
|
|
<select className={input} name="target" required>
|
|
<option value="">Seleccionar</option>
|
|
{targets.opportunities.map((o) => (
|
|
<option key={o.id} value={`opportunity_id:${o.id}`}>
|
|
{o.title} · oportunidade
|
|
</option>
|
|
))}
|
|
{targets.dossiers.map((d) => {
|
|
const field = d.project_id
|
|
? "project_id"
|
|
: d.cluster_id
|
|
? "cluster_id"
|
|
: "corridor_id";
|
|
return (
|
|
<option key={d.id} value={`${field}:${d[field]}`}>
|
|
{d.title} · ficha territorial
|
|
</option>
|
|
);
|
|
})}
|
|
</select>
|
|
</Field>
|
|
<Field label="Tipo de associação">
|
|
<select className={input} name="association_type">
|
|
<option value="potential">Potencial</option>
|
|
<option value="official">Oficial, com título próprio</option>
|
|
<option value="influence">Área de influência</option>
|
|
<option value="proximity">Proximidade geográfica</option>
|
|
<option value="historical">Histórica</option>
|
|
</select>
|
|
</Field>
|
|
<Field label="Papel">
|
|
<select className={input} name="role">
|
|
<option value="related">Relacionada</option>
|
|
<option value="primary">Principal</option>
|
|
<option value="beneficiary">Beneficiária</option>
|
|
<option value="served">Servida</option>
|
|
<option value="enabled">Habilitada</option>
|
|
<option value="dependent">Dependente</option>
|
|
<option value="potential">Potencial</option>
|
|
<option value="historical">Histórica</option>
|
|
</select>
|
|
</Field>
|
|
<Field label="Fonte jurídica, obrigatória para associação oficial">
|
|
<select name="legal_source_id" className={input}>
|
|
<option value="">Não aplicável à relação proposta</option>
|
|
{sources.map((s) => (
|
|
<option key={s.id} value={s.id}>
|
|
{s.title}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Fonte da relação">
|
|
<input
|
|
name="source_reference"
|
|
className={input}
|
|
required
|
|
minLength={10}
|
|
maxLength={2000}
|
|
/>
|
|
</Field>
|
|
<Field label="Justificação">
|
|
<textarea
|
|
name="justification"
|
|
className={input}
|
|
required
|
|
minLength={10}
|
|
maxLength={3000}
|
|
/>
|
|
</Field>
|
|
<Field label="Válida desde">
|
|
<input name="valid_from" type="date" className={input} required />
|
|
</Field>
|
|
<Field label="Válida até">
|
|
<input name="valid_until" type="date" className={input} />
|
|
</Field>
|
|
<button disabled={busy} className={button}>
|
|
Submeter associação para revisão
|
|
</button>
|
|
</form>
|
|
<section className={card}>
|
|
<h2 className="text-xl font-bold">Relações territoriais</h2>
|
|
{relations.map((r) => (
|
|
<p key={r.id}>
|
|
{r.association_type} · {r.validation_status}{" "}
|
|
<button
|
|
disabled={busy || r.validation_status === "revoked"}
|
|
className={button}
|
|
onClick={() =>
|
|
action(
|
|
`/api/v2/investment/admin/zone-relations/${r.id}/${r.validation_status === "pending" ? "validate" : "revoke"}`,
|
|
{ version_no: r.version_no },
|
|
)
|
|
}
|
|
>
|
|
{r.validation_status === "pending" ? "Validar" : "Revogar"}
|
|
</button>
|
|
</p>
|
|
))}
|
|
</section>
|
|
<form
|
|
className={card + " grid gap-4 md:grid-cols-2"}
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
const data = Object.fromEntries(new FormData(e.currentTarget));
|
|
if (typeof data.geometry !== "string") return;
|
|
const raw = data.geometry;
|
|
try {
|
|
data.geometry = JSON.parse(raw);
|
|
void action(`/api/v2/investment/admin/zones/${id}/geometries`, data);
|
|
} catch {
|
|
const field = e.currentTarget.elements.namedItem(
|
|
"geometry",
|
|
) as HTMLTextAreaElement;
|
|
field.setCustomValidity("Introduza GeoJSON válido.");
|
|
field.reportValidity();
|
|
}
|
|
}}
|
|
>
|
|
<h2 className="text-xl font-bold md:col-span-2">Geometria para revisão GIS</h2>
|
|
<p>
|
|
GeoJSON no SRID 4326, até 200 KB. Não desenhe um perímetro sem fonte autorizada.
|
|
</p>
|
|
<Field label="GeoJSON">
|
|
<textarea
|
|
name="geometry"
|
|
className={input}
|
|
required
|
|
onChange={(e) => e.currentTarget.setCustomValidity("")}
|
|
/>
|
|
</Field>
|
|
<Field label="Finalidade">
|
|
<select name="purpose" className={input}>
|
|
<option value="site">Localização</option>
|
|
<option value="influence">Área de influência</option>
|
|
<option value="alternative">Alternativa</option>
|
|
<option value="official_perimeter">Perímetro oficial</option>
|
|
</select>
|
|
</Field>
|
|
<Field label="Fonte jurídica do perímetro">
|
|
<select name="legal_source_id" className={input}>
|
|
<option value="">Por confirmar</option>
|
|
{sources.map((s) => (
|
|
<option key={s.id} value={s.id}>
|
|
{s.title}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Proveniência da geometria">
|
|
<input className={input} name="source_reference" required minLength={10} />
|
|
</Field>
|
|
<Field label="Entidade indicada para validação">
|
|
<input className={input} name="validating_entity" required />
|
|
</Field>
|
|
<Field label="Data da observação">
|
|
<input className={input} type="date" name="observed_at" required />
|
|
</Field>
|
|
<button disabled={busy} className={button}>
|
|
Submeter geometria
|
|
</button>
|
|
</form>
|
|
<section className={card}>
|
|
<h2 className="text-xl font-bold">Revisão GIS</h2>
|
|
{geometries.map((g) => (
|
|
<p key={g.id}>
|
|
{g.purpose} · {g.validation_status}{" "}
|
|
<button
|
|
className={button}
|
|
disabled={busy || g.validation_status === "revoked"}
|
|
onClick={() =>
|
|
action(
|
|
`/api/v2/investment/admin/zone-geometries/${g.id}/${g.validation_status === "pending" ? "validate" : "revoke"}`,
|
|
{ version_no: g.version_no },
|
|
)
|
|
}
|
|
>
|
|
{g.validation_status === "pending"
|
|
? "Validar e autorizar divulgação"
|
|
: "Retirar geometria"}
|
|
</button>
|
|
</p>
|
|
))}
|
|
</section>
|
|
</>
|
|
);
|
|
}
|