refactor(brief): move the action lifecycle into the machine (RD-12)
The action lifecycle (Idle | Busy | Failed) lived in an imperative store-level signal, set from ten call sites outside the reducer. The reducer could not enforce which action transitions are legal. Add `action` to `BriefState.Loaded`, driven by three new messages (ActionStarted, ActionFinished, ActionFailed) and handled in `reduce`. Replace every `actionState.set(...)` call in `brief.store.ts` with the matching `dispatch`. `BriefLoaded` resets `action` to Idle, so a fresh load clears a stale action error instead of letting it outlive the reload. `busy` and `lastError` stay as `computed`s on the store with a byte-identical public signature — they are the render seam for four components and two page templates, and the union belongs in the machine, not the components. `revealBigNummer` still sets only `Failed`, never `Busy` — an existing asymmetry, not changed here. `SaveState`, `org-template.store.ts`, and `pendingPublish` are out of scope (RD-13, RD-14). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -36,6 +36,10 @@ import { passagesForBesluit } from './besluit';
|
||||
* structurally impossible (a pasted `{{…}}` is caught by the linter as `malformed`).
|
||||
*/
|
||||
|
||||
/** The one-shot action lifecycle (submit/approve/reject/send/preview/reveal/reset),
|
||||
owned by the reducer instead of an imperative store-level signal (RD-12). */
|
||||
export type BriefActionState = { tag: 'Idle' } | { tag: 'Busy' } | { tag: 'Failed'; error: string };
|
||||
|
||||
export type BriefState =
|
||||
| { tag: 'Loading' }
|
||||
| {
|
||||
@@ -43,6 +47,7 @@ export type BriefState =
|
||||
brief: Brief;
|
||||
availablePassages: readonly LibraryPassage[];
|
||||
decisions: BriefDecisions;
|
||||
action: BriefActionState;
|
||||
}
|
||||
| { tag: 'Failed'; reason: string };
|
||||
|
||||
@@ -65,7 +70,10 @@ export type BriefMsg =
|
||||
| { tag: 'Approved'; by: string; at: string; decisions: BriefDecisions } // submitted → approved
|
||||
| { tag: 'Rejected'; by: string; at: string; comments: string; decisions: BriefDecisions } // submitted → rejected
|
||||
| { tag: 'Sent'; at: string; decisions: BriefDecisions } // approved → sent
|
||||
| { tag: 'Seed'; state: BriefState };
|
||||
| { tag: 'Seed'; state: BriefState }
|
||||
| { tag: 'ActionStarted' } // a one-shot action (submit/approve/preview/…) began
|
||||
| { tag: 'ActionFinished' } // it completed successfully
|
||||
| { tag: 'ActionFailed'; error: string }; // it failed, carrying the message to show
|
||||
|
||||
/** Edits are allowed only in these statuses; editing a rejected letter reopens it. */
|
||||
function isEditable(status: BriefStatus): boolean {
|
||||
@@ -193,6 +201,9 @@ export function reduce(s: BriefState, m: BriefMsg): BriefState {
|
||||
brief: m.brief,
|
||||
availablePassages: m.availablePassages,
|
||||
decisions: m.decisions,
|
||||
// A fresh load clears a stale action error rather than letting it outlive
|
||||
// the reload (RD-12, decision 4).
|
||||
action: { tag: 'Idle' },
|
||||
};
|
||||
case 'BriefLoadFailed':
|
||||
return { tag: 'Failed', reason: m.reason };
|
||||
@@ -260,6 +271,15 @@ export function reduce(s: BriefState, m: BriefMsg): BriefState {
|
||||
case 'Sent':
|
||||
return transition(s, 'approved', () => ({ tag: 'sent', sentAt: m.at }), m.decisions);
|
||||
|
||||
// The action lifecycle (RD-12): a no-op unless a brief is loaded, since there is
|
||||
// nothing to attach the action state to otherwise.
|
||||
case 'ActionStarted':
|
||||
return s.tag === 'Loaded' ? { ...s, action: { tag: 'Busy' } } : s;
|
||||
case 'ActionFinished':
|
||||
return s.tag === 'Loaded' ? { ...s, action: { tag: 'Idle' } } : s;
|
||||
case 'ActionFailed':
|
||||
return s.tag === 'Loaded' ? { ...s, action: { tag: 'Failed', error: m.error } } : s;
|
||||
|
||||
default:
|
||||
return assertNever(m);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user