provincias
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-09-29 14:14:06 +02:00
parent e780e67e98
commit 67dad461d9
35 changed files with 1046 additions and 42 deletions
@@ -7,6 +7,7 @@ export type TerritoryOption = {
parent_id: string | null;
location_type: string;
deleted_at?: string | null;
source_code?: string | null;
};
type Option = TerritoryOption;
const levels = ["province", "district", "admin_post", "locality"];
@@ -16,9 +17,11 @@ const control = "w-full rounded border bg-background p-2 text-foreground";
export function TerritoryFields({
initial = [],
onChange,
endpoint = "/api/v1/investment/territory-options",
}: {
initial?: TerritoryOption[];
onChange?: (path: TerritoryOption[]) => void;
endpoint?: string;
}) {
const [path, setPath] = useState(initial);
return (
@@ -31,7 +34,7 @@ export function TerritoryFields({
index={index}
parent={path[index - 1]?.id ?? ""}
selected={path[index]}
endpoint="/api/v1/investment/territory-options"
endpoint={endpoint}
onChange={(value) => {
const next = [...path.slice(0, index), ...(value ? [value] : [])];
setPath(next);
@@ -51,12 +54,14 @@ export function TerritoryLevel({
selected,
onChange,
endpoint = "/management/territory-options",
fieldError,
}: {
index: number;
parent: string;
selected?: Option;
onChange: (value?: Option) => void;
endpoint?: string;
fieldError?: string;
}) {
const id = useId();
const [q, setQ] = useState("");
@@ -65,9 +70,13 @@ export function TerritoryLevel({
const [more, setMore] = useState(false);
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [retry, setRetry] = useState(0);
const enabled = index === 0 || Boolean(parent);
useEffect(() => {
setOptions([]);
setMore(false);
if (!enabled) return;
setLoading(true);
const controller = new AbortController();
const timer = setTimeout(async () => {
setLoading(true);
@@ -85,6 +94,7 @@ export function TerritoryLevel({
});
if (!response.ok) throw new Error("Não foi possível carregar as localizações.");
const data = await response.json();
if (controller.signal.aborted) return;
setOptions(data.data);
setMore(Boolean(data.next_page_url));
} catch (e) {
@@ -98,7 +108,7 @@ export function TerritoryLevel({
clearTimeout(timer);
controller.abort();
};
}, [index, parent, q, page, enabled, endpoint]);
}, [index, parent, q, page, enabled, endpoint, retry]);
return (
<div className="space-y-2">
<label htmlFor={id} className="font-medium">
@@ -113,10 +123,12 @@ export function TerritoryLevel({
setQ(e.target.value);
setPage(1);
}}
placeholder="Pesquisar por nome"
placeholder="Pesquisar por código ou nome"
/>
<select
id={id}
aria-invalid={Boolean(fieldError)}
aria-describedby={fieldError ? `${id}-error` : undefined}
className={control}
disabled={!enabled || loading}
value={selected?.id ?? ""}
@@ -132,11 +144,24 @@ export function TerritoryLevel({
{options.map((o) => (
<option key={o.id} value={o.id}>
{o.name}
{o.source_code ? ` (${o.source_code})` : ""}
</option>
))}
</select>
{loading && <p role="status">A carregar…</p>}
{error && <p role="alert">{error}</p>}
{error && (
<div role="alert">
{error}{" "}
<button type="button" onClick={() => setRetry(retry + 1)}>
Tentar novamente
</button>
</div>
)}
{fieldError && (
<p id={`${id}-error`} role="alert">
{fieldError}
</p>
)}
{enabled && !loading && !error && options.length === 0 && (
<p className="text-sm">Sem opções para esta pesquisa.</p>
)}
+1 -1
View File
@@ -119,7 +119,7 @@ export default function Zones({
}}
>
<h2 className="text-xl font-bold">Criar rascunho</h2>
<TerritoryFields />
<TerritoryFields endpoint="/management/territory-options" />
<Field label="Código">
<input name="code" className={input} required maxLength={80} />
</Field>
+10 -5
View File
@@ -1,5 +1,4 @@
import { Link, useForm } from "@inertiajs/react";
import InvestmentTerritory from "@/components/investment-territory";
import InvestmentCoordinates from "@/components/investment-coordinates";
import { TerritoryLevel, type TerritoryOption } from "@/components/investment-territory";
import { useState } from "react";
@@ -40,10 +39,12 @@ export default function Editor({
resource,
record,
sectors,
territoryPath = [],
}: {
resource: "sectors" | "opportunities";
record: Entry | null;
sectors: { id: number; name: string }[];
territoryPath?: TerritoryOption[];
}) {
const isSector = resource === "sectors";
const fields = isSector ? sectorFields : opportunityFields;
@@ -66,9 +67,13 @@ export default function Editor({
initial.maturity_level = String(record?.maturity_level ?? "");
initial.status = String(record?.status ?? "draft");
initial.is_featured = Boolean(record?.is_featured ?? false);
initial.territory_version = String(record?.territory_version ?? 0);
["province_id", "district_id", "admin_post_id", "locality_id"].forEach((field, index) => {
initial[field] = territoryPath[index]?.id ?? "";
});
}
const form = useForm<Values>(initial);
const [creationPath, setCreationPath] = useState<TerritoryOption[]>([]);
const [creationPath, setCreationPath] = useState<TerritoryOption[]>(territoryPath);
return (
<Management
title={`${record ? "Editar" : "Criar"} ${isSector ? "sector" : "oportunidade"}`}
@@ -87,7 +92,7 @@ export default function Editor({
}}
>
<fieldset disabled={form.processing} className="space-y-5">
{!isSector && !record && (
{!isSector && (
<div className="grid gap-4 md:grid-cols-2">
{(
[
@@ -102,7 +107,8 @@ export default function Editor({
index={index}
parent={creationPath[index - 1]?.id ?? ""}
selected={creationPath[index]}
endpoint="/api/v1/investment/territory-options"
endpoint="/management/territory-options"
fieldError={form.errors[key]}
onChange={(value) => {
const path = [
...creationPath.slice(0, index),
@@ -214,7 +220,6 @@ export default function Editor({
</div>
</fieldset>
</form>
{!isSector && record && <InvestmentTerritory kind="opportunities" id={record.id} />}
{!isSector && record && <InvestmentCoordinates id={record.id} />}
</Management>
);
+1
View File
@@ -108,6 +108,7 @@ export default function Editor({
<p>Registo interno de gestão. O estado activo não publica este conteúdo no portal.</p>
{!record && (
<TerritoryFields
endpoint="/management/territory-options"
onChange={(path) =>
form.setData((data) => ({
...data,