fix(form): 다시 마운트한 폼의 재전송이 보내지 않은 화면 값을 저장 값으로 쓰지 않는다

저장 결과가 불명인 채 페이지를 떠났다 돌아오면 폼은 새로 마운트되어
보낸 값을 모른다. 그때 resendUnknown은 화면 값을 제출 값으로 썼다
(unknownSubmissionRef ?? values). 재전송이 성공하면 기본 resetOnSuccess가
사용자가 돌아와 입력한, 보낸 적 없는 값을 지우거나 깨끗한 값으로
표시했다. 재전송이 쓰기 전에 거절되어 결과 불명에 남으면 그 화면 값을
보낸 값으로 기억해, 다음 「변경됨으로 확인」이 같은 일을 했다. 같은
상황의 settleApplied는 이미 화면 값과 dirty를 그대로 둔다(1106/08-01).
그 settleApplied 분기를 지키던 시험은 참조 기능 시험과 함께 지워졌다.

고른 방법: 결과 불명 기록을 { values: Values | null }로 바꿔 "보낸 값
모름"을 따로 둔다. 재전송은 그 값만 쓰고, 값을 모르면 성공·적용 확인을
settleApplied의 재마운트 분기와 같은 처리(settleUnsentValues)로 끝낸다.
기록이 남아 있는 동안 새 저장은 그대로 막는다. 버린 대안: 화면 값을
쓰되 성공 뒤 값을 되돌리기(사용자가 친 값을 잃는다).

위험·한계: 다시 마운트한 폼에서 결과 불명이 확인되기 전까지는 폼도 새
저장을 막는다(전에도 재전송이 불명으로 남으면 막았다). 결과 불명 상태를
코디네이터와 폼이 따로 갖는 구조(1112/01-01)는 결정 대기다.

검증: tests/component/form-foundation.test.tsx의 새 describe 셋 중 둘이
고치기 전 코드에서 실패하고 고친 뒤 통과한다(desktop, PR 본문).

Refs: 1106/03-01, 1112/01-02, 1112/10-01
This commit is contained in:
DongHyeonka
2026-11-12 22:53:23 +09:00
parent 5250ff950e
commit 7420bafe49
2 changed files with 116 additions and 21 deletions
+43 -21
View File
@@ -111,7 +111,10 @@ export function useAppForm<
const [result, setResult] = useState<FormResultState>("idle");
const [statusFailure, setStatusFailure] = useState<AppFailure | null>(null);
const pendingRef = useRef<Promise<FormResult<Output> | null> | null>(null);
const unknownSubmissionRef = useRef<Values | null>(null);
// The save whose effect is unknown: what it sent, or `values: null` when
// an earlier mount sent it (a resend from a remounted form). Held, it keeps
// a new save from going out as a new request.
const unknownSubmissionRef = useRef<Readonly<{ values: Values | null }> | null>(null);
// 1103/08-02. A save that ends in neither a field error nor success shows
// its status above the form while the page disables the button that had
// focus, so the browser drops focus to the body. The status takes focus
@@ -231,22 +234,30 @@ export function useAppForm<
[defaultValues, resetOnSuccess],
);
/**
* The save went through, with values this form did not send: an earlier
* mount sent them. 1106/08-01. The confirmed save is shown, and focus,
* lost with the button that confirmed it, goes to the feedback area that
* shows it. The values on screen were never sent, so they and the dirty
* state stay as they are (diff review of 1106/08-01).
*/
const settleUnsentValues = useCallback(() => {
unknownSubmissionRef.current = null;
focusFeedbackRef.current = focusIsLost();
setFieldErrors({} as FieldErrors<Values>);
setFormErrors([]);
setResult("success");
}, []);
const settleApplied = useCallback(() => {
const submittedValues = unknownSubmissionRef.current;
if (!submittedValues) {
// 1106/08-01. After a remount the form no longer holds the submitted
// values. The confirmed save is shown, and focus, lost with the
// confirm button, goes to the feedback area that shows it. The values
// on screen were never sent, so they and the dirty state stay as they
// are (diff review of 1106/08-01).
focusFeedbackRef.current = focusIsLost();
setFieldErrors({} as FieldErrors<Values>);
setFormErrors([]);
setResult("success");
const submittedValues = unknownSubmissionRef.current?.values ?? null;
if (submittedValues === null) {
// After a remount the form no longer holds the submitted values.
settleUnsentValues();
return;
}
settleSuccessfulValues(submittedValues);
}, [settleSuccessfulValues]);
}, [settleSuccessfulValues, settleUnsentValues]);
const settleNotApplied = useCallback(() => {
if (!unknownSubmissionRef.current) return;
@@ -256,10 +267,19 @@ export function useAppForm<
setResult("idle");
}, []);
/**
* `submittedValues` is null when this form did not send the request the
* outcome answers (a resend after a remount): a success then keeps the
* values on screen, and a result left unknown holds no values either.
*/
const applyOutcome = useCallback(
(outcome: FormResult<Output>, submittedValues: Values) => {
(outcome: FormResult<Output>, submittedValues: Values | null) => {
const settleSaved = () =>
submittedValues === null
? settleUnsentValues()
: settleSuccessfulValues(submittedValues);
if (outcome.ok) {
settleSuccessfulValues(submittedValues);
settleSaved();
return;
}
const refusal = refusalFor(refusalMessages, outcome.error);
@@ -303,13 +323,13 @@ export function useAppForm<
setResult("reauth-required");
} else if (outcome.error.effect === "MAYBE_APPLIED") {
focusFeedbackRef.current = true;
unknownSubmissionRef.current = submittedValues;
unknownSubmissionRef.current = { values: submittedValues };
setFormErrors([]);
// 0929/08-04. Kept for its request id, not shown as a status.
setStatusFailure(outcome.error);
setResult("effect-unknown");
} else if (outcome.error.effect === "APPLIED_CONFIRMED") {
settleSuccessfulValues(submittedValues);
settleSaved();
} else if (outcome.error.kind === "CONFLICT") {
focusFeedbackRef.current = true;
unknownSubmissionRef.current = null;
@@ -341,6 +361,7 @@ export function useAppForm<
focusFirstError,
refusalMessages,
settleSuccessfulValues,
settleUnsentValues,
],
);
@@ -421,15 +442,16 @@ export function useAppForm<
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;
// After a remount the form no longer holds the submitted values, and
// the values on screen were never sent (1106/03-01 diff review FR-03).
const submittedValues = unknownSubmissionRef.current?.values ?? null;
const resend = (async () => {
setPending(true);
try {
const outcome = await send();
if (outcome && resendLeftUnknown(outcome)) {
focusFeedbackRef.current = true;
unknownSubmissionRef.current = submittedValues;
unknownSubmissionRef.current = { values: submittedValues };
setFieldErrors({} as FieldErrors<Values>);
setFormErrors(["form.resendLater"]);
setResult("effect-unknown");
@@ -446,7 +468,7 @@ export function useAppForm<
pendingRef.current = resend;
return resend;
},
[applyOutcome, values],
[applyOutcome],
);
return Object.freeze({