fix: recover saved investment context without resubmitting confirmation
This commit is contained in:
@@ -180,7 +180,10 @@ export default function ContextEditor({
|
||||
draft?.replace_fields ?? [],
|
||||
);
|
||||
const [plan, setPlan] = useState<ContextPlan | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [requestBusy, setBusy] = useState(false);
|
||||
const [needsRecovery, setNeedsRecovery] = useState(false);
|
||||
const awaitingSavedContext = useRef(false);
|
||||
const busy = requestBusy || needsRecovery;
|
||||
const [error, setError] = useState('');
|
||||
const [message, setMessage] = useState('');
|
||||
const [corridors, setCorridors] = useState<CorridorOption[]>([]);
|
||||
@@ -313,7 +316,11 @@ export default function ContextEditor({
|
||||
useEffect(() => {
|
||||
const flush = () => {
|
||||
const body = latestDraft.current;
|
||||
if (body !== confirmedDraftBody.current && body.length < 60000)
|
||||
if (
|
||||
!awaitingSavedContext.current &&
|
||||
body !== confirmedDraftBody.current &&
|
||||
body.length < 60000
|
||||
)
|
||||
void contextApi(
|
||||
'/investimentos/' + initial.case.id + '/contexto/rascunho',
|
||||
{
|
||||
@@ -423,8 +430,40 @@ export default function ContextEditor({
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
async function recoverSavedContext() {
|
||||
setBusy(true);
|
||||
setError('');
|
||||
try {
|
||||
const next = await contextApi<ContextView>(
|
||||
`/investimentos/${view.case.id}/contexto`,
|
||||
);
|
||||
skipNextDraft.current = true;
|
||||
setView(next);
|
||||
setChoices(next.context.opportunity_choices ?? []);
|
||||
setComplements(next.context.complements ?? []);
|
||||
setPreparation(next.private_investment);
|
||||
navigation.refresh();
|
||||
setChanges({});
|
||||
setDeclarations({});
|
||||
setDocumentLinks({});
|
||||
setReplace([]);
|
||||
setPlan(null);
|
||||
awaitingSavedContext.current = false;
|
||||
setNeedsRecovery(false);
|
||||
setOperation(createOperationKey());
|
||||
setMessage(
|
||||
'Dados guardados. Pode continuar a preparação do investimento.',
|
||||
);
|
||||
} catch {
|
||||
setError(
|
||||
'Os dados foram guardados, mas não foi possível recuperar a versão actual. Recupere os dados guardados para continuar.',
|
||||
);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
async function confirm() {
|
||||
if (!plan || !requestBody) return;
|
||||
if (!plan || !requestBody || busy) return;
|
||||
setBusy(true);
|
||||
await queue.current.catch(() => {});
|
||||
setError('');
|
||||
@@ -438,30 +477,17 @@ export default function ContextEditor({
|
||||
confirm: true,
|
||||
},
|
||||
);
|
||||
const next = await contextApi<ContextView>(
|
||||
`/investimentos/${view.case.id}/contexto`,
|
||||
);
|
||||
skipNextDraft.current = true;
|
||||
// The entry has been consumed: refreshing must recover saved choices, not reapply a removed association.
|
||||
awaitingSavedContext.current = true;
|
||||
setNeedsRecovery(true);
|
||||
setPlan(null);
|
||||
setRequestBody(null);
|
||||
// Consume entry parameters as soon as saving succeeds, even if recovery fails.
|
||||
window.history.replaceState(
|
||||
window.history.state,
|
||||
'',
|
||||
`/investimentos/${view.case.id}/contexto/editar${window.location.hash}`,
|
||||
);
|
||||
setView(next);
|
||||
setChoices(next.context.opportunity_choices ?? []);
|
||||
setComplements(next.context.complements ?? []);
|
||||
setPreparation(next.private_investment);
|
||||
navigation.refresh();
|
||||
setChanges({});
|
||||
setDeclarations({});
|
||||
setDocumentLinks({});
|
||||
setReplace([]);
|
||||
setPlan(null);
|
||||
setOperation(createOperationKey());
|
||||
setMessage(
|
||||
'Dados guardados. Pode continuar a preparação do investimento.',
|
||||
);
|
||||
await recoverSavedContext();
|
||||
} catch (e) {
|
||||
setError(
|
||||
e instanceof Error
|
||||
@@ -560,9 +586,16 @@ export default function ContextEditor({
|
||||
<button
|
||||
type="button"
|
||||
className="underline"
|
||||
onClick={() => window.location.reload()}
|
||||
disabled={requestBusy}
|
||||
onClick={() =>
|
||||
needsRecovery
|
||||
? void recoverSavedContext()
|
||||
: window.location.reload()
|
||||
}
|
||||
>
|
||||
Recuperar o dossier atual
|
||||
{needsRecovery
|
||||
? 'Recuperar dados guardados'
|
||||
: 'Recuperar o dossier atual'}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user