This creates the alternative hotels page. It is mostly a copy of the select hotel page, and most of the contents of the pages lives under the same component in /components.
Merged in feat/sw-397-alternative-hotels (pull request #1211) Feat/sw 397 alternative hotels * fix(SW-397): create alternative hotels page * update types * Adapt to new changes for fetching data * Make bookingcode optional * Code review fixes Approved-by: Simon.Emanuelsson
This commit is contained in:
12
utils/url.ts
12
utils/url.ts
@@ -29,6 +29,10 @@ const keyedSearchParams = new Map([
|
||||
["child", "childrenInRoom"],
|
||||
])
|
||||
|
||||
export type SelectHotelParams<T> = Omit<T, "hotel"> & {
|
||||
hotelId: string
|
||||
} & PartialRoom
|
||||
|
||||
export function getKeyFromSearchParam(key: string): string {
|
||||
return keyedSearchParams.get(key) || key
|
||||
}
|
||||
@@ -46,12 +50,12 @@ export function convertSearchParamsToObj<T extends PartialRoom>(
|
||||
searchParams: Record<string, string>
|
||||
) {
|
||||
const searchParamsObject = Object.entries(searchParams).reduce<
|
||||
T & PartialRoom
|
||||
SelectHotelParams<T>
|
||||
>((acc, [key, value]) => {
|
||||
// The params are sometimes indexed with a number (for ex: `room[0].adults`),
|
||||
// so we need to split them by . or []
|
||||
const keys = key.replace(/\]/g, "").split(/\[|\./)
|
||||
const firstKey = getKeyFromSearchParam(keys[0]) as keyof T
|
||||
const firstKey = getKeyFromSearchParam(keys[0])
|
||||
|
||||
// Room is a special case since it is an array, so we need to handle it separately
|
||||
if (firstKey === "rooms") {
|
||||
@@ -92,11 +96,11 @@ export function convertSearchParamsToObj<T extends PartialRoom>(
|
||||
roomObject[index][roomObjectKey] = value
|
||||
}
|
||||
} else {
|
||||
acc[firstKey] = value as T[keyof T]
|
||||
return { ...acc, [firstKey]: value }
|
||||
}
|
||||
|
||||
return acc
|
||||
}, {} as T)
|
||||
}, {} as SelectHotelParams<T>)
|
||||
|
||||
return searchParamsObject
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user