Sub topic 9
Reviewing and submitting
This chapter assumes you have read the earlier chapters on flow state, navigation, and per-step validation. It uses the Step type and the Data record from those chapters. Here you will add the last screen of a flow, the review step, and make the final submission safe.
What the review step is for
A review step shows every answer the user gave and lets them fix mistakes before anything is sent. It has three jobs:
- Show the collected data in a readable form, grouped by step.
- Offer an "Edit" action next to each group that jumps to that step.
- Offer one clear "Submit" action.
The review step is also where you catch problems the per-step checks missed. A user can go back, change an early answer, and invalidate a later step without noticing. Treat the review step as the last checkpoint, not as a formality.
Review only the active path
Branching creates a trap. Suppose a user picks "business", enters a company name, goes back, and switches to "personal". The company value is still in the data, but the company step is no longer part of their path. If you submit all the data, you send an answer the user no longer stands behind.
The fix is to compute the active path by following each step's next function from the first step. Review and submit only the steps on that path.
type Data = Record<string, string>;
type Step = {
id: string;
validate(data: Data): string[];
next(data: Data): string | null;
};
function activePath(
steps: Record<string, Step>,
start: string,
data: Data
): string[] {
const path: string[] = [];
let id: string | null = start;
while (id !== null) {
if (path.includes(id)) throw new Error("Cycle in flow at step " + id);
const step: Step | undefined = steps[id];
if (!step) throw new Error("Unknown step " + id);
path.push(id);
id = step.next(data);
}
return path;
}The cycle check turns a misconfigured flow into an immediate error instead of an endless loop.
Validating everything before sending
Per-step validation only ran when the user pressed Next. Before submitting, run it again for every step on the active path. If one fails, send the user to that step with its errors showing.
This example assumes a fieldsByStep map that lists which data keys each step owns. It lets you build a payload that contains only the answers from the active path.
type Review =
| { ok: true; payload: Data }
| { ok: false; firstInvalid: string; errors: string[] };
function prepareSubmission(
steps: Record<string, Step>,
fieldsByStep: Record<string, string[]>,
start: string,
data: Data
): Review {
const path = activePath(steps, start, data);
for (const id of path) {
const errors = steps[id].validate(data);
if (errors.length > 0) return { ok: false, firstInvalid: id, errors };
}
const payload: Data = {};
for (const id of path) {
for (const field of fieldsByStep[id] ?? []) {
if (field in data) payload[field] = data[field];
}
}
return { ok: true, payload };
}When ok is false, set current to firstInvalid and show its errors. The user lands on the problem instead of seeing a generic failure message.
Submitting safely
Submission is the one action with side effects outside the flow, so it needs more care than navigation does. Three problems are common:
- Double submission. A user double-clicks, or presses Enter twice. Ignore new attempts while one is in flight.
- Retries that duplicate work. If a request times out, the user will retry, and the server may have already processed the first one. Send an idempotency key, a unique ID that lets the server recognize repeats. Reuse the same key for every retry of one flow.
- Lost input on failure. Never clear the flow state until the server confirms success.
function createSubmitter(
send: (payload: Data, key: string) => Promise<void>
) {
let inFlight = false;
const key = crypto.randomUUID();
return async function submit(
payload: Data
): Promise<"done" | "failed" | "busy"> {
if (inFlight) return "busy";
inFlight = true;
try {
await send(payload, key);
return "done";
} catch {
return "failed";
} finally {
inFlight = false;
}
};
}Create one submitter per flow session, not per click, so the key stays stable across retries.
Handling failure and success
On failure, stay on the review step, keep every answer, and say what happened in plain words, such as "We could not reach the server. Your answers are saved. Try again." If the server rejects a specific field, map that error back to the step that owns the field and offer a link to it.
On success, replace the review screen with a confirmation. Show a reference number if the server returns one. Then clear the flow state and any saved draft, and make Back not return to the review step.
Exercise
A user chooses "business", fills in a company name, then goes back and changes the account type to "personal". Why can the company name still end up in the submitted payload, and how does this chapter prevent it?
Show answer
Changing the answer does not delete the old company value, so it stays in the flow data. Submitting all the data would send it. The chapter prevents this by computing the active path with activePath, which follows each step's next function, and building the payload only from the fields of steps on that path. The company step is no longer reached, so its field is left out.