Merged in feat/SW-964-Sticky-summary-multiroom (pull request #1231)
Feat/SW-964 Sticky summary multiroom (UX) * feat(SW-964) Multiroom support for summary in select-rate * feat(SW-964) added utils for calculateTotalPrice * feat(SW-964) Removed duplicated code Approved-by: Tobias Johansson
This commit is contained in:
58
components/HotelReservation/SelectRate/RateSummary/utils.ts
Normal file
58
components/HotelReservation/SelectRate/RateSummary/utils.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import {
|
||||
type RoomPackage,
|
||||
RoomPackageCodeEnum,
|
||||
} from "@/types/components/hotelReservation/selectRate/roomFilter"
|
||||
import type { Rate } from "@/types/components/hotelReservation/selectRate/selectRate"
|
||||
|
||||
interface TotalPrice {
|
||||
localPrice: { currency: string; price: number }
|
||||
requestedPrice?: { currency: string; price: number }
|
||||
}
|
||||
|
||||
export const calculateTotalPrice = (
|
||||
selectedRateSummary: Rate[],
|
||||
isUserLoggedIn: boolean,
|
||||
petRoomPackage: RoomPackage | undefined
|
||||
) => {
|
||||
return selectedRateSummary.reduce<TotalPrice>(
|
||||
(total, room) => {
|
||||
const priceToUse =
|
||||
isUserLoggedIn && room.member ? room.member : room.public
|
||||
const isPetRoom = room.features.some(
|
||||
(feature) => feature.code === RoomPackageCodeEnum.PET_ROOM
|
||||
)
|
||||
const petRoomPrice =
|
||||
isPetRoom && petRoomPackage
|
||||
? isUserLoggedIn
|
||||
? Number(petRoomPackage.localPrice.totalPrice || 0)
|
||||
: Number(petRoomPackage.requestedPrice.totalPrice || 0)
|
||||
: 0
|
||||
|
||||
return {
|
||||
localPrice: {
|
||||
currency: priceToUse.localPrice.currency,
|
||||
price:
|
||||
total.localPrice.price +
|
||||
priceToUse.localPrice.pricePerStay +
|
||||
petRoomPrice,
|
||||
},
|
||||
requestedPrice: priceToUse.requestedPrice
|
||||
? {
|
||||
currency: priceToUse.requestedPrice.currency,
|
||||
price:
|
||||
(total.requestedPrice?.price ?? 0) +
|
||||
priceToUse.requestedPrice.pricePerStay +
|
||||
petRoomPrice,
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
},
|
||||
{
|
||||
localPrice: {
|
||||
currency: selectedRateSummary[0].public.localPrice.currency,
|
||||
price: 0,
|
||||
},
|
||||
requestedPrice: undefined,
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user