feat(SW-717) make reduce more understandable
This commit is contained in:
@@ -6,6 +6,7 @@ import { getIntl } from "@/i18n"
|
|||||||
import { safeTry } from "@/utils/safeTry"
|
import { safeTry } from "@/utils/safeTry"
|
||||||
|
|
||||||
import { generateChildrenString } from "../../utils"
|
import { generateChildrenString } from "../../utils"
|
||||||
|
import { combineRoomAvailabilities } from "./utils"
|
||||||
|
|
||||||
import styles from "./NoRoomsAlert.module.css"
|
import styles from "./NoRoomsAlert.module.css"
|
||||||
|
|
||||||
@@ -51,23 +52,9 @@ export async function NoRoomsAlert({
|
|||||||
|
|
||||||
const roomsAvailabilityResults = await Promise.all(roomsAvailabilityPromises)
|
const roomsAvailabilityResults = await Promise.all(roomsAvailabilityPromises)
|
||||||
|
|
||||||
const roomsAvailability = roomsAvailabilityResults.reduce<
|
const roomsAvailability = combineRoomAvailabilities({
|
||||||
(typeof roomsAvailabilityResults)[0][0]
|
availabilityResults: roomsAvailabilityResults,
|
||||||
>((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)
|
|
||||||
|
|
||||||
if (!roomsAvailability) {
|
if (!roomsAvailability) {
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -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<RoomsAvailability | null>(
|
||||||
|
(combinedResult, [currentResult, error]) => {
|
||||||
|
if (error || !currentResult) return combinedResult
|
||||||
|
if (!combinedResult) return currentResult
|
||||||
|
|
||||||
|
return {
|
||||||
|
...currentResult,
|
||||||
|
roomConfigurations: [
|
||||||
|
...combinedResult.roomConfigurations,
|
||||||
|
...currentResult.roomConfigurations,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
null
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -8,12 +8,17 @@ import type { RoomConfiguration } from "@/types/trpc/routers/hotel/roomAvailabil
|
|||||||
export function filterDuplicateRoomTypesByLowestPrice(
|
export function filterDuplicateRoomTypesByLowestPrice(
|
||||||
roomConfigurations: RoomConfiguration[]
|
roomConfigurations: RoomConfiguration[]
|
||||||
): RoomConfiguration[] {
|
): RoomConfiguration[] {
|
||||||
const roomTypeCount = roomConfigurations.reduce(
|
const roomTypeCount = roomConfigurations.reduce<Record<string, number>>(
|
||||||
(acc, room) => {
|
(roomTypeTally, currentRoom) => {
|
||||||
acc[room.roomType] = (acc[room.roomType] || 0) + 1
|
const currentRoomType = currentRoom.roomType
|
||||||
return acc
|
const currentCount = roomTypeTally[currentRoomType] || 0
|
||||||
|
|
||||||
|
return {
|
||||||
|
...roomTypeTally,
|
||||||
|
[currentRoomType]: currentCount + 1,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{} as Record<string, number>
|
{}
|
||||||
)
|
)
|
||||||
|
|
||||||
const duplicateRoomTypes = new Set(
|
const duplicateRoomTypes = new Set(
|
||||||
|
|||||||
Reference in New Issue
Block a user