refactor: url management in hotel reservation flow
This commit is contained in:
@@ -2,10 +2,8 @@ import { notFound } from "next/navigation"
|
||||
|
||||
import { getLocations } from "@/lib/trpc/memoizedRequests"
|
||||
|
||||
import {
|
||||
generateChildrenString,
|
||||
getHotelReservationQueryParams,
|
||||
} from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
|
||||
import { generateChildrenString } from "@/components/HotelReservation/utils"
|
||||
import { convertSearchParamsToObj } from "@/utils/url"
|
||||
|
||||
import type { SelectHotelSearchParams } from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
|
||||
import type {
|
||||
@@ -14,62 +12,59 @@ import type {
|
||||
} from "@/types/components/hotelReservation/selectRate/selectRate"
|
||||
import type { Location } from "@/types/trpc/routers/hotel/locations"
|
||||
|
||||
interface HotelSearchDetails {
|
||||
interface HotelSearchDetails<T> {
|
||||
city: Location | null
|
||||
hotel: Location | null
|
||||
urlSearchParams?: URLSearchParams
|
||||
selectHotelParams: T
|
||||
adultsInRoom: number
|
||||
childrenInRoom?: string
|
||||
childrenInRoomArray?: Child[]
|
||||
}
|
||||
|
||||
export async function getHotelSearchDetails({
|
||||
export async function getHotelSearchDetails<
|
||||
T extends SelectHotelSearchParams | SelectRateSearchParams,
|
||||
>({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams:
|
||||
| (SelectHotelSearchParams & {
|
||||
[key: string]: string
|
||||
})
|
||||
| (SelectRateSearchParams & {
|
||||
[key: string]: string
|
||||
})
|
||||
}): Promise<HotelSearchDetails | null> {
|
||||
searchParams: T & {
|
||||
[key: string]: string
|
||||
}
|
||||
}): Promise<HotelSearchDetails<T> | null> {
|
||||
const selectHotelParams = convertSearchParamsToObj<T>(searchParams)
|
||||
|
||||
const locations = await getLocations()
|
||||
|
||||
if (!locations || "error" in locations) return null
|
||||
|
||||
const city = locations.data.find(
|
||||
(location) =>
|
||||
location.name.toLowerCase() === searchParams.city?.toLowerCase()
|
||||
location.name.toLowerCase() === selectHotelParams.city?.toLowerCase()
|
||||
)
|
||||
const hotel = locations.data.find(
|
||||
(location) =>
|
||||
"operaId" in location && location.operaId == searchParams.hotel
|
||||
"operaId" in location && location.operaId == selectHotelParams.hotelId
|
||||
)
|
||||
|
||||
if (!city && !hotel) return notFound()
|
||||
|
||||
const urlSearchParams = new URLSearchParams(searchParams)
|
||||
const searchParamsObject = getHotelReservationQueryParams(urlSearchParams)
|
||||
|
||||
let adultsInRoom = 1
|
||||
let childrenInRoom: string | undefined = undefined
|
||||
let childrenInRoomArray: Child[] | undefined = undefined
|
||||
|
||||
if (searchParamsObject.room && searchParamsObject.room.length > 0) {
|
||||
adultsInRoom = searchParamsObject.room[0].adults // TODO: Handle multiple rooms
|
||||
childrenInRoom = searchParamsObject.room[0].child
|
||||
? generateChildrenString(searchParamsObject.room[0].child)
|
||||
: undefined // TODO: Handle multiple rooms
|
||||
childrenInRoomArray = searchParamsObject.room[0].child
|
||||
? searchParamsObject.room[0].child
|
||||
const { rooms } = selectHotelParams
|
||||
|
||||
if (rooms && rooms.length > 0) {
|
||||
adultsInRoom = rooms[0].adults // TODO: Handle multiple rooms
|
||||
childrenInRoom = rooms[0].children
|
||||
? generateChildrenString(rooms[0].children)
|
||||
: undefined // TODO: Handle multiple rooms
|
||||
childrenInRoomArray = rooms[0].children ? rooms[0].children : undefined // TODO: Handle multiple rooms
|
||||
}
|
||||
|
||||
return {
|
||||
city: city ?? null,
|
||||
hotel: hotel ?? null,
|
||||
urlSearchParams,
|
||||
selectHotelParams,
|
||||
adultsInRoom,
|
||||
childrenInRoom,
|
||||
childrenInRoomArray,
|
||||
|
||||
Reference in New Issue
Block a user