feat(SW-2033): Added new route for fetching room features, and merged the data with existing availability data * feat(SW-2033): Added new route for fetching room features, and merged the data with existing availability data * fix: issue with total price not including room features * fix: add return null * fix * fix * fixes from PR feedback Approved-by: Arvid Norlin
117 lines
3.1 KiB
TypeScript
117 lines
3.1 KiB
TypeScript
import { useSearchParams } from "next/navigation"
|
|
import { useEffect, useMemo, useState } from "react"
|
|
|
|
import { trpc } from "@/lib/trpc/client"
|
|
|
|
import { convertSearchParamsToObj, searchParamsToRecord } from "@/utils/url"
|
|
|
|
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
|
|
import type { SelectRateSearchParams } from "@/types/components/hotelReservation/selectRate/selectRate"
|
|
import type { Lang } from "@/constants/languages"
|
|
import type { ChildrenInRoom } from "@/utils/hotelSearchDetails"
|
|
|
|
export function useRoomsAvailability(
|
|
adultsCount: number[],
|
|
hotelId: string,
|
|
fromDateString: string,
|
|
toDateString: string,
|
|
lang: Lang,
|
|
childArray: ChildrenInRoom,
|
|
bookingCode?: string,
|
|
redemption?: boolean
|
|
) {
|
|
const searchParams = useSearchParams()
|
|
const searchParamsObj = convertSearchParamsToObj<SelectRateSearchParams>(
|
|
searchParamsToRecord(searchParams)
|
|
)
|
|
|
|
const hasPackagesParam = searchParamsObj.rooms.some((room) => room.packages)
|
|
const [hasRoomFeatures, setHasRoomFeatures] = useState(hasPackagesParam)
|
|
|
|
useEffect(() => {
|
|
setHasRoomFeatures(hasPackagesParam)
|
|
}, [hasPackagesParam, setHasRoomFeatures])
|
|
|
|
const { data: roomFeatures, isPending: isRoomFeaturesPending } =
|
|
trpc.hotel.availability.roomFeatures.useQuery(
|
|
{
|
|
hotelId,
|
|
startDate: fromDateString,
|
|
endDate: toDateString,
|
|
adultsCount,
|
|
childArray,
|
|
},
|
|
{
|
|
enabled: hasRoomFeatures,
|
|
}
|
|
)
|
|
|
|
const { data: roomsAvailability, isPending: isRoomsAvailabiltyPending } =
|
|
trpc.hotel.availability.roomsCombinedAvailability.useQuery({
|
|
adultsCount,
|
|
bookingCode,
|
|
childArray,
|
|
hotelId,
|
|
lang,
|
|
redemption,
|
|
roomStayEndDate: toDateString,
|
|
roomStayStartDate: fromDateString,
|
|
})
|
|
|
|
const combinedData = useMemo(() => {
|
|
if (!roomsAvailability) {
|
|
return undefined
|
|
}
|
|
if (!roomFeatures) {
|
|
return roomsAvailability
|
|
}
|
|
|
|
return roomsAvailability.map((room, idx) => {
|
|
if ("error" in room) {
|
|
return room
|
|
}
|
|
|
|
return {
|
|
...room,
|
|
roomConfigurations: room.roomConfigurations.map((config) => ({
|
|
...config,
|
|
features:
|
|
roomFeatures?.[idx]?.find(
|
|
(r) => r.roomTypeCode === config.roomTypeCode
|
|
)?.features ?? [],
|
|
})),
|
|
}
|
|
})
|
|
}, [roomFeatures, roomsAvailability])
|
|
|
|
return {
|
|
data: combinedData,
|
|
isPending: hasRoomFeatures
|
|
? isRoomsAvailabiltyPending || isRoomFeaturesPending
|
|
: isRoomsAvailabiltyPending,
|
|
}
|
|
}
|
|
|
|
export function useHotelPackages(
|
|
adultArray: number[],
|
|
childArray: ChildrenInRoom,
|
|
fromDateString: string,
|
|
toDateString: string,
|
|
hotelId: string,
|
|
lang: Lang
|
|
) {
|
|
return trpc.hotel.packages.get.useQuery({
|
|
adults: adultArray[0], // Using the first adult count
|
|
children: childArray?.[0]?.length, // Using the first children count
|
|
endDate: toDateString,
|
|
hotelId,
|
|
packageCodes: [
|
|
RoomPackageCodeEnum.ACCESSIBILITY_ROOM,
|
|
RoomPackageCodeEnum.PET_ROOM,
|
|
RoomPackageCodeEnum.ALLERGY_ROOM,
|
|
],
|
|
startDate: fromDateString,
|
|
lang: lang,
|
|
})
|
|
}
|