fix: close summary sheet

This commit is contained in:
Christel Westerberg
2024-11-26 14:37:20 +01:00
committed by Simon Emanuelsson
parent ccb15593ea
commit 439bc13e65
9 changed files with 197 additions and 264 deletions

View File

@@ -4,21 +4,25 @@ import { useContext } from "react"
import { create, useStore } from "zustand"
import { createJSONStorage, persist } from "zustand/middleware"
import { bedTypeSchema } from "@/components/HotelReservation/EnterDetails/BedType/schema"
import { breakfastStoreSchema } from "@/components/HotelReservation/EnterDetails/Breakfast/schema"
import {
guestDetailsSchema,
signedInDetailsSchema,
} from "@/components/HotelReservation/EnterDetails/Details/schema"
import { DetailsContext } from "@/contexts/Details"
import { arrayMerge } from "@/utils/merge"
import {
add,
calcTotalMemberPrice,
calcTotalPublicPrice,
checkIsSameBooking,
extractGuestFromUser,
getHydratedMemberPrice,
getInitialRoomPrice,
getInitialTotalPrice,
langToCurrency,
navigate,
persistedStateSchema,
validateSteps,
} from "./helpers"
import { CurrencyEnum } from "@/types/enums/currency"
@@ -27,7 +31,6 @@ import type {
DetailsState,
FormValues,
InitialState,
PersistedState,
} from "@/types/stores/enter-details"
import type { SafeUser } from "@/types/user"
@@ -292,6 +295,10 @@ export function createDetailsStore(
const memberPrice = calcTotalMemberPrice(state)
state.roomPrice = memberPrice.roomPrice
state.totalPrice = memberPrice.totalPrice
} else {
const publicPrice = calcTotalPublicPrice(state)
state.roomPrice = publicPrice.roomPrice
state.totalPrice = publicPrice.totalPrice
}
const currentStepIndex = state.steps.indexOf(state.currentStep)
@@ -332,95 +339,125 @@ export function createDetailsStore(
}),
{
name: detailsStorageName,
merge(_persistedState, currentState) {
const parsedPersistedState =
persistedStateSchema.safeParse(_persistedState)
let persistedState
if (parsedPersistedState.success) {
if (parsedPersistedState.data) {
persistedState = parsedPersistedState.data as PersistedState
merge(persistedState, currentState) {
if (
persistedState &&
Object.prototype.hasOwnProperty.call(persistedState, "booking")
) {
const isSameBooking = checkIsSameBooking(
// @ts-expect-error - persistedState cannot be typed
persistedState.booking,
currentState.booking
)
if (!isSameBooking) {
return deepmerge(persistedState, currentState, { arrayMerge })
}
}
return deepmerge(currentState, persistedState ?? {}, { arrayMerge })
},
onRehydrateStorage(initState) {
return function (state) {
if (state) {
if (
(state.guest.join || state.guest.membershipNo || isMember) &&
state.roomRate.memberRate
) {
const memberPrice = calcTotalMemberPrice(state)
if (!persistedState) {
persistedState = currentState as DetailsState
}
state.roomPrice = memberPrice.roomPrice
state.totalPrice = memberPrice.totalPrice
} else {
const publicPrice = calcTotalPublicPrice(state)
if (
currentState.guest.join ||
!!currentState.guest.membershipNo ||
isMember
) {
if (currentState.roomRate.memberRate) {
const memberPrice = getHydratedMemberPrice(
currentState.roomRate.memberRate,
currentState.breakfast,
currentState.packages
state.roomPrice = publicPrice.roomPrice
state.totalPrice = publicPrice.totalPrice
}
/**
* TODO:
* - when included in rate, can packages still be received?
* - no hotels yet with breakfast included in the rate so
* impossible to build for atm.
*
* checking against initialState since that means the
* hotel doesn't offer breakfast
*
* matching breakfast first so the steps array is altered
* before the bedTypes possible step altering
*/
if (initialState.breakfast === false) {
state.steps = state.steps.filter(
(step) => step === StepEnum.breakfast
)
if (state.currentStep === StepEnum.breakfast) {
state.currentStep = state.steps[1]
}
}
if (initialState.bedType) {
if (state.currentStep === StepEnum.selectBed) {
state.currentStep = state.steps[1]
}
}
const isSameBooking = checkIsSameBooking(
initState.booking,
state.booking
)
currentState.roomPrice = memberPrice.roomPrice
currentState.totalPrice = memberPrice.totalPrice
const validateBooking = isSameBooking ? state : initState
const validPaths = [StepEnum.selectBed]
const validatedBedType = bedTypeSchema.safeParse(validateBooking)
if (validatedBedType.success) {
state.isValid["select-bed"] = true
validPaths.push(state.steps[1])
}
const validatedBreakfast =
breakfastStoreSchema.safeParse(validateBooking)
if (validatedBreakfast.success) {
state.isValid.breakfast = true
validPaths.push(StepEnum.details)
}
const detailsSchema = isMember
? signedInDetailsSchema
: guestDetailsSchema
const validatedDetails = detailsSchema.safeParse(
validateBooking.guest
)
// Need to add the breakfast check here too since
// when a member comes into the flow, their data is
// already added and valid, and thus to avoid showing a
// step the user hasn't been on yet as complete
if (state.isValid.breakfast && validatedDetails.success) {
state.isValid.details = true
validPaths.push(StepEnum.payment)
}
if (!validPaths.includes(state.currentStep)) {
state.currentStep = validPaths.at(-1)!
}
if (currentStep !== state.currentStep) {
const stateCurrentStep = state.currentStep
setTimeout(() => {
navigate(stateCurrentStep, searchParams)
})
}
if (isSameBooking) {
state = deepmerge<DetailsState>(initState, state, {
arrayMerge,
})
} else {
state = deepmerge<DetailsState>(state, initState, {
arrayMerge,
})
}
}
}
const isSameBooking = checkIsSameBooking(
persistedState.booking,
currentState.booking
)
let mergedState
if (isSameBooking) {
mergedState = deepmerge<DetailsState>(
currentState,
persistedState,
{ arrayMerge }
)
} else {
mergedState = deepmerge<DetailsState>(
persistedState,
currentState,
{ arrayMerge }
)
}
/**
* TODO:
* - when included in rate, can packages still be received?
* - no hotels yet with breakfast included in the rate so
* impossible to build for atm.
*
* checking against initialState since that means the
* hotel doesn't offer breakfast
*
* matching breakfast first so the steps array is altered
* before the bedTypes possible step altering
*/
if (initialState.breakfast === false) {
mergedState.steps = mergedState.steps.filter(
(step) => step === StepEnum.breakfast
)
if (mergedState.currentStep === StepEnum.breakfast) {
mergedState.currentStep = mergedState.steps[1]
}
}
if (initialState.bedType) {
if (mergedState.currentStep === StepEnum.selectBed) {
mergedState.currentStep = mergedState.steps[1]
}
}
const validPaths = validateSteps(mergedState, isMember)
if (!validPaths.includes(mergedState.currentStep)) {
mergedState.currentStep = validPaths.at(-1)!
}
if (currentStep !== mergedState.currentStep) {
setTimeout(() => {
navigate(mergedState.currentStep, searchParams)
})
}
return mergedState
},
partialize(state) {
return {