chore(SW-3321): Moved Select rate context to booking-flow package * chore(SW-3321): Moved Select rate context to booking-flow package * chore(SW-3321): Optimised code Approved-by: Joakim Jäderberg
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import isEqual from "fast-deep-equal"
|
|
|
|
import { parseBookingWidgetSearchParams, searchParamsToRecord } from "./url"
|
|
|
|
import type { BookingWidgetSearchData } from "../components/BookingWidget"
|
|
import type { SelectRateBooking } from "../types/components/selectRate/selectRate"
|
|
|
|
/**
|
|
* Parses and compares booking widget search parameters
|
|
* @param param0
|
|
* @returns true if the searches are the same
|
|
*/
|
|
export function isSameBookingWidgetParams({
|
|
previousParams,
|
|
currentParams,
|
|
}: {
|
|
previousParams: URLSearchParams
|
|
currentParams: URLSearchParams
|
|
}) {
|
|
const previousParamsObject = parseBookingWidgetSearchParams(
|
|
searchParamsToRecord(previousParams)
|
|
)
|
|
const currentParamsObject = parseBookingWidgetSearchParams(
|
|
searchParamsToRecord(currentParams)
|
|
)
|
|
|
|
if (!previousParamsObject && !currentParamsObject) return false
|
|
if (!previousParamsObject || !currentParamsObject) return true
|
|
|
|
const isSame = isSameBooking(previousParamsObject, currentParamsObject)
|
|
return !isSame
|
|
}
|
|
|
|
/**
|
|
* Compares if two sets of select-rate searches are the same
|
|
* @param prev
|
|
* @param next
|
|
* @returns
|
|
*/
|
|
export function isSameBooking(
|
|
prev: (SelectRateBooking | BookingWidgetSearchData) & { errorCode?: string },
|
|
next: (SelectRateBooking | BookingWidgetSearchData) & { errorCode?: string }
|
|
) {
|
|
const { rooms: prevRooms, errorCode: prevErrorCode, ...prevBooking } = prev
|
|
|
|
const prevRoomsWithoutRateCodes = prevRooms?.map(
|
|
({ adults, childrenInRoom }) => ({ adults, childrenInRoom })
|
|
)
|
|
const { rooms: nextRooms, errorCode: nextErrorCode, ...nextBooking } = next
|
|
|
|
const nextRoomsWithoutRateCodes = nextRooms?.map(
|
|
({ adults, childrenInRoom }) => ({ adults, childrenInRoom })
|
|
)
|
|
|
|
return isEqual(
|
|
{
|
|
...prevBooking,
|
|
rooms: prevRoomsWithoutRateCodes,
|
|
},
|
|
{
|
|
...nextBooking,
|
|
rooms: nextRoomsWithoutRateCodes,
|
|
}
|
|
)
|
|
}
|