From dcd45ee31d61c2d3681671e008b9f6322317dfbe Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Fri, 7 Feb 2025 08:31:40 +0100 Subject: [PATCH] feat(SW-717) make reduce more understandable --- .../SelectRate/HotelInfoCard/NoRoomsAlert.tsx | 21 ++++------------- .../SelectRate/HotelInfoCard/utils.ts | 23 +++++++++++++++++++ .../SelectRate/Rooms/utils.ts | 15 ++++++++---- 3 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 components/HotelReservation/SelectRate/HotelInfoCard/utils.ts diff --git a/components/HotelReservation/SelectRate/HotelInfoCard/NoRoomsAlert.tsx b/components/HotelReservation/SelectRate/HotelInfoCard/NoRoomsAlert.tsx index d3edeb373..81ff62b02 100644 --- a/components/HotelReservation/SelectRate/HotelInfoCard/NoRoomsAlert.tsx +++ b/components/HotelReservation/SelectRate/HotelInfoCard/NoRoomsAlert.tsx @@ -6,6 +6,7 @@ import { getIntl } from "@/i18n" import { safeTry } from "@/utils/safeTry" import { generateChildrenString } from "../../utils" +import { combineRoomAvailabilities } from "./utils" import styles from "./NoRoomsAlert.module.css" @@ -51,23 +52,9 @@ export async function NoRoomsAlert({ const roomsAvailabilityResults = await Promise.all(roomsAvailabilityPromises) - const roomsAvailability = roomsAvailabilityResults.reduce< - (typeof roomsAvailabilityResults)[0][0] - >((acc, [result, error]) => { - if (error) { - console.error("[RoomsContainer] unable to fetch room availability") - return acc - } - if (!acc) return result - if (!result) return acc - return { - ...result, - roomConfigurations: [ - ...acc.roomConfigurations, - ...result.roomConfigurations, - ], - } - }, null) + const roomsAvailability = combineRoomAvailabilities({ + availabilityResults: roomsAvailabilityResults, + }) if (!roomsAvailability) { return null diff --git a/components/HotelReservation/SelectRate/HotelInfoCard/utils.ts b/components/HotelReservation/SelectRate/HotelInfoCard/utils.ts new file mode 100644 index 000000000..af24c3e66 --- /dev/null +++ b/components/HotelReservation/SelectRate/HotelInfoCard/utils.ts @@ -0,0 +1,23 @@ +import type { RoomsAvailability } from "@/server/routers/hotels/output" + +export function combineRoomAvailabilities({ + availabilityResults, +}: { + availabilityResults: Array<[RoomsAvailability | undefined | null, unknown]> +}): RoomsAvailability | null { + return availabilityResults.reduce( + (combinedResult, [currentResult, error]) => { + if (error || !currentResult) return combinedResult + if (!combinedResult) return currentResult + + return { + ...currentResult, + roomConfigurations: [ + ...combinedResult.roomConfigurations, + ...currentResult.roomConfigurations, + ], + } + }, + null + ) +} diff --git a/components/HotelReservation/SelectRate/Rooms/utils.ts b/components/HotelReservation/SelectRate/Rooms/utils.ts index fa9b1ac0e..fdedec453 100644 --- a/components/HotelReservation/SelectRate/Rooms/utils.ts +++ b/components/HotelReservation/SelectRate/Rooms/utils.ts @@ -8,12 +8,17 @@ import type { RoomConfiguration } from "@/types/trpc/routers/hotel/roomAvailabil export function filterDuplicateRoomTypesByLowestPrice( roomConfigurations: RoomConfiguration[] ): RoomConfiguration[] { - const roomTypeCount = roomConfigurations.reduce( - (acc, room) => { - acc[room.roomType] = (acc[room.roomType] || 0) + 1 - return acc + const roomTypeCount = roomConfigurations.reduce>( + (roomTypeTally, currentRoom) => { + const currentRoomType = currentRoom.roomType + const currentCount = roomTypeTally[currentRoomType] || 0 + + return { + ...roomTypeTally, + [currentRoomType]: currentCount + 1, + } }, - {} as Record + {} ) const duplicateRoomTypes = new Set(