feat: booking flow performance * feat: booking flow performance * Cleanup Approved-by: Michael Zetterberg Approved-by: Pontus Dreij
115 lines
3.1 KiB
TypeScript
115 lines
3.1 KiB
TypeScript
import { dt } from "@/lib/dt"
|
|
import {
|
|
getHotel,
|
|
getPackages,
|
|
getRoomsAvailability,
|
|
} from "@/lib/trpc/memoizedRequests"
|
|
|
|
import { auth } from "@/auth"
|
|
import Alert from "@/components/TempDesignSystem/Alert"
|
|
import { getIntl } from "@/i18n"
|
|
import { safeTry } from "@/utils/safeTry"
|
|
import { isValidSession } from "@/utils/session"
|
|
|
|
import { generateChildrenString } from "../../utils"
|
|
import { combineRoomAvailabilities } from "../utils"
|
|
import Rooms from "."
|
|
|
|
import styles from "./rooms.module.css"
|
|
|
|
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
|
|
import type { RoomsContainerProps } from "@/types/components/hotelReservation/selectRate/roomsContainer"
|
|
import { AlertTypeEnum } from "@/types/enums/alert"
|
|
|
|
export async function RoomsContainer({
|
|
adultArray,
|
|
childArray,
|
|
fromDate,
|
|
hotelId,
|
|
lang,
|
|
toDate,
|
|
}: RoomsContainerProps) {
|
|
const session = await auth()
|
|
const isUserLoggedIn = isValidSession(session)
|
|
|
|
const fromDateString = dt(fromDate).format("YYYY-MM-DD")
|
|
const toDateString = dt(toDate).format("YYYY-MM-DD")
|
|
|
|
const hotelDataPromise = safeTry(
|
|
getHotel({
|
|
hotelId: hotelId.toString(),
|
|
isCardOnlyPayment: false,
|
|
language: lang,
|
|
})
|
|
)
|
|
|
|
const packagesPromise = safeTry(
|
|
getPackages({
|
|
hotelId: hotelId.toString(),
|
|
startDate: fromDateString,
|
|
endDate: toDateString,
|
|
adults: adultArray[0],
|
|
children: childArray ? childArray.length : undefined,
|
|
packageCodes: [
|
|
RoomPackageCodeEnum.ACCESSIBILITY_ROOM,
|
|
RoomPackageCodeEnum.PET_ROOM,
|
|
RoomPackageCodeEnum.ALLERGY_ROOM,
|
|
],
|
|
})
|
|
)
|
|
|
|
const uniqueAdultCounts = [...new Set(adultArray)]
|
|
const roomsAvailabilityPromises = uniqueAdultCounts.map((adultCount) => {
|
|
return safeTry(
|
|
getRoomsAvailability({
|
|
hotelId: hotelId,
|
|
roomStayStartDate: fromDateString,
|
|
roomStayEndDate: toDateString,
|
|
adults: adultCount,
|
|
children:
|
|
childArray && childArray.length > 0
|
|
? generateChildrenString(childArray)
|
|
: undefined,
|
|
})
|
|
)
|
|
})
|
|
|
|
const [hotelData, hotelDataError] = await hotelDataPromise
|
|
const [packages, packagesError] = await packagesPromise
|
|
const roomsAvailabilityResults = await Promise.all(roomsAvailabilityPromises)
|
|
|
|
const roomsAvailability = combineRoomAvailabilities({
|
|
availabilityResults: roomsAvailabilityResults,
|
|
})
|
|
|
|
const intl = await getIntl(lang)
|
|
|
|
if (packagesError) {
|
|
// TODO: Log packages error
|
|
console.error("[RoomsContainer] unable to fetch packages")
|
|
}
|
|
|
|
if (!roomsAvailability) {
|
|
return (
|
|
<div className={styles.hotelAlert}>
|
|
<Alert
|
|
type={AlertTypeEnum.Info}
|
|
text={intl.formatMessage({
|
|
id: "There are no rooms available that match your request",
|
|
})}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<Rooms
|
|
availablePackages={packages ?? []}
|
|
hotelType={hotelData?.hotel.hotelType}
|
|
isUserLoggedIn={isUserLoggedIn}
|
|
roomsAvailability={roomsAvailability}
|
|
roomCategories={hotelData?.roomCategories ?? []}
|
|
/>
|
|
)
|
|
}
|