fix(query): resend an unknown-effect write with its original idempotency key (03-01, 08-03)
A save whose response was lost minted a new key on the next submit, and the screen let the user declare "not applied" without evidence. The same canonical input now reuses the unknown admission's intent; the screen offers "send the same request again" and a list refresh link instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
926f448fce
commit
dfb1b9e0ee
@@ -175,6 +175,44 @@ export function useAppForm<
|
||||
setResult("idle");
|
||||
}, []);
|
||||
|
||||
const applyOutcome = useCallback(
|
||||
(outcome: FormResult<Output>, submittedValues: Values) => {
|
||||
if (outcome.ok) {
|
||||
settleSuccessfulValues(submittedValues);
|
||||
return;
|
||||
}
|
||||
if (outcome.error.kind === "VALIDATION_REJECTED") {
|
||||
unknownSubmissionRef.current = null;
|
||||
const mapped = mapValidationFailureToFields<Values>(
|
||||
outcome.error,
|
||||
allowedServerFields,
|
||||
message,
|
||||
);
|
||||
setFieldErrors(mapped.fieldErrors);
|
||||
setFormErrors(mapped.formErrors);
|
||||
setResult("validation-error");
|
||||
focusFirstError(mapped.fieldErrors);
|
||||
} else if (outcome.error.effect === "MAYBE_APPLIED") {
|
||||
unknownSubmissionRef.current = submittedValues;
|
||||
setFormErrors([]);
|
||||
setResult("effect-unknown");
|
||||
} else if (outcome.error.effect === "APPLIED_CONFIRMED") {
|
||||
settleSuccessfulValues(submittedValues);
|
||||
} else if (outcome.error.kind === "CONFLICT") {
|
||||
unknownSubmissionRef.current = null;
|
||||
setFormErrors([
|
||||
message("form.conflict"),
|
||||
]);
|
||||
setResult("conflict");
|
||||
} else {
|
||||
unknownSubmissionRef.current = null;
|
||||
setFormErrors([message("form.unavailable")]);
|
||||
setResult("unavailable");
|
||||
}
|
||||
},
|
||||
[allowedServerFields, focusFirstError, message, settleSuccessfulValues],
|
||||
);
|
||||
|
||||
const validateAndSubmit = useCallback(
|
||||
async (): Promise<FormResult<Output> | null> => {
|
||||
setFieldErrors({} as FieldErrors<Values>);
|
||||
@@ -205,38 +243,7 @@ export function useAppForm<
|
||||
setPending(true);
|
||||
try {
|
||||
const outcome = await submit(command);
|
||||
if (outcome.ok) {
|
||||
settleSuccessfulValues(parsed.data);
|
||||
return outcome;
|
||||
}
|
||||
if (outcome.error.kind === "VALIDATION_REJECTED") {
|
||||
unknownSubmissionRef.current = null;
|
||||
const mapped = mapValidationFailureToFields<Values>(
|
||||
outcome.error,
|
||||
allowedServerFields,
|
||||
message,
|
||||
);
|
||||
setFieldErrors(mapped.fieldErrors);
|
||||
setFormErrors(mapped.formErrors);
|
||||
setResult("validation-error");
|
||||
focusFirstError(mapped.fieldErrors);
|
||||
} else if (outcome.error.effect === "MAYBE_APPLIED") {
|
||||
unknownSubmissionRef.current = parsed.data;
|
||||
setFormErrors([]);
|
||||
setResult("effect-unknown");
|
||||
} else if (outcome.error.effect === "APPLIED_CONFIRMED") {
|
||||
settleSuccessfulValues(parsed.data);
|
||||
} else if (outcome.error.kind === "CONFLICT") {
|
||||
unknownSubmissionRef.current = null;
|
||||
setFormErrors([
|
||||
message("form.conflict"),
|
||||
]);
|
||||
setResult("conflict");
|
||||
} else {
|
||||
unknownSubmissionRef.current = null;
|
||||
setFormErrors([message("form.unavailable")]);
|
||||
setResult("unavailable");
|
||||
}
|
||||
applyOutcome(outcome, parsed.data);
|
||||
return outcome;
|
||||
} finally {
|
||||
setPending(false);
|
||||
@@ -244,11 +251,11 @@ export function useAppForm<
|
||||
},
|
||||
[
|
||||
allowedServerFields,
|
||||
applyOutcome,
|
||||
focusFirstError,
|
||||
mapToCommand,
|
||||
message,
|
||||
schema,
|
||||
settleSuccessfulValues,
|
||||
submit,
|
||||
values,
|
||||
],
|
||||
@@ -270,6 +277,37 @@ export function useAppForm<
|
||||
[validateAndSubmit],
|
||||
);
|
||||
|
||||
/**
|
||||
* D-1. Runs a resend of the unknown submission (for example
|
||||
* `mutation.resendUnknownEffect`) through the same outcome handling as a
|
||||
* submit. The form never builds a new command here: the caller resends the
|
||||
* original request with its original key.
|
||||
*/
|
||||
const resendUnknown = useCallback(
|
||||
(
|
||||
send: () => Promise<FormResult<Output> | null>,
|
||||
): Promise<FormResult<Output> | null> => {
|
||||
if (pendingRef.current) return pendingRef.current;
|
||||
// After a remount the form no longer holds the submitted values.
|
||||
const submittedValues = unknownSubmissionRef.current ?? values;
|
||||
const resend = (async () => {
|
||||
setPending(true);
|
||||
try {
|
||||
const outcome = await send();
|
||||
if (outcome) applyOutcome(outcome, submittedValues);
|
||||
return outcome;
|
||||
} finally {
|
||||
setPending(false);
|
||||
}
|
||||
})().finally(() => {
|
||||
pendingRef.current = null;
|
||||
});
|
||||
pendingRef.current = resend;
|
||||
return resend;
|
||||
},
|
||||
[applyOutcome, values],
|
||||
);
|
||||
|
||||
return Object.freeze({
|
||||
formId,
|
||||
values,
|
||||
@@ -284,6 +322,7 @@ export function useAppForm<
|
||||
focusField,
|
||||
setValue,
|
||||
submitForm,
|
||||
resendUnknown,
|
||||
reset,
|
||||
settleApplied,
|
||||
settleNotApplied,
|
||||
|
||||
Reference in New Issue
Block a user