diff --git a/src/state/models/register.ts b/src/state/models/register.ts index 3c7847a..6394e5e 100644 --- a/src/state/models/register.ts +++ b/src/state/models/register.ts @@ -26,25 +26,20 @@ export type TicketType = { readonly type: 'full' } | { readonly type: 'day', readonly day: ReadonlyDateTime } -export type TicketLevel = { - readonly level: keyof TicketLevelConfig - readonly addons: { - readonly [K in keyof TicketAddonsConfig]: { - readonly selected: boolean - readonly options: { - [L in keyof TicketAddonsConfig[K]['options']]: ParseAddonOption - } +export type TicketLevelAddons = { + readonly [K in keyof TicketAddonsConfig]: { + readonly selected: boolean + readonly options: { + [L in keyof TicketAddonsConfig[K]['options']]: ParseAddonOption } - // readonly stagePass: { - // readonly selected: boolean - // } - // readonly tshirt: { - // readonly selected: boolean - // readonly size: 'S' | 'M' | 'L' | 'XL' | 'XXL' - // } } } +export type TicketLevel = { + readonly level: keyof TicketLevelConfig + readonly addons: TicketLevelAddons +} + export interface ContactInfo { readonly email: string readonly phoneNumber: string diff --git a/src/state/reducers/register.ts b/src/state/reducers/register.ts index 45c01ea..49fb69d 100644 --- a/src/state/reducers/register.ts +++ b/src/state/reducers/register.ts @@ -5,7 +5,7 @@ import { SubmitForm, SubmitFormActionBundle } from '~/state/actions/forms' import { LoadRegistrationState, SetLocale } from '~/state/actions/register' import config from '~/config' import { DateTime } from 'luxon' -import { map } from 'ramda' +import { determineDefaultAddons } from '~/state/selectors/forms' export interface ClosedRegisterState { readonly isOpen: false | null @@ -53,18 +53,14 @@ const transformPersonalInfo = (payload: GetAction, action: GetAction): Partial => { switch (action.type) { case SubmitForm('register-ticket-type').type: { - // here we can force reset ticket level to defaults (different hidden packages, different defaults) + // here we can force reset ticket addons to defaults (different hidden packages, different defaults) if (action.payload.type === 'day') { // not setting ticketType - it is set when choosing a day if (state.ticketLevel?.level) { return { ...state, ticketLevel: { level: state.ticketLevel.level, - // reset addons - addons: map(addon => ({ - selected: addon.default && !(addon.unavailableFor?.type?.includes('day') ?? false), - options: map(option => option.default as never, addon.options), - }), config.addons), + addons: determineDefaultAddons('day'), } } } else { return state @@ -75,11 +71,7 @@ const registrationInfoReducer = (state: Partial, action: GetAc return { ...state, ticketLevel: { level: state.ticketLevel.level, - // reset addons - addons: map(addon => ({ - selected: addon.default && !(addon.unavailableFor?.type?.includes('full') ?? false), - options: map(option => option.default as never, addon.options), - }), config.addons), + addons: determineDefaultAddons('full'), }, ticketType: { type: action.payload.type! } } } else { diff --git a/src/state/selectors/forms.ts b/src/state/selectors/forms.ts index e36516b..a339330 100644 --- a/src/state/selectors/forms.ts +++ b/src/state/selectors/forms.ts @@ -4,9 +4,24 @@ import { FormIds, FormValuesType } from '../forms' import config from '~/config' import { map } from 'ramda' import { getContactInfo, getOptionalInfo, getPersonalInfo, getTicketLevel, getTicketType, isEditMode } from './register' +import { TicketLevelAddons } from '~/state/models/register' type GetDefaultFormValuesFn = (id: F) => (s: AppState) => FormValuesType +export const determineDefaultAddons = (ticketType: 'day' | 'full' | undefined): TicketLevelAddons => { + if (ticketType === undefined) { + return map(addon => ({ + selected: addon.default, + options: map(option => option.default as never, addon.options), + }), config.addons) + } else { + return map(addon => ({ + selected: addon.default && !(addon.unavailableFor?.type?.includes(ticketType) ?? false), + options: map(option => option.default as never, addon.options), + }), config.addons) + } +} + // eslint-disable-next-line complexity export const getDefaultFormValues = ((id: FormIds) => (s: AppState): FormValuesType => { switch (id) { @@ -22,22 +37,9 @@ export const getDefaultFormValues = ((id: FormIds) => (s: AppState): FormValuesT case 'register-ticket-level': { const ticketType = getTicketType()(s) - if (ticketType === undefined) { - return { - level: null, - addons: map(addon => ({ - selected: addon.default, - options: map(option => option.default as never, addon.options), - }), config.addons), - } - } - return { level: null, - addons: map(addon => ({ - selected: addon.default && !(addon.unavailableFor?.type?.includes(ticketType.type) ?? false), - options: map(option => option.default as never, addon.options), - }), config.addons), + addons: determineDefaultAddons(ticketType?.type), } }