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,17 +1,12 @@
import { notFound } from "next/navigation"
import { Suspense } from "react" import { Suspense } from "react"
import { getLocations } from "@/lib/trpc/memoizedRequests"
import { SelectHotelMapContainer } from "@/components/HotelReservation/SelectHotel/SelectHotelMap/SelectHotelMapContainer" import { SelectHotelMapContainer } from "@/components/HotelReservation/SelectHotel/SelectHotelMap/SelectHotelMapContainer"
import { SelectHotelMapContainerSkeleton } from "@/components/HotelReservation/SelectHotel/SelectHotelMap/SelectHotelMapContainerSkeleton" import { SelectHotelMapContainerSkeleton } from "@/components/HotelReservation/SelectHotel/SelectHotelMap/SelectHotelMapContainerSkeleton"
import {
generateChildrenString,
getHotelReservationQueryParams,
} from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
import { MapContainer } from "@/components/MapContainer" import { MapContainer } from "@/components/MapContainer"
import { setLang } from "@/i18n/serverContext" import { setLang } from "@/i18n/serverContext"
import { getHotelSearchDetails } from "../../utils"
import styles from "./page.module.css" import styles from "./page.module.css"
import type { SelectHotelSearchParams } from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams" import type { SelectHotelSearchParams } from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
@@ -22,24 +17,9 @@ export default async function SelectHotelMapPage({
searchParams, searchParams,
}: PageArgs<LangParams, SelectHotelSearchParams>) { }: PageArgs<LangParams, SelectHotelSearchParams>) {
setLang(params.lang) setLang(params.lang)
const locations = await getLocations() const searchDetails = await getHotelSearchDetails({ searchParams })
if (!searchDetails) return null
if (!locations || "error" in locations) { const { city, adultsInRoom, childrenInRoom } = searchDetails
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)
const adultsInRoom = selectHotelParamsObject.room[0].adults // TODO: Handle multiple rooms
const childrenInRoom = selectHotelParamsObject.room[0].child
? generateChildrenString(selectHotelParamsObject.room[0].child)
: undefined // TODO: Handle multiple rooms
return ( return (
<div className={styles.main}> <div className={styles.main}>

View File

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

View File

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

View File

@@ -0,0 +1,77 @@
import { notFound } from "next/navigation"
import { getLocations } from "@/lib/trpc/memoizedRequests"
import {
generateChildrenString,
getHotelReservationQueryParams,
} from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
import type { SelectHotelSearchParams } from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
import type {
Child,
SelectRateSearchParams,
} from "@/types/components/hotelReservation/selectRate/selectRate"
import type { Location } from "@/types/trpc/routers/hotel/locations"
interface HotelSearchDetails {
city: Location
hotel: Location
urlSearchParams?: URLSearchParams
adultsInRoom: number
childrenInRoom?: string
childrenInRoomArray?: Child[]
}
export async function getHotelSearchDetails({
searchParams,
}: {
searchParams:
| (SelectHotelSearchParams & {
[key: string]: string
})
| (SelectRateSearchParams & {
[key: string]: string
})
}): Promise<HotelSearchDetails | null> {
const locations = await getLocations()
if (!locations || "error" in locations) return null
const city = locations.data.find(
(location) =>
location.name.toLowerCase() === searchParams.city?.toLowerCase()
)
const hotel = locations.data.find(
(location) =>
"operaId" in location && location.operaId == searchParams.hotel
)
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
: undefined // TODO: Handle multiple rooms
}
return {
city: city!,
hotel: hotel!,
urlSearchParams,
adultsInRoom,
childrenInRoom,
childrenInRoomArray,
}
}

View File

@@ -34,7 +34,7 @@ export default async function SelectHotel({
params, params,
reservationParams, reservationParams,
}: SelectHotelProps) { }: SelectHotelProps) {
const { selectHotelParams, searchParams, adultsParams, childrenParams } = const { selectHotelParams, searchParams, adultsInRoom, childrenInRoom } =
reservationParams reservationParams
const intl = await getIntl() const intl = await getIntl()
@@ -44,8 +44,8 @@ export default async function SelectHotel({
cityId: city.id, cityId: city.id,
roomStayStartDate: searchParams.fromDate, roomStayStartDate: searchParams.fromDate,
roomStayEndDate: searchParams.toDate, roomStayEndDate: searchParams.toDate,
adults: adultsParams, adults: adultsInRoom,
children: childrenParams?.toString(), children: childrenInRoom,
}) })
) )

View File

@@ -1,7 +1,6 @@
import { Lang } from "@/constants/languages"
import type { CheckInData, Hotel, ParkingData } from "@/types/hotel" import type { CheckInData, Hotel, ParkingData } from "@/types/hotel"
import type { Location } from "@/types/trpc/routers/hotel/locations" import type { Location } from "@/types/trpc/routers/hotel/locations"
import type { Lang } from "@/constants/languages"
import type { SelectHotelSearchParams } from "./selectHotelSearchParams" import type { SelectHotelSearchParams } from "./selectHotelSearchParams"
export enum AvailabilityEnum { export enum AvailabilityEnum {
@@ -46,9 +45,9 @@ export interface SelectHotelProps {
lang: Lang lang: Lang
} }
reservationParams: { reservationParams: {
selectHotelParams: URLSearchParams selectHotelParams: URLSearchParams | undefined
searchParams: SelectHotelSearchParams searchParams: SelectHotelSearchParams
adultsParams: number adultsInRoom: number
childrenParams: string | undefined childrenInRoom: string | undefined
} }
} }