@@ -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>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user