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,16 +1,11 @@
import { notFound } from "next/navigation"
import { Suspense } from "react"
import { getLocations } from "@/lib/trpc/memoizedRequests"
import SelectHotel from "@/components/HotelReservation/SelectHotel"
import { SelectHotelSkeleton } from "@/components/HotelReservation/SelectHotel/SelectHotelSkeleton"
import {
generateChildrenString,
getHotelReservationQueryParams,
} from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
import { setLang } from "@/i18n/serverContext"
import { getHotelSearchDetails } from "../utils"
import type { SelectHotelSearchParams } from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
import type { LangParams, PageArgs } from "@/types/params"
@@ -19,39 +14,15 @@ export default async function SelectHotelPage({
searchParams,
}: PageArgs<LangParams, SelectHotelSearchParams>) {
setLang(params.lang)
const locations = await getLocations()
if (!locations || "error" in locations) {
return null
}
const city = locations.data.find(
(location) =>
location.name.toLowerCase() === searchParams.city.toLowerCase()
)
if (!city) return notFound()
const selectHotelParams = new URLSearchParams(searchParams)
const selectHotelParamsObject =
getHotelReservationQueryParams(selectHotelParams)
if (
!selectHotelParamsObject.room ||
selectHotelParamsObject.room.length === 0
) {
return notFound()
}
const adultsParams = selectHotelParamsObject.room[0].adults // TODO: Handle multiple rooms
const childrenParams = selectHotelParamsObject.room[0].child
? generateChildrenString(selectHotelParamsObject.room[0].child)
: undefined // TODO: Handle multiple rooms
const searchDetails = await getHotelSearchDetails({ searchParams })
if (!searchDetails) return null
const { city, urlSearchParams, adultsInRoom, childrenInRoom } = searchDetails
const reservationParams = {
selectHotelParams,
selectHotelParams: urlSearchParams,
searchParams,
adultsParams,
childrenParams,
adultsInRoom,
childrenInRoom,
}
return (