SupportForm.state.ts96 lines · main
| 1 | import type { ExtendedSupportCategories } from './Support.constants' |
| 2 | import { neverGuard } from '@/lib/helpers' |
| 3 | |
| 4 | export type SupportFormState = |
| 5 | | { |
| 6 | type: 'initializing' |
| 7 | } |
| 8 | | { |
| 9 | type: 'editing' |
| 10 | } |
| 11 | | { |
| 12 | type: 'submitting' |
| 13 | } |
| 14 | | { |
| 15 | type: 'success' |
| 16 | sentProjectRef: string | undefined |
| 17 | sentOrgSlug: string | undefined |
| 18 | sentCategory: ExtendedSupportCategories |
| 19 | } |
| 20 | | { |
| 21 | type: 'error' |
| 22 | message: string |
| 23 | } |
| 24 | |
| 25 | export type SupportFormActions = |
| 26 | | { type: 'INITIALIZE'; debugSource?: string } |
| 27 | | { type: 'SUBMIT'; debugSource?: string } |
| 28 | | { |
| 29 | type: 'SUCCESS' |
| 30 | sentProjectRef: string | undefined |
| 31 | sentOrgSlug: string | undefined |
| 32 | sentCategory: ExtendedSupportCategories |
| 33 | debugSource?: string |
| 34 | } |
| 35 | | { type: 'ERROR'; message: string; debugSource?: string } |
| 36 | | { type: 'RETURN_TO_EDITING'; debugSource?: string } |
| 37 | |
| 38 | export function createInitialSupportFormState(): SupportFormState { |
| 39 | return { |
| 40 | type: 'initializing', |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | export function supportFormReducer( |
| 45 | state: SupportFormState, |
| 46 | action: SupportFormActions |
| 47 | ): SupportFormState { |
| 48 | switch (state.type) { |
| 49 | case 'initializing': |
| 50 | if (action.type === 'INITIALIZE') { |
| 51 | return { type: 'editing' } |
| 52 | } |
| 53 | console.warn( |
| 54 | `[SupportForm > supportFormReducer] ${action.type} action not allowed in 'initializing' state` |
| 55 | ) |
| 56 | return state |
| 57 | case 'editing': |
| 58 | if (action.type === 'SUBMIT') { |
| 59 | return { type: 'submitting' } |
| 60 | } |
| 61 | console.warn( |
| 62 | `[SupportForm > supportFromReducer] ${action.type} action not allowed in 'filling_out' state` |
| 63 | ) |
| 64 | return state |
| 65 | case 'submitting': |
| 66 | if (action.type === 'SUCCESS') { |
| 67 | return { |
| 68 | type: 'success', |
| 69 | sentProjectRef: action.sentProjectRef, |
| 70 | sentOrgSlug: action.sentOrgSlug, |
| 71 | sentCategory: action.sentCategory, |
| 72 | } |
| 73 | } |
| 74 | if (action.type === 'ERROR') { |
| 75 | return { |
| 76 | type: 'error', |
| 77 | message: action.message, |
| 78 | } |
| 79 | } |
| 80 | console.warn( |
| 81 | `[SupportForm > supportFormReducer] ${action.type} action not allowed in 'submitting' state` |
| 82 | ) |
| 83 | return state |
| 84 | case 'success': |
| 85 | console.warn(`[SupportForm > supportFormReducer] ${action.type} allowed in 'success' state`) |
| 86 | return state |
| 87 | case 'error': |
| 88 | if (action.type === 'RETURN_TO_EDITING') { |
| 89 | return { type: 'editing' } |
| 90 | } |
| 91 | console.warn(`[SupportForm > supportFormReducer] ${action.type} allowed in 'success' state`) |
| 92 | return state |
| 93 | default: |
| 94 | return neverGuard(state) |
| 95 | } |
| 96 | } |