104 lines
3.2 KiB
TypeScript
104 lines
3.2 KiB
TypeScript
import { ChildBedTypeEnum } from "@/constants/booking"
|
|
|
|
import { getFormattedUrlQueryParams } from "@/utils/url"
|
|
|
|
import { ChildBedMapEnum } from "@/types/components/bookingWidget/enums"
|
|
import { BookingData } from "@/types/components/hotelReservation/enterDetails/bookingData"
|
|
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
|
|
import type {
|
|
Child,
|
|
SelectRateSearchParams,
|
|
} from "@/types/components/hotelReservation/selectRate/selectRate"
|
|
|
|
export function getHotelReservationQueryParams(searchParams: URLSearchParams) {
|
|
return getFormattedUrlQueryParams(searchParams, {
|
|
adults: "number",
|
|
age: "number",
|
|
bed: ChildBedMapEnum,
|
|
}) as SelectRateSearchParams
|
|
}
|
|
|
|
export const bedTypeMap: Record<number, ChildBedTypeEnum> = {
|
|
[ChildBedMapEnum.IN_ADULTS_BED]: ChildBedTypeEnum.ParentsBed,
|
|
[ChildBedMapEnum.IN_CRIB]: ChildBedTypeEnum.Crib,
|
|
[ChildBedMapEnum.IN_EXTRA_BED]: ChildBedTypeEnum.ExtraBed,
|
|
}
|
|
|
|
export function generateChildrenString(children: Child[]): string {
|
|
return `[${children
|
|
.map((child) => {
|
|
const age = child.age
|
|
const bedType = bedTypeMap[parseInt(child.bed.toString())]
|
|
return `${age}:${bedType}`
|
|
})
|
|
.join(",")}]`
|
|
}
|
|
|
|
export function getQueryParamsForEnterDetails(
|
|
searchParams: URLSearchParams
|
|
): BookingData {
|
|
const selectRoomParamsObject = getHotelReservationQueryParams(searchParams)
|
|
|
|
const { room } = selectRoomParamsObject
|
|
return {
|
|
fromDate: selectRoomParamsObject.fromDate,
|
|
toDate: selectRoomParamsObject.toDate,
|
|
hotel: selectRoomParamsObject.hotel,
|
|
rooms: room.map((room) => ({
|
|
adults: room.adults, // TODO: Handle multiple rooms
|
|
children: room.child, // TODO: Handle multiple rooms and children
|
|
roomTypeCode: room.roomtype,
|
|
rateCode: room.ratecode,
|
|
packages: room.packages?.split(",") as RoomPackageCodeEnum[],
|
|
counterRateCode: room.counterratecode,
|
|
})),
|
|
}
|
|
}
|
|
|
|
export function createQueryParamsForEnterDetails(
|
|
bookingData: BookingData,
|
|
intitalSearchParams: URLSearchParams
|
|
) {
|
|
const { hotel, fromDate, toDate, rooms } = bookingData
|
|
|
|
const bookingSearchParams = new URLSearchParams({ hotel, fromDate, toDate })
|
|
const searchParams = new URLSearchParams([
|
|
...intitalSearchParams,
|
|
...bookingSearchParams,
|
|
])
|
|
|
|
rooms.forEach((item, index) => {
|
|
if (item?.adults) {
|
|
searchParams.set(`room[${index}].adults`, item.adults.toString())
|
|
}
|
|
if (item?.children) {
|
|
item.children.forEach((child, childIndex) => {
|
|
searchParams.set(
|
|
`room[${index}].child[${childIndex}].age`,
|
|
child.age.toString()
|
|
)
|
|
searchParams.set(
|
|
`room[${index}].child[${childIndex}].bed`,
|
|
child.bed.toString()
|
|
)
|
|
})
|
|
}
|
|
if (item?.roomTypeCode) {
|
|
searchParams.set(`room[${index}].roomtype`, item.roomTypeCode)
|
|
}
|
|
if (item?.rateCode) {
|
|
searchParams.set(`room[${index}].ratecode`, item.rateCode)
|
|
}
|
|
|
|
if (item?.counterRateCode) {
|
|
searchParams.set(`room[${index}].counterratecode`, item.counterRateCode)
|
|
}
|
|
|
|
if (item.packages && item.packages.length > 0) {
|
|
searchParams.set(`room[${index}].packages`, item.packages.join(","))
|
|
}
|
|
})
|
|
|
|
return searchParams
|
|
}
|