feat(SW-1259): Enter details multiroom * refactor: remove per-step URLs * WIP: map multiroom data * fix: lint errors in details page * fix: made useEnterDetailsStore tests pass * fix: WIP refactor enter details store * fix: WIP enter details store update * fix: added room index to select correct room * fix: added logic for navigating between steps and rooms * fix: update summary to work with store changes * fix: added room and total price calculation * fix: removed unused code and added test for breakfast included * refactor: move store selectors into helpers * refactor: session storage state for multiroom booking * feat: update enter details accordion navigation * fix: added room index to each form component so they select correct room * fix: added unique id to input to handle case when multiple inputs have same name * fix: update payment step with store changes * fix: rebase issues * fix: now you should only be able to go to a step if previous room is completed * refactor: cleanup * fix: if no availability just skip that room for now * fix: add select-rate Summary and adjust typings Approved-by: Arvid Norlin
81 lines
2.1 KiB
TypeScript
81 lines
2.1 KiB
TypeScript
"use client"
|
|
import { useEffect, useRef } from "react"
|
|
|
|
import { createDetailsStore } from "@/stores/enter-details"
|
|
import {
|
|
checkIsSameRoom,
|
|
clearSessionStorage,
|
|
readFromSessionStorage,
|
|
} from "@/stores/enter-details/helpers"
|
|
|
|
import { DetailsContext } from "@/contexts/Details"
|
|
|
|
import type { DetailsStore } from "@/types/contexts/enter-details"
|
|
import type { DetailsProviderProps } from "@/types/providers/enter-details"
|
|
import type { InitialState } from "@/types/stores/enter-details"
|
|
|
|
export default function EnterDetailsProvider({
|
|
booking,
|
|
showBreakfastStep,
|
|
children,
|
|
roomsData,
|
|
searchParamsStr,
|
|
user,
|
|
vat,
|
|
}: DetailsProviderProps) {
|
|
const storeRef = useRef<DetailsStore>()
|
|
if (!storeRef.current) {
|
|
const initialData: InitialState = {
|
|
booking,
|
|
rooms: roomsData
|
|
.filter((r) => r.bedTypes?.length) // TODO: how to handle room without bedtypes?
|
|
.map((room) => ({
|
|
roomFeatures: room.packages,
|
|
roomRate: room.roomRate,
|
|
roomType: room.roomType,
|
|
cancellationText: room.cancellationText,
|
|
rateDetails: room.rateDetails,
|
|
bedType:
|
|
room.bedTypes?.length === 1
|
|
? {
|
|
roomTypeCode: room.bedTypes[0].value,
|
|
description: room.bedTypes[0].description,
|
|
}
|
|
: undefined,
|
|
})),
|
|
vat,
|
|
}
|
|
|
|
if (!showBreakfastStep) {
|
|
initialData.breakfast = false
|
|
}
|
|
|
|
storeRef.current = createDetailsStore(initialData, searchParamsStr, user)
|
|
}
|
|
|
|
useEffect(() => {
|
|
const storedValues = readFromSessionStorage()
|
|
if (!storedValues) {
|
|
return
|
|
}
|
|
const isSameRoom = checkIsSameRoom(storedValues.booking, booking)
|
|
if (!isSameRoom) {
|
|
clearSessionStorage()
|
|
return
|
|
}
|
|
|
|
const state = storeRef.current?.getState()
|
|
storeRef.current?.setState({
|
|
...state,
|
|
rooms: storedValues.rooms,
|
|
bookingProgress: storedValues.bookingProgress,
|
|
})
|
|
}, [booking])
|
|
|
|
return (
|
|
<DetailsContext.Provider value={storeRef.current}>
|
|
{children}
|
|
</DetailsContext.Provider>
|
|
)
|
|
}
|