feat(SW-718): parse room from searchParams
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import type { RoomParam } from "@/types/components/hotelReservation/selectRate/section"
|
||||
import type { RoomConfiguration } from "@/server/routers/hotels/output"
|
||||
|
||||
/**
|
||||
@@ -103,3 +104,54 @@ export function filterDuplicateRoomTypesByLowestPrice(
|
||||
|
||||
return Array.from(roomMap.values())
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the room params from the search params.
|
||||
*/
|
||||
|
||||
export function parseRoomParams(searchParams: URLSearchParams): RoomParam[] {
|
||||
const rooms: RoomParam[] = []
|
||||
|
||||
// Collect all param keys and sort them to ensure correct order
|
||||
const paramKeys = Array.from(searchParams.keys()).sort()
|
||||
|
||||
for (const key of paramKeys) {
|
||||
const roomRegex = /^room\[(\d+)\]\.(.+)/
|
||||
const roomMatch = roomRegex.exec(key)
|
||||
if (!roomMatch) continue
|
||||
|
||||
const [, roomIndex, param] = roomMatch
|
||||
const value = searchParams.get(key)
|
||||
if (!value) continue
|
||||
|
||||
// Initialize room if it doesn't exist
|
||||
if (!rooms[Number(roomIndex)]) rooms[Number(roomIndex)] = { adults: 1 }
|
||||
|
||||
// Handle adults
|
||||
if (param === "adults") {
|
||||
rooms[Number(roomIndex)].adults = Number(value)
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle children
|
||||
const childRegex = /child\[(\d+)\]\.(.+)/
|
||||
const childMatch = childRegex.exec(param)
|
||||
|
||||
if (childMatch) {
|
||||
const [, childIndex, childParam] = childMatch
|
||||
const room = rooms[Number(roomIndex)]
|
||||
if (!room.children) room.children = []
|
||||
|
||||
// Set child properties
|
||||
if (childParam === "age" && room.children) {
|
||||
room.children[Number(childIndex)] = {
|
||||
age: Number(value),
|
||||
bed: Number(value),
|
||||
}
|
||||
} else if (childParam === "bed" && room.children?.[Number(childIndex)])
|
||||
room.children[Number(childIndex)].bed = Number(value)
|
||||
}
|
||||
}
|
||||
|
||||
return rooms
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user