fix: persist selection of bed and breakfast if same room

This commit is contained in:
Christel Westerberg
2024-12-04 16:16:32 +01:00
parent f075521421
commit 4210218852
11 changed files with 410 additions and 439 deletions

View File

@@ -1,13 +1,22 @@
import deepmerge from "deepmerge"
import isEqual from "fast-deep-equal"
import { Lang } from "@/constants/languages"
import { getLang } from "@/i18n/serverContext"
import { arrayMerge } from "@/utils/merge"
import { detailsStorageName } from "."
import type { BookingData } from "@/types/components/hotelReservation/enterDetails/bookingData"
import { CurrencyEnum } from "@/types/enums/currency"
import { StepEnum } from "@/types/enums/step"
import type { DetailsState, RoomRate } from "@/types/stores/enter-details"
import type {
DetailsState,
PersistedState,
PersistedStatePart,
RoomRate,
} from "@/types/stores/enter-details"
import type { SafeUser } from "@/types/user"
export function langToCurrency() {
@@ -44,8 +53,28 @@ export function navigate(step: StepEnum, searchParams: string) {
window.history.pushState({ step }, "", `${step}?${searchParams}`)
}
export function checkIsSameBooking(prev: BookingData, next: BookingData) {
return isEqual(prev, next)
export function checkIsSameRoom(prev: BookingData, next: BookingData) {
const { rooms: prevRooms, ...prevBooking } = prev
const prevRoomsWithoutRateCodes = prevRooms.map(
({ rateCode, counterRateCode, ...room }) => room
)
const { rooms: nextRooms, ...nextBooking } = next
const nextRoomsWithoutRateCodes = nextRooms.map(
({ rateCode, counterRateCode, ...room }) => room
)
return isEqual(
{
...prevBooking,
rooms: prevRoomsWithoutRateCodes,
},
{
...nextBooking,
rooms: nextRoomsWithoutRateCodes,
}
)
}
export function add(...nums: (number | string | undefined)[]) {
@@ -160,9 +189,11 @@ export function calcTotalPrice(
> &
DetailsState["roomRate"]["publicRate"]
) {
// state is sometimes read-only, thus we
// need to create a copy of the values
const roomAndTotalPrice = {
roomPrice: state.roomPrice,
totalPrice: state.totalPrice,
roomPrice: { ...state.roomPrice },
totalPrice: { ...state.totalPrice },
}
if (state.requestedPrice?.pricePerStay) {
roomAndTotalPrice.roomPrice.requested = {
@@ -222,3 +253,16 @@ export function calcTotalPrice(
return roomAndTotalPrice
}
export function writeToSessionStorage(part: PersistedStatePart) {
const unparsedData = sessionStorage.getItem(detailsStorageName)
if (unparsedData) {
const data: PersistedState = JSON.parse(unparsedData)
// @ts-expect-error - deepmerge is not to happy with
// the part type
const updated = deepmerge(data, part, { arrayMerge })
sessionStorage.setItem(detailsStorageName, JSON.stringify(updated))
} else {
sessionStorage.setItem(detailsStorageName, JSON.stringify(part))
}
}