Fix: created util to handle searchParams for hotelreservation

This commit is contained in:
Pontus Dreij
2024-12-12 14:36:41 +01:00
parent ec0c6234ef
commit 50fc8a183c
6 changed files with 106 additions and 110 deletions

View File

@@ -1,15 +1,11 @@
import { notFound } from "next/navigation"
import { Suspense } from "react"
import { getHotelData, getLocations } from "@/lib/trpc/memoizedRequests"
import HotelInfoCard from "@/components/HotelReservation/SelectRate/HotelInfoCard"
import { RoomsContainer } from "@/components/HotelReservation/SelectRate/Rooms/RoomsContainer"
import { RoomsContainerSkeleton } from "@/components/HotelReservation/SelectRate/Rooms/RoomsContainerSkeleton"
import { getHotelReservationQueryParams } from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
import { setLang } from "@/i18n/serverContext"
import { safeTry } from "@/utils/safeTry"
import { getHotelSearchDetails } from "../utils"
import { getValidDates } from "./getValidDates"
import type { SelectRateSearchParams } from "@/types/components/hotelReservation/selectRate/selectRate"
@@ -20,43 +16,16 @@ export default async function SelectRatePage({
searchParams,
}: PageArgs<LangParams & { section: string }, SelectRateSearchParams>) {
setLang(params.lang)
const locations = await getLocations()
if (!locations || "error" in locations) {
return null
}
const hotel = locations.data.find(
(location) =>
"operaId" in location && location.operaId == searchParams.hotel
)
if (!hotel) {
return notFound()
}
const selectRoomParams = new URLSearchParams(searchParams)
const selectRoomParamsObject =
getHotelReservationQueryParams(selectRoomParams)
if (!selectRoomParamsObject.room) {
return notFound()
}
const searchDetails = await getHotelSearchDetails({ searchParams })
if (!searchDetails) return null
const { hotel, adultsInRoom, childrenInRoomArray } = searchDetails
const { fromDate, toDate } = getValidDates(
searchParams.fromDate,
searchParams.toDate
)
const adults = selectRoomParamsObject.room[0].adults || 1 // TODO: Handle multiple rooms
const children = selectRoomParamsObject.room[0].child // TODO: Handle multiple rooms
const [hotelData, hotelDataError] = await safeTry(
getHotelData({ hotelId: searchParams.hotel, language: params.lang })
)
if (!hotelData && !hotelDataError) {
return notFound()
}
const hotelId = +searchParams.hotel
const hotelId = +hotel.id
return (
<>
<HotelInfoCard
@@ -64,8 +33,8 @@ export default async function SelectRatePage({
lang={params.lang}
fromDate={fromDate.toDate()}
toDate={toDate.toDate()}
adultCount={adults}
childArray={children}
adultCount={adultsInRoom}
childArray={childrenInRoomArray}
/>
<Suspense key={hotelId} fallback={<RoomsContainerSkeleton />}>
@@ -74,8 +43,8 @@ export default async function SelectRatePage({
lang={params.lang}
fromDate={fromDate.toDate()}
toDate={toDate.toDate()}
adultCount={adults}
childArray={children}
adultCount={adultsInRoom}
childArray={childrenInRoomArray}
/>
</Suspense>
</>