Merged in fix/refactor-booking-flow-search-params (pull request #2148)
Fix: refactor booking flow search params * wip: apply codemod and upgrade swc plugin * wip: design-system to react 19, fix issues from async (search)params * Prepare new parse function for booking flow search params * Prepare serialize function for booking flow search params * Improve handling of comma separated arrays * Slightly refactor for readability * Next abstracts URLSearchParams so handle the abstraction instead * Refactor booking widget to use new search params parsing * Rename search param functions * Refactor select-hotel to use new search param parser * Use new search params parser in select-rate and details * Fix hotelId type * Avoid passing down search params into BookingWidget components * More updates to use new types instead of SearchParams<T> * Remove types SelectHotelSearchParams and AlternativeSelectHotelSearchParams * Fix parseBookingWidgetSearchParams return type * Add error handling to booking search param parsers * Fix modifyRateIndex handling in details page * Clean up * Refactor booking widget search param serializing to util function * Move start page booking widget search param parsing to page * Use new search param serializer in HandleErrorCallback * Delete convertSearchParamsToObj & convertObjToSearchParams Approved-by: Michael Zetterberg
This commit is contained in:
@@ -5,16 +5,9 @@ import { getLocations } from "@/lib/trpc/memoizedRequests"
|
||||
|
||||
import { generateChildrenString } from "@/components/HotelReservation/utils"
|
||||
import { safeTry } from "@/utils/safeTry"
|
||||
import { type SelectHotelParams } from "@/utils/url"
|
||||
|
||||
import type {
|
||||
AlternativeHotelsSearchParams,
|
||||
SelectHotelSearchParams,
|
||||
} from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
|
||||
import type {
|
||||
Child,
|
||||
SelectRateSearchParams,
|
||||
} from "@/types/components/hotelReservation/selectRate/selectRate"
|
||||
import type { BookingSearchType } from "@/types/components/hotelReservation/booking"
|
||||
import type { Child } from "@/types/components/hotelReservation/selectRate/selectRate"
|
||||
import {
|
||||
type HotelLocation,
|
||||
isHotelLocation,
|
||||
@@ -24,41 +17,44 @@ import {
|
||||
export type ChildrenInRoom = (Child[] | null)[] | null
|
||||
export type ChildrenInRoomString = (string | null)[] | null
|
||||
|
||||
interface HotelSearchDetails<T> {
|
||||
interface HotelSearchDetails {
|
||||
adultsInRoom: number[]
|
||||
bookingCode?: string
|
||||
childrenInRoom: ChildrenInRoom
|
||||
childrenInRoomString: ChildrenInRoomString
|
||||
city: Location | null
|
||||
cityName: string | undefined
|
||||
hotel: HotelLocation | null
|
||||
noOfRooms: number
|
||||
redemption?: boolean
|
||||
selectHotelParams: SelectHotelParams<T> & { city: string | undefined }
|
||||
}
|
||||
|
||||
export async function getHotelSearchDetails<
|
||||
T extends
|
||||
| SelectHotelSearchParams
|
||||
| SelectRateSearchParams
|
||||
| AlternativeHotelsSearchParams,
|
||||
>(
|
||||
selectHotelParams: SelectHotelParams<T>,
|
||||
export async function getHotelSearchDetails(
|
||||
params: {
|
||||
hotelId?: string
|
||||
city?: string
|
||||
rooms?: {
|
||||
adults: number
|
||||
childrenInRoom?: Child[]
|
||||
}[]
|
||||
bookingCode?: string
|
||||
searchType?: BookingSearchType
|
||||
},
|
||||
isAlternativeHotels?: boolean
|
||||
): Promise<HotelSearchDetails<T> | null> {
|
||||
): Promise<HotelSearchDetails | null> {
|
||||
const [locations, error] = await safeTry(getLocations())
|
||||
if (!locations || error) {
|
||||
return null
|
||||
}
|
||||
|
||||
const hotel =
|
||||
("hotelId" in selectHotelParams &&
|
||||
(locations.find(
|
||||
const hotel = params.hotelId
|
||||
? ((locations.find(
|
||||
(location) =>
|
||||
isHotelLocation(location) &&
|
||||
"operaId" in location &&
|
||||
location.operaId === selectHotelParams.hotelId
|
||||
) as HotelLocation | undefined)) ||
|
||||
null
|
||||
location.operaId === params.hotelId
|
||||
) as HotelLocation | undefined) ?? null)
|
||||
: null
|
||||
|
||||
if (isAlternativeHotels && !hotel) {
|
||||
return notFound()
|
||||
@@ -66,16 +62,13 @@ export async function getHotelSearchDetails<
|
||||
|
||||
const cityName = isAlternativeHotels
|
||||
? hotel?.relationships.city.name
|
||||
: "city" in selectHotelParams
|
||||
? (selectHotelParams.city as string | undefined)
|
||||
: undefined
|
||||
: params.city
|
||||
|
||||
const city =
|
||||
(typeof cityName === "string" &&
|
||||
locations.find(
|
||||
const city = cityName
|
||||
? (locations.find(
|
||||
(location) => location.name.toLowerCase() === cityName.toLowerCase()
|
||||
)) ||
|
||||
null
|
||||
) ?? null)
|
||||
: null
|
||||
|
||||
if (!city && !hotel) return notFound()
|
||||
if (isAlternativeHotels && (!city || !hotel)) return notFound()
|
||||
@@ -84,7 +77,7 @@ export async function getHotelSearchDetails<
|
||||
let childrenInRoom: ChildrenInRoom = null
|
||||
let childrenInRoomString: ChildrenInRoomString = null
|
||||
|
||||
const { rooms } = selectHotelParams
|
||||
const { rooms } = params
|
||||
|
||||
if (rooms?.length) {
|
||||
adultsInRoom = rooms.map((room) => room.adults ?? 0)
|
||||
@@ -97,13 +90,13 @@ export async function getHotelSearchDetails<
|
||||
|
||||
return {
|
||||
adultsInRoom,
|
||||
bookingCode: selectHotelParams.bookingCode ?? undefined,
|
||||
bookingCode: params.bookingCode ?? undefined,
|
||||
childrenInRoom,
|
||||
childrenInRoomString,
|
||||
city,
|
||||
cityName,
|
||||
hotel,
|
||||
noOfRooms: rooms?.length ?? 0,
|
||||
redemption: selectHotelParams.searchType === REDEMPTION,
|
||||
selectHotelParams: { city: cityName, ...selectHotelParams },
|
||||
redemption: params.searchType === REDEMPTION,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user