feat(sw-452): updated typing on util
This commit is contained in:
@@ -2,6 +2,7 @@ import { getProfileSafely } from "@/lib/trpc/memoizedRequests"
|
||||
import { serverClient } from "@/lib/trpc/server"
|
||||
|
||||
import RoomSelection from "@/components/HotelReservation/SelectRate/RoomSelection"
|
||||
import getHotelReservationQueryParams from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
|
||||
import { setLang } from "@/i18n/serverContext"
|
||||
|
||||
import styles from "./page.module.css"
|
||||
@@ -15,8 +16,10 @@ export default async function SelectRatePage({
|
||||
}: PageArgs<LangParams & { section: string }, SelectRateSearchParams>) {
|
||||
setLang(params.lang)
|
||||
|
||||
const roomParams = new URLSearchParams(searchParams)
|
||||
const adults = roomParams.get("room[0].adults") || "1" // Default to '1' if not found
|
||||
const selecetRoomParams = new URLSearchParams(searchParams)
|
||||
const selecetRoomParamsObject =
|
||||
getHotelReservationQueryParams(selecetRoomParams)
|
||||
const adults = selecetRoomParamsObject.room[0].adults // TODO: Handle multiple rooms
|
||||
|
||||
const [hotelData, roomConfigurations, user] = await Promise.all([
|
||||
serverClient().hotel.hotelData.get({
|
||||
@@ -28,7 +31,7 @@ export default async function SelectRatePage({
|
||||
hotelId: parseInt(searchParams.hotel, 10),
|
||||
roomStayStartDate: searchParams.fromDate,
|
||||
roomStayEndDate: searchParams.toDate,
|
||||
adults: parseInt(adults, 10),
|
||||
adults: adults,
|
||||
}),
|
||||
getProfileSafely(),
|
||||
])
|
||||
|
||||
@@ -47,8 +47,8 @@ export default function FlexibilityOption({
|
||||
|
||||
function onChange() {
|
||||
const rate = {
|
||||
roomTypeCode: roomTypeCode,
|
||||
roomType: roomType,
|
||||
roomTypeCode,
|
||||
roomType,
|
||||
priceName: name,
|
||||
public: publicPrice,
|
||||
member: memberPrice,
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useState } from "react"
|
||||
|
||||
import RateSummary from "./RateSummary"
|
||||
import RoomCard from "./RoomCard"
|
||||
import { getHotelReservationQueryParams } from "./utils"
|
||||
import getHotelReservationQueryParams from "./utils"
|
||||
|
||||
import styles from "./roomSelection.module.css"
|
||||
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
import { SelectRateSearchParams } from "@/types/components/hotelReservation/selectRate/selectRate"
|
||||
|
||||
function getHotelReservationQueryParams(searchParams: URLSearchParams) {
|
||||
const searchParamsObject: SelectRateSearchParams = Array.from(
|
||||
const searchParamsObject: Record<string, unknown> = Array.from(
|
||||
searchParams.entries()
|
||||
).reduce((acc, [key, value]) => {
|
||||
const keys = key.replace(/\]/g, "").split(/\[|\./) // Split keys by '[' or '.'
|
||||
keys.reduce((nestedAcc, k, i) => {
|
||||
if (i === keys.length - 1) {
|
||||
nestedAcc[k] = value // Assign value at the last key
|
||||
} else {
|
||||
if (!nestedAcc[k]) {
|
||||
nestedAcc[k] = isNaN(Number(keys[i + 1])) ? {} : [] // Initialize as array or object
|
||||
).reduce<Record<string, unknown>>(
|
||||
(acc, [key, value]) => {
|
||||
const keys = key.replace(/\]/g, "").split(/\[|\./) // Split keys by '[' or '.'
|
||||
keys.reduce((nestedAcc, k, i) => {
|
||||
if (i === keys.length - 1) {
|
||||
// Convert value to number if the key is 'adults' or 'age'
|
||||
;(nestedAcc as Record<string, unknown>)[k] =
|
||||
k === "adults" || k === "age" ? Number(value) : value
|
||||
} else {
|
||||
if (!nestedAcc[k]) {
|
||||
nestedAcc[k] = isNaN(Number(keys[i + 1])) ? {} : [] // Initialize as object or array
|
||||
}
|
||||
}
|
||||
}
|
||||
return nestedAcc[k]
|
||||
}, acc)
|
||||
return acc
|
||||
}, {} as SelectRateSearchParams)
|
||||
return searchParamsObject
|
||||
return nestedAcc[k] as Record<string, unknown>
|
||||
}, acc)
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, unknown>
|
||||
)
|
||||
return searchParamsObject as SelectRateSearchParams
|
||||
}
|
||||
|
||||
export { getHotelReservationQueryParams }
|
||||
export default getHotelReservationQueryParams
|
||||
|
||||
@@ -17,7 +17,7 @@ export interface SelectRateSearchParams {
|
||||
fromdate: string
|
||||
todate: string
|
||||
room: Room[]
|
||||
[key: string]: any
|
||||
[key: string]: string | string[] | Room[]
|
||||
}
|
||||
|
||||
export interface Rate {
|
||||
|
||||
Reference in New Issue
Block a user