Conservar progresso editorial entre renovações MFA e tornar a retoma visível
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Link } from "@inertiajs/react";
|
||||
import { useRef, useState } from "react";
|
||||
import { Link, router } from "@inertiajs/react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import {
|
||||
financialCategories,
|
||||
financialTypes,
|
||||
@@ -27,21 +27,82 @@ type Receipt = {
|
||||
published?: boolean;
|
||||
};
|
||||
|
||||
export default function Assisted({
|
||||
type Progress = {
|
||||
selected: string[];
|
||||
receipts: Record<string, Receipt>;
|
||||
reason: string;
|
||||
pending: Record<string, object>;
|
||||
};
|
||||
const emptyProgress = (): Progress => ({
|
||||
selected: [],
|
||||
receipts: {},
|
||||
reason: "",
|
||||
pending: {},
|
||||
});
|
||||
export function readProgress(key: string): Progress {
|
||||
try {
|
||||
const value = JSON.parse(sessionStorage.getItem(key) ?? "null");
|
||||
if (
|
||||
!value ||
|
||||
!Array.isArray(value.selected) ||
|
||||
!value.selected.every((c: unknown) => typeof c === "string") ||
|
||||
!value.receipts ||
|
||||
typeof value.receipts !== "object" ||
|
||||
typeof value.reason !== "string" ||
|
||||
!value.pending ||
|
||||
typeof value.pending !== "object"
|
||||
)
|
||||
return emptyProgress();
|
||||
return value;
|
||||
} catch {
|
||||
return emptyProgress();
|
||||
}
|
||||
}
|
||||
function saveProgress(key: string, value: Progress): void {
|
||||
try {
|
||||
sessionStorage.setItem(key, JSON.stringify(value));
|
||||
} catch {
|
||||
/* Native receipts remain authoritative if tab storage is unavailable. */
|
||||
}
|
||||
}
|
||||
|
||||
export function AssistedEditor({
|
||||
batch,
|
||||
}: {
|
||||
batch: { dataset_hash: string; items: Item[] };
|
||||
batch: { dataset_hash: string; editor_id: number; items: Item[] };
|
||||
}) {
|
||||
const [selected, setSelected] = useState<string[]>([]);
|
||||
const [receipts, setReceipts] = useState<Record<string, Receipt>>({});
|
||||
const storageKey = `investmoz-funding-progress:${batch.editor_id}:${batch.dataset_hash}`;
|
||||
const [restored] = useState(() => readProgress(storageKey));
|
||||
const [selected, setSelected] = useState<string[]>(
|
||||
restored.selected.filter((c) => batch.items.some((i) => i.code === c)),
|
||||
);
|
||||
const [receipts, setReceipts] = useState<Record<string, Receipt>>(
|
||||
restored.receipts,
|
||||
);
|
||||
const [reviewed, setReviewed] = useState(false);
|
||||
const [confirmed, setConfirmed] = useState(false);
|
||||
const [reason, setReason] = useState("");
|
||||
const [reason, setReason] = useState(restored.reason);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [message, setMessage] = useState("");
|
||||
const running = useRef(false);
|
||||
const stop = useRef(false);
|
||||
const pending = useRef<Record<string, object>>({});
|
||||
const pending = useRef<Record<string, object>>(restored.pending);
|
||||
const [needsMfa, setNeedsMfa] = useState(false);
|
||||
useEffect(() => {
|
||||
saveProgress(storageKey, {
|
||||
selected,
|
||||
receipts,
|
||||
reason,
|
||||
pending: pending.current,
|
||||
});
|
||||
}, [storageKey, selected, receipts, reason]);
|
||||
const ready = selected.filter(
|
||||
(c) =>
|
||||
receipts[c]?.matches_reference &&
|
||||
receipts[c]?.publication &&
|
||||
!receipts[c]?.published,
|
||||
).length;
|
||||
const published = selected.filter((c) => receipts[c]?.published).length;
|
||||
async function run(publish: boolean) {
|
||||
if (
|
||||
running.current ||
|
||||
@@ -57,6 +118,7 @@ export default function Assisted({
|
||||
try {
|
||||
for (const code of selected) {
|
||||
if (stop.current) break;
|
||||
if (!publish && receipts[code]) continue;
|
||||
setMessage(
|
||||
`${publish ? "Publicação" : "Preparação"}: ${batch.items.find((i) => i.code === code)?.title}`,
|
||||
);
|
||||
@@ -74,6 +136,12 @@ export default function Assisted({
|
||||
confirm: true,
|
||||
};
|
||||
pending.current[code] = body;
|
||||
saveProgress(storageKey, {
|
||||
selected,
|
||||
receipts,
|
||||
reason,
|
||||
pending: pending.current,
|
||||
});
|
||||
await profileJson(
|
||||
`/management/fundos/${receipt.id}/publicar`,
|
||||
body,
|
||||
@@ -108,6 +176,14 @@ export default function Assisted({
|
||||
: "Preparação concluída. Confira os resultados antes de confirmar a publicação.",
|
||||
);
|
||||
} catch (error) {
|
||||
setNeedsMfa(
|
||||
!!error &&
|
||||
typeof error === "object" &&
|
||||
"status" in error &&
|
||||
(error.status === 401 ||
|
||||
error.status === 403 ||
|
||||
error.status === 419),
|
||||
);
|
||||
setMessage(
|
||||
`${error instanceof Error ? error.message : "Operação interrompida."} Os resultados anteriores foram conservados. Se a sessão ou MFA expirou, autentique-se no portal e retome. A publicação incerta pode ser repetida com a mesma confirmação.`,
|
||||
);
|
||||
@@ -145,71 +221,94 @@ export default function Assisted({
|
||||
setConfirmed(false);
|
||||
}}
|
||||
>
|
||||
Seleccionar instituições do documento
|
||||
Seleccionar todas as instituições (270)
|
||||
</button>
|
||||
<p>{selected.length} seleccionadas</p>
|
||||
{batch.items.map((item) => (
|
||||
<details key={item.code} className="border-b py-2">
|
||||
<summary>
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.includes(item.code)}
|
||||
onChange={(e) => {
|
||||
setSelected((old) =>
|
||||
e.target.checked
|
||||
? [...old, item.code]
|
||||
: old.filter(
|
||||
(c) => c !== item.code,
|
||||
),
|
||||
);
|
||||
setConfirmed(false);
|
||||
}}
|
||||
/>{" "}
|
||||
{item.title}
|
||||
</label>{" "}
|
||||
—{" "}
|
||||
{receipts[item.code]?.published
|
||||
? "Publicação confirmada"
|
||||
: receipts[item.code]
|
||||
? receipts[item.code].matches_reference
|
||||
? "Conferida, aguarda publicação"
|
||||
: "Edições existentes: rever individualmente"
|
||||
: item.published
|
||||
? "Já tem publicação"
|
||||
: "Por preparar"}
|
||||
</summary>
|
||||
<p>{item.summary}</p>
|
||||
<p>
|
||||
{item.categories.map(c => financialCategories[c] ?? c).join(", ")} —{" "}
|
||||
{item.types.map(t => financialTypes[t] ?? t).join(", ")} — {item.status}
|
||||
</p>
|
||||
<p>
|
||||
{item.source_document}{" "}
|
||||
<code className="break-all">
|
||||
{item.source_sha256}
|
||||
</code>
|
||||
</p>
|
||||
<p>
|
||||
{item.locations.length} localizações;{" "}
|
||||
{item.contacts.length} contactos.
|
||||
</p>
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/management/fundos/referencias/${item.code}`}
|
||||
>
|
||||
Conferir fonte e dados completos
|
||||
</Link>
|
||||
{receipts[item.code] && (
|
||||
<p role="status">
|
||||
{selected.length} seleccionadas ·{" "}
|
||||
{Object.keys(receipts).length} preparadas nesta retoma ·{" "}
|
||||
{ready} prontas para publicar · {published} publicadas
|
||||
</p>
|
||||
<p>
|
||||
O progresso fica guardado nesta aba, separado por conta e
|
||||
fontes. Depois de renovar o MFA, retome os passos sem voltar
|
||||
a preparar as fichas conferidas. As confirmações devem ser
|
||||
marcadas novamente; nenhuma publicação é automática.
|
||||
</p>
|
||||
<details>
|
||||
<summary>
|
||||
Conferir a lista e alterar a selecção (
|
||||
{batch.items.length} referências)
|
||||
</summary>
|
||||
{batch.items.map((item) => (
|
||||
<details key={item.code} className="border-b py-2">
|
||||
<summary>
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.includes(item.code)}
|
||||
onChange={(e) => {
|
||||
setSelected((old) =>
|
||||
e.target.checked
|
||||
? [...old, item.code]
|
||||
: old.filter(
|
||||
(c) =>
|
||||
c !== item.code,
|
||||
),
|
||||
);
|
||||
setConfirmed(false);
|
||||
}}
|
||||
/>{" "}
|
||||
{item.title}
|
||||
</label>{" "}
|
||||
—{" "}
|
||||
{receipts[item.code]?.published
|
||||
? "Publicação confirmada"
|
||||
: receipts[item.code]
|
||||
? receipts[item.code].matches_reference
|
||||
? "Conferida, aguarda publicação"
|
||||
: "Edições existentes: rever individualmente"
|
||||
: item.published
|
||||
? "Já tem publicação"
|
||||
: "Por preparar"}
|
||||
</summary>
|
||||
<p>{item.summary}</p>
|
||||
<p>
|
||||
{item.categories
|
||||
.map((c) => financialCategories[c] ?? c)
|
||||
.join(", ")}{" "}
|
||||
—{" "}
|
||||
{item.types
|
||||
.map((t) => financialTypes[t] ?? t)
|
||||
.join(", ")}{" "}
|
||||
— {item.status}
|
||||
</p>
|
||||
<p>
|
||||
{item.source_document}{" "}
|
||||
<code className="break-all">
|
||||
{item.source_sha256}
|
||||
</code>
|
||||
</p>
|
||||
<p>
|
||||
{item.locations.length} localizações;{" "}
|
||||
{item.contacts.length} contactos.
|
||||
</p>
|
||||
<Link
|
||||
className="ml-4 underline"
|
||||
href={`/management/fundos/${receipts[item.code].id}`}
|
||||
className="underline"
|
||||
href={`/management/fundos/referencias/${item.code}`}
|
||||
>
|
||||
Alterar ficha existente
|
||||
Conferir fonte e dados completos
|
||||
</Link>
|
||||
)}
|
||||
</details>
|
||||
))}
|
||||
{receipts[item.code] && (
|
||||
<Link
|
||||
className="ml-4 underline"
|
||||
href={`/management/fundos/${receipts[item.code].id}`}
|
||||
>
|
||||
Alterar ficha existente
|
||||
</Link>
|
||||
)}
|
||||
</details>
|
||||
))}
|
||||
</details>
|
||||
<label className="block py-3">
|
||||
<input
|
||||
type="checkbox"
|
||||
@@ -245,6 +344,11 @@ export default function Assisted({
|
||||
Confirmo a publicação pública das versões conferidas,
|
||||
preservando os dados e as incertezas apresentados.
|
||||
</label>
|
||||
<p>
|
||||
Para publicar: termine a preparação, marque a conferência,
|
||||
preencha o fundamento e confirme a publicação. Fichas com
|
||||
edições existentes exigem revisão individual.
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
className={button}
|
||||
@@ -263,6 +367,33 @@ export default function Assisted({
|
||||
2. Publicar conferidas
|
||||
</button>
|
||||
</fieldset>
|
||||
{needsMfa && (
|
||||
<div role="alert" className={card}>
|
||||
<p>
|
||||
A sessão ou confirmação MFA precisa de renovação. O
|
||||
progresso desta aba foi conservado.
|
||||
</p>
|
||||
<button
|
||||
className={button}
|
||||
type="button"
|
||||
disabled={busy}
|
||||
onClick={() =>
|
||||
router.post(
|
||||
"/logout",
|
||||
{},
|
||||
{
|
||||
onSuccess: () =>
|
||||
router.visit(
|
||||
"/management/fundos/referencias-assistidas",
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
>
|
||||
Renovar entrada com MFA e retomar
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{busy && (
|
||||
<button
|
||||
type="button"
|
||||
@@ -283,3 +414,16 @@ export default function Assisted({
|
||||
</Frame>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Assisted({
|
||||
batch,
|
||||
}: {
|
||||
batch: { dataset_hash: string; editor_id: number; items: Item[] };
|
||||
}) {
|
||||
return (
|
||||
<AssistedEditor
|
||||
key={`${batch.editor_id}:${batch.dataset_hash}`}
|
||||
batch={batch}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user