From fce9ef1b6166fdf47a3ca54c47e1d18e7ee03425 Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Thu, 17 Oct 2024 10:10:28 +0200 Subject: [PATCH] feat(sw-452): updated typing on util --- .../hotelreservation/select-rate/page.tsx | 9 +++-- .../RoomSelection/FlexibilityOption/index.tsx | 4 +- .../SelectRate/RoomSelection/index.tsx | 2 +- .../SelectRate/RoomSelection/utils.ts | 37 +++++++++++-------- .../hotelReservation/selectRate/selectRate.ts | 2 +- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/app/[lang]/(live)/(public)/hotelreservation/select-rate/page.tsx b/app/[lang]/(live)/(public)/hotelreservation/select-rate/page.tsx index 4dd231574..44f3dbd15 100644 --- a/app/[lang]/(live)/(public)/hotelreservation/select-rate/page.tsx +++ b/app/[lang]/(live)/(public)/hotelreservation/select-rate/page.tsx @@ -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) { 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(), ]) diff --git a/components/HotelReservation/SelectRate/RoomSelection/FlexibilityOption/index.tsx b/components/HotelReservation/SelectRate/RoomSelection/FlexibilityOption/index.tsx index 30d5d5264..fc2b4d819 100644 --- a/components/HotelReservation/SelectRate/RoomSelection/FlexibilityOption/index.tsx +++ b/components/HotelReservation/SelectRate/RoomSelection/FlexibilityOption/index.tsx @@ -47,8 +47,8 @@ export default function FlexibilityOption({ function onChange() { const rate = { - roomTypeCode: roomTypeCode, - roomType: roomType, + roomTypeCode, + roomType, priceName: name, public: publicPrice, member: memberPrice, diff --git a/components/HotelReservation/SelectRate/RoomSelection/index.tsx b/components/HotelReservation/SelectRate/RoomSelection/index.tsx index efa7f2b3f..c4c5e2e87 100644 --- a/components/HotelReservation/SelectRate/RoomSelection/index.tsx +++ b/components/HotelReservation/SelectRate/RoomSelection/index.tsx @@ -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" diff --git a/components/HotelReservation/SelectRate/RoomSelection/utils.ts b/components/HotelReservation/SelectRate/RoomSelection/utils.ts index df8562504..e47a0da70 100644 --- a/components/HotelReservation/SelectRate/RoomSelection/utils.ts +++ b/components/HotelReservation/SelectRate/RoomSelection/utils.ts @@ -1,23 +1,28 @@ import { SelectRateSearchParams } from "@/types/components/hotelReservation/selectRate/selectRate" function getHotelReservationQueryParams(searchParams: URLSearchParams) { - const searchParamsObject: SelectRateSearchParams = Array.from( + const searchParamsObject: Record = 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>( + (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)[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 + }, acc) + return acc + }, + {} as Record + ) + return searchParamsObject as SelectRateSearchParams } -export { getHotelReservationQueryParams } +export default getHotelReservationQueryParams diff --git a/types/components/hotelReservation/selectRate/selectRate.ts b/types/components/hotelReservation/selectRate/selectRate.ts index f806223c6..58aa6fbbe 100644 --- a/types/components/hotelReservation/selectRate/selectRate.ts +++ b/types/components/hotelReservation/selectRate/selectRate.ts @@ -17,7 +17,7 @@ export interface SelectRateSearchParams { fromdate: string todate: string room: Room[] - [key: string]: any + [key: string]: string | string[] | Room[] } export interface Rate {