Skip to content

Commit

Permalink
feat(#213): try cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumpy-Squirrel committed Dec 28, 2023
1 parent 8fad78c commit c7170d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 41 deletions.
25 changes: 10 additions & 15 deletions src/state/models/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TicketAddonsConfig[K]['options'][L]>
}
export type TicketLevelAddons = {
readonly [K in keyof TicketAddonsConfig]: {
readonly selected: boolean
readonly options: {
[L in keyof TicketAddonsConfig[K]['options']]: ParseAddonOption<TicketAddonsConfig[K]['options'][L]>
}
// 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
Expand Down
16 changes: 4 additions & 12 deletions src/state/reducers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,18 +53,14 @@ const transformPersonalInfo = (payload: GetAction<SubmitFormActionBundle<'regist
const registrationInfoReducer = (state: Partial<RegistrationInfo>, action: GetAction<AnyAppAction>): Partial<RegistrationInfo> => {
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
Expand All @@ -75,11 +71,7 @@ const registrationInfoReducer = (state: Partial<RegistrationInfo>, 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 {
Expand Down
30 changes: 16 additions & 14 deletions src/state/selectors/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <F extends FormIds>(id: F) => (s: AppState) => FormValuesType<F>

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<FormIds> => {
switch (id) {
Expand All @@ -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),
}
}

Expand Down

0 comments on commit c7170d5

Please sign in to comment.