Line data Source code
1 : import type { PlanRowState } from './planRow'
2 :
3 : export type PlanLifecycleIntent =
4 : | {
5 : kind: 'stay'
6 : planName: string
7 : targetPlanName: string
8 : effectiveAt: string
9 : }
10 : | {
11 : kind: 'resume'
12 : planName: string
13 : renewsAt: string
14 : }
15 :
16 : export type PlanCommandOutcome =
17 : | { ok: true }
18 : | { ok: false; serverMessage: string | null }
19 :
20 : export type PlanConfirmState =
21 : | { phase: 'closed' }
22 : | { phase: 'confirming'; intent: PlanLifecycleIntent }
23 : | { phase: 'submitting'; intent: PlanLifecycleIntent }
24 : | {
25 : phase: 'refused'
26 : intent: PlanLifecycleIntent
27 : serverMessage: string | null
28 : }
29 :
30 : export type PlanConfirmEvent =
31 : | { type: 'opened'; intent: PlanLifecycleIntent }
32 : | { type: 'submitted' }
33 : | { type: 'settled'; outcome: PlanCommandOutcome }
34 : | { type: 'dismissed' }
35 :
36 5 : export const PLAN_CONFIRM_CLOSED: PlanConfirmState = { phase: 'closed' }
37 :
38 : /** The Stay on {plan} confirmation for a row whose status carries a scheduled change. */
39 0 : export function stayIntent(state: PlanRowState): PlanLifecycleIntent | null {
40 0 : if (state.kind !== 'active' || state.scheduledChange === null) return null
41 0 : return {
42 : kind: 'stay',
43 : planName: state.planName,
44 : targetPlanName: state.scheduledChange.planName,
45 : effectiveAt: state.scheduledChange.effectiveAt
46 : }
47 : }
48 :
49 : /** The Resume plan confirmation for a row whose plan is ending. */
50 0 : export function resumeIntent(state: PlanRowState): PlanLifecycleIntent | null {
51 0 : if (state.kind !== 'ending') return null
52 0 : return {
53 : kind: 'resume',
54 : planName: state.planName,
55 : renewsAt: state.endsAt
56 : }
57 : }
58 :
59 0 : export function reducePlanConfirm(
60 : state: PlanConfirmState,
61 : event: PlanConfirmEvent
62 : ): PlanConfirmState {
63 0 : switch (event.type) {
64 : case 'opened':
65 0 : return state.phase === 'closed'
66 : ? { phase: 'confirming', intent: event.intent }
67 : : state
68 : case 'submitted':
69 0 : return state.phase === 'confirming' || state.phase === 'refused'
70 : ? { phase: 'submitting', intent: state.intent }
71 : : state
72 : case 'settled':
73 0 : if (state.phase !== 'submitting') return state
74 0 : return event.outcome.ok
75 : ? PLAN_CONFIRM_CLOSED
76 : : {
77 : phase: 'refused',
78 : intent: state.intent,
79 : serverMessage: event.outcome.serverMessage
80 : }
81 : case 'dismissed':
82 0 : return state.phase === 'submitting' ? state : PLAN_CONFIRM_CLOSED
83 : }
84 : }
|