feat: save search params from select-rate to store
This commit is contained in:
@@ -1,30 +1,34 @@
|
||||
import { produce } from "immer"
|
||||
import { ReadonlyURLSearchParams } from "next/navigation"
|
||||
import { createContext, useContext } from "react"
|
||||
import { create, useStore } from "zustand"
|
||||
|
||||
import { bedTypeSchema } from "@/components/HotelReservation/EnterDetails/BedType/schema"
|
||||
import { breakfastStoreSchema } from "@/components/HotelReservation/EnterDetails/Breakfast/schema"
|
||||
import { detailsSchema } from "@/components/HotelReservation/EnterDetails/Details/schema"
|
||||
import getHotelReservationQueryParams from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
|
||||
|
||||
import { BreakfastPackage } from "@/types/components/enterDetails/breakfast"
|
||||
import { DetailsSchema } from "@/types/components/enterDetails/details"
|
||||
import { SidePeekEnum } from "@/types/components/enterDetails/sidePeek"
|
||||
import { StepEnum } from "@/types/components/enterDetails/step"
|
||||
import { BreakfastPackage } from "@/types/components/hotelReservation/enterDetails/breakfast"
|
||||
import { SidePeekEnum } from "@/types/components/hotelReservation/enterDetails/sidePeek"
|
||||
import { StepEnum } from "@/types/components/hotelReservation/enterDetails/step"
|
||||
import { BedTypeEnum } from "@/types/enums/bedType"
|
||||
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
|
||||
import type { BookingData } from "@/types/components/hotelReservation/enterDetails/bookingData"
|
||||
import type { DetailsSchema } from "@/types/components/hotelReservation/enterDetails/details"
|
||||
|
||||
const SESSION_STORAGE_KEY = "enterDetails"
|
||||
|
||||
interface EnterDetailsState {
|
||||
data: {
|
||||
userData: {
|
||||
bedType: BedTypeEnum | undefined
|
||||
breakfast: BreakfastPackage | BreakfastPackageEnum.NO_BREAKFAST | undefined
|
||||
} & DetailsSchema
|
||||
roomData: BookingData
|
||||
steps: StepEnum[]
|
||||
currentStep: StepEnum
|
||||
activeSidePeek: SidePeekEnum | null
|
||||
isValid: Record<StepEnum, boolean>
|
||||
completeStep: (updatedData: Partial<EnterDetailsState["data"]>) => void
|
||||
completeStep: (updatedData: Partial<EnterDetailsState["userData"]>) => void
|
||||
navigate: (
|
||||
step: StepEnum,
|
||||
updatedData?: Record<string, string | boolean | BreakfastPackage>
|
||||
@@ -34,13 +38,60 @@ interface EnterDetailsState {
|
||||
closeSidePeek: () => void
|
||||
}
|
||||
|
||||
export function initEditDetailsState(currentStep: StepEnum) {
|
||||
function getUpdatedValue<T>(
|
||||
searchParams: URLSearchParams,
|
||||
key: string,
|
||||
defaultValue: T
|
||||
): T {
|
||||
const value = searchParams.get(key)
|
||||
if (value === null) return defaultValue
|
||||
if (typeof defaultValue === "number")
|
||||
return parseInt(value, 10) as unknown as T
|
||||
if (typeof defaultValue === "boolean")
|
||||
return (value === "true") as unknown as T
|
||||
if (defaultValue instanceof Date) return new Date(value) as unknown as T
|
||||
|
||||
return value as unknown as T
|
||||
}
|
||||
|
||||
export function initEditDetailsState(
|
||||
currentStep: StepEnum,
|
||||
searchParams: ReadonlyURLSearchParams
|
||||
) {
|
||||
const isBrowser = typeof window !== "undefined"
|
||||
const sessionData = isBrowser
|
||||
? sessionStorage.getItem(SESSION_STORAGE_KEY)
|
||||
: null
|
||||
|
||||
const defaultData: EnterDetailsState["data"] = {
|
||||
const today = new Date()
|
||||
const tomorrow = new Date()
|
||||
tomorrow.setDate(today.getDate() + 1)
|
||||
|
||||
let roomData: BookingData
|
||||
if (searchParams?.size) {
|
||||
roomData = getHotelReservationQueryParams(searchParams)
|
||||
|
||||
roomData.room = roomData.room.map((room, index) => ({
|
||||
...room,
|
||||
adults: getUpdatedValue(
|
||||
searchParams,
|
||||
`room[${index}].adults`,
|
||||
room.adults
|
||||
),
|
||||
roomtypecode: getUpdatedValue(
|
||||
searchParams,
|
||||
`room[${index}].roomtypecode`,
|
||||
room.roomtypecode
|
||||
),
|
||||
ratecode: getUpdatedValue(
|
||||
searchParams,
|
||||
`room[${index}].ratecode`,
|
||||
room.ratecode
|
||||
),
|
||||
}))
|
||||
}
|
||||
|
||||
const defaultUserData: EnterDetailsState["userData"] = {
|
||||
bedType: undefined,
|
||||
breakfast: undefined,
|
||||
countryCode: "",
|
||||
@@ -54,14 +105,14 @@ export function initEditDetailsState(currentStep: StepEnum) {
|
||||
termsAccepted: false,
|
||||
}
|
||||
|
||||
let inputData = {}
|
||||
let inputUserData = {}
|
||||
if (sessionData) {
|
||||
inputData = JSON.parse(sessionData)
|
||||
inputUserData = JSON.parse(sessionData)
|
||||
}
|
||||
|
||||
const validPaths = [StepEnum.selectBed]
|
||||
|
||||
let initialData: EnterDetailsState["data"] = defaultData
|
||||
let initialData: EnterDetailsState["userData"] = defaultUserData
|
||||
|
||||
const isValid = {
|
||||
[StepEnum.selectBed]: false,
|
||||
@@ -70,19 +121,19 @@ export function initEditDetailsState(currentStep: StepEnum) {
|
||||
[StepEnum.payment]: false,
|
||||
}
|
||||
|
||||
const validatedBedType = bedTypeSchema.safeParse(inputData)
|
||||
const validatedBedType = bedTypeSchema.safeParse(inputUserData)
|
||||
if (validatedBedType.success) {
|
||||
validPaths.push(StepEnum.breakfast)
|
||||
initialData = { ...initialData, ...validatedBedType.data }
|
||||
isValid[StepEnum.selectBed] = true
|
||||
}
|
||||
const validatedBreakfast = breakfastStoreSchema.safeParse(inputData)
|
||||
const validatedBreakfast = breakfastStoreSchema.safeParse(inputUserData)
|
||||
if (validatedBreakfast.success) {
|
||||
validPaths.push(StepEnum.details)
|
||||
initialData = { ...initialData, ...validatedBreakfast.data }
|
||||
isValid[StepEnum.breakfast] = true
|
||||
}
|
||||
const validatedDetails = detailsSchema.safeParse(inputData)
|
||||
const validatedDetails = detailsSchema.safeParse(inputUserData)
|
||||
if (validatedDetails.success) {
|
||||
validPaths.push(StepEnum.payment)
|
||||
initialData = { ...initialData, ...validatedDetails.data }
|
||||
@@ -101,7 +152,8 @@ export function initEditDetailsState(currentStep: StepEnum) {
|
||||
}
|
||||
|
||||
return create<EnterDetailsState>()((set, get) => ({
|
||||
data: initialData,
|
||||
userData: initialData,
|
||||
roomData,
|
||||
steps: Object.values(StepEnum),
|
||||
setCurrentStep: (step) => set({ currentStep: step }),
|
||||
navigate: (step, updatedData) =>
|
||||
@@ -129,14 +181,17 @@ export function initEditDetailsState(currentStep: StepEnum) {
|
||||
isValid,
|
||||
completeStep: (updatedData) =>
|
||||
set(
|
||||
produce((state) => {
|
||||
produce((state: EnterDetailsState) => {
|
||||
state.isValid[state.currentStep] = true
|
||||
|
||||
const nextStep =
|
||||
state.steps[state.steps.indexOf(state.currentStep) + 1]
|
||||
|
||||
state.data = { ...state.data, ...updatedData }
|
||||
|
||||
// @ts-expect-error: ts has a hard time understanding that "false | true" equals "boolean"
|
||||
state.userData = {
|
||||
...state.userData,
|
||||
...updatedData,
|
||||
}
|
||||
state.currentStep = nextStep
|
||||
get().navigate(nextStep, updatedData)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user