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 { 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
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user