@@ -0,0 +1,184 @@
|
||||
import { router } from "@inertiajs/react";
|
||||
import { useState } from "react";
|
||||
import { Management, Field, control, button } from "./shared";
|
||||
|
||||
type Plan = {
|
||||
level: number;
|
||||
expected: number;
|
||||
resolved: number;
|
||||
pending: number;
|
||||
hash: string;
|
||||
scan_ready: boolean;
|
||||
source_sha256: string;
|
||||
rows: {
|
||||
external_id: string;
|
||||
name: string;
|
||||
parent_name: string | null;
|
||||
location_id: string | null;
|
||||
status: string;
|
||||
}[];
|
||||
};
|
||||
export default function GeometryImport({ plan }: { plan: Plan }) {
|
||||
const [reason, setReason] = useState("");
|
||||
const [confirmed, setConfirmed] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [message, setMessage] = useState("");
|
||||
const [batch, setBatch] = useState("");
|
||||
const [applyKey, setApplyKey] = useState(() => crypto.randomUUID());
|
||||
const [reverseKey, setReverseKey] = useState(() => crypto.randomUUID());
|
||||
const execute = async (reverse: boolean) => {
|
||||
setBusy(true);
|
||||
try {
|
||||
const cookie = document.cookie
|
||||
.split("; ")
|
||||
.find((v) => v.startsWith("XSRF-TOKEN="))
|
||||
?.slice(11);
|
||||
const response = await fetch(
|
||||
`/management/territory-geometry-import${reverse ? `/${batch}/reverse` : ""}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"X-XSRF-TOKEN": decodeURIComponent(cookie ?? ""),
|
||||
},
|
||||
body: JSON.stringify({
|
||||
level: plan.level,
|
||||
hash: plan.hash,
|
||||
reason,
|
||||
confirm: confirmed,
|
||||
idempotency_key: reverse ? reverseKey : applyKey,
|
||||
}),
|
||||
},
|
||||
);
|
||||
const result = await response.json();
|
||||
if (!response.ok)
|
||||
throw new Error(
|
||||
result.message ??
|
||||
"Operação recusada. Reveja o plano e a autenticação.",
|
||||
);
|
||||
if (!reverse) setBatch(result.batch_id);
|
||||
setMessage(
|
||||
reverse
|
||||
? `Revertidas ${result.reversed}; preservadas ${result.reused_preserved} geometrias reutilizadas.`
|
||||
: `Criadas ${result.inserted}; reutilizadas ${result.reused}; pendentes ${result.pending}. Lote ${result.batch_id}. Nenhuma camada publicada.`,
|
||||
);
|
||||
} catch (e) {
|
||||
setMessage(e instanceof Error ? e.message : "Erro de ligação.");
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<Management title="Importação cartográfica">
|
||||
<p>
|
||||
Exemplar INE de 07/06/2019 aceite. Correspondência exata de nome
|
||||
e pai; sem alterações de classificações territoriais,
|
||||
disponibilidade fundiária ou cálculos.
|
||||
</p>
|
||||
<Field label="Nível">
|
||||
<select
|
||||
className={control}
|
||||
value={plan.level}
|
||||
onChange={(e) => {
|
||||
setBatch("");
|
||||
setMessage("");
|
||||
setConfirmed(false);
|
||||
setApplyKey(crypto.randomUUID());
|
||||
setReverseKey(crypto.randomUUID());
|
||||
router.get("/management/territory-geometry-import", {
|
||||
level: Number(e.target.value),
|
||||
});
|
||||
}}
|
||||
>
|
||||
<option value={1}>Províncias</option>
|
||||
<option value={2}>Distritos</option>
|
||||
<option value={3}>Postos administrativos</option>
|
||||
</select>
|
||||
</Field>
|
||||
<p>
|
||||
Esperado: {plan.expected}; resolvido: {plan.resolved}; pendente:{" "}
|
||||
{plan.pending}. Scan:{" "}
|
||||
{plan.scan_ready ? "limpo e atual" : "pendente ou inconclusivo"}
|
||||
.
|
||||
</p>
|
||||
<p className="break-all">
|
||||
SHA-256 do original: {plan.source_sha256}
|
||||
</p>
|
||||
<div className="overflow-x-auto">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Identificador</th>
|
||||
<th>Nome</th>
|
||||
<th>Pai</th>
|
||||
<th>Correspondência</th>
|
||||
<th>Ação prevista</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{plan.rows.map((r) => (
|
||||
<tr key={r.external_id}>
|
||||
<td>{r.external_id}</td>
|
||||
<td>{r.name}</td>
|
||||
<td>{r.parent_name ?? "—"}</td>
|
||||
<td>{r.location_id ?? "Pendente"}</td>
|
||||
<td>{r.status}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<Field label="Fundamento da aplicação ou reversão">
|
||||
<textarea
|
||||
className={control}
|
||||
value={reason}
|
||||
onChange={(e) => setReason(e.target.value)}
|
||||
minLength={20}
|
||||
maxLength={1000}
|
||||
/>
|
||||
</Field>
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={confirmed}
|
||||
onChange={(e) => setConfirmed(e.target.checked)}
|
||||
/>{" "}
|
||||
Confirmei as correspondências. A aplicação mantém geometrias
|
||||
privadas e publicação separada.
|
||||
</label>
|
||||
<button
|
||||
className={button}
|
||||
disabled={
|
||||
busy || !confirmed || reason.length < 20 || !plan.scan_ready
|
||||
}
|
||||
onClick={() => execute(false)}
|
||||
>
|
||||
Aplicar correspondências
|
||||
</button>
|
||||
<Field label="Lote a reverter">
|
||||
<input
|
||||
className={control}
|
||||
value={batch}
|
||||
onChange={(e) => {
|
||||
setBatch(e.target.value);
|
||||
setReverseKey(crypto.randomUUID());
|
||||
}}
|
||||
/>
|
||||
</Field>
|
||||
<button
|
||||
className={button}
|
||||
disabled={busy || !confirmed || reason.length < 20 || !batch}
|
||||
onClick={() => execute(true)}
|
||||
>
|
||||
Reverter apenas geometrias criadas pelo lote
|
||||
</button>
|
||||
<p role="status">{message}</p>
|
||||
<p>
|
||||
A reversão preserva originais, histórico e geometrias
|
||||
reutilizadas; recusa alterações ou relações posteriores.
|
||||
Publicação não é realizada nesta página.
|
||||
</p>
|
||||
</Management>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user