fix: unite all price details modals to one and align on ui
This commit is contained in:
committed by
Michael Zetterberg
parent
8152aea649
commit
1f94c581ae
@@ -0,0 +1,92 @@
|
||||
"use client"
|
||||
import { dt } from "@/lib/dt"
|
||||
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
|
||||
|
||||
import PriceDetailsModal from "@/components/HotelReservation/PriceDetailsModal"
|
||||
|
||||
import { mapToPrice } from "./mapToPrice"
|
||||
|
||||
import type { Price } from "@/types/components/hotelReservation/price"
|
||||
import { CurrencyEnum } from "@/types/enums/currency"
|
||||
|
||||
export default function PriceDetails() {
|
||||
const { bookingCode, currency, fromDate, rooms, vat, toDate } =
|
||||
useBookingConfirmationStore((state) => ({
|
||||
bookingCode: state.bookingCode ?? undefined,
|
||||
currency: state.currencyCode,
|
||||
fromDate: state.fromDate,
|
||||
rooms: state.rooms,
|
||||
toDate: state.toDate,
|
||||
vat: state.vat,
|
||||
}))
|
||||
|
||||
if (!rooms[0]) {
|
||||
return null
|
||||
}
|
||||
|
||||
const checkInDate = dt(fromDate).format("YYYY-MM-DD")
|
||||
const checkOutDate = dt(toDate).format("YYYY-MM-DD")
|
||||
const nights = dt(toDate)
|
||||
.startOf("day")
|
||||
.diff(dt(fromDate).startOf("day"), "days")
|
||||
|
||||
const totalPrice = rooms.reduce<Price>(
|
||||
(total, room) => {
|
||||
if (!room) {
|
||||
return total
|
||||
}
|
||||
|
||||
const pkgsSum =
|
||||
room.roomFeatures?.reduce((total, pkg) => total + pkg.totalPrice, 0) ??
|
||||
0
|
||||
|
||||
if (room.cheques) {
|
||||
// CorporateCheque Booking
|
||||
total.local.currency = CurrencyEnum.CC
|
||||
total.local.price = total.local.price + room.cheques
|
||||
} else if (room.roomPoints) {
|
||||
// Redemption Booking
|
||||
total.local.currency = CurrencyEnum.POINTS
|
||||
total.local.price = total.local.price + room.roomPoints
|
||||
} else if (room.vouchers) {
|
||||
// Vouchers Booking
|
||||
total.local.currency = CurrencyEnum.Voucher
|
||||
total.local.price = total.local.price + room.vouchers
|
||||
} else {
|
||||
// Price Booking
|
||||
total.local.price = total.local.price + room.roomPrice + pkgsSum
|
||||
}
|
||||
|
||||
if (
|
||||
(room.cheques || room.roomPoints || room.vouchers) &&
|
||||
room.roomPrice
|
||||
) {
|
||||
total.local.additionalPrice =
|
||||
(total.local.additionalPrice || 0) + room.roomPrice + pkgsSum
|
||||
total.local.additionalPriceCurrency = currency
|
||||
}
|
||||
|
||||
return total
|
||||
},
|
||||
{
|
||||
local: {
|
||||
currency,
|
||||
price: 0,
|
||||
},
|
||||
requested: undefined,
|
||||
}
|
||||
)
|
||||
|
||||
const mappedRooms = mapToPrice(rooms, nights)
|
||||
|
||||
return (
|
||||
<PriceDetailsModal
|
||||
bookingCode={bookingCode}
|
||||
fromDate={checkInDate}
|
||||
rooms={mappedRooms}
|
||||
toDate={checkOutDate}
|
||||
totalPrice={totalPrice}
|
||||
vat={vat}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import {
|
||||
breakfastPackageSchema,
|
||||
packageSchema,
|
||||
} from "@/server/routers/hotels/schemas/packages"
|
||||
|
||||
import { ChildBedMapEnum } from "@/types/components/bookingWidget/enums"
|
||||
import type { Package } from "@/types/requests/packages"
|
||||
import type { Room } from "@/types/stores/booking-confirmation"
|
||||
|
||||
export function mapToPrice(rooms: (Room | null)[], nights: number) {
|
||||
return rooms
|
||||
.filter((room): room is Room => !!room)
|
||||
.map((room) => {
|
||||
let price
|
||||
if (room.cheques) {
|
||||
price = {
|
||||
corporateCheque: {
|
||||
additionalPricePerStay: room.roomPrice ? room.roomPrice : undefined,
|
||||
currency: room.roomPrice ? room.currencyCode : undefined,
|
||||
numberOfCheques: room.cheques,
|
||||
},
|
||||
}
|
||||
} else if (room.roomPoints) {
|
||||
price = {
|
||||
redemption: {
|
||||
additionalPricePerStay: room.roomPrice ? room.roomPrice : undefined,
|
||||
currency: room.roomPrice ? room.currencyCode : undefined,
|
||||
pointsPerNight: room.roomPoints / nights,
|
||||
pointsPerStay: room.roomPoints,
|
||||
},
|
||||
}
|
||||
} else if (room.vouchers) {
|
||||
price = {
|
||||
voucher: {
|
||||
numberOfVouchers: room.vouchers,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
price = {
|
||||
regular: {
|
||||
currency: room.currencyCode,
|
||||
pricePerNight: room.roomPrice / nights,
|
||||
pricePerStay: room.roomPrice,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const breakfastPackage = breakfastPackageSchema.safeParse({
|
||||
code: room.breakfast?.code,
|
||||
description: room.breakfast?.description,
|
||||
localPrice: {
|
||||
currency: room.breakfast?.currency,
|
||||
price: room.breakfast?.unitPrice,
|
||||
totalPrice: room.breakfast?.totalPrice,
|
||||
},
|
||||
packageType: room.breakfast?.type,
|
||||
requestedPrice: {
|
||||
currency: room.breakfast?.currency,
|
||||
price: room.breakfast?.unitPrice,
|
||||
totalPrice: room.breakfast?.totalPrice,
|
||||
},
|
||||
})
|
||||
|
||||
const packages = room.roomFeatures
|
||||
?.map((featPkg) => {
|
||||
const pkg = packageSchema.safeParse({
|
||||
code: featPkg.code,
|
||||
description: featPkg.description,
|
||||
inventories: [],
|
||||
localPrice: {
|
||||
currency: featPkg.currency,
|
||||
price: featPkg.unitPrice,
|
||||
totalPrice: featPkg.totalPrice,
|
||||
},
|
||||
requestedPrice: {
|
||||
currency: featPkg.currency,
|
||||
price: featPkg.unitPrice,
|
||||
totalPrice: featPkg.totalPrice,
|
||||
},
|
||||
})
|
||||
if (pkg.success) {
|
||||
return pkg.data
|
||||
}
|
||||
return null
|
||||
})
|
||||
.filter((pkg): pkg is Package => !!pkg)
|
||||
|
||||
return {
|
||||
...room,
|
||||
adults: room.adults,
|
||||
bedType: {
|
||||
description: room.bedDescription,
|
||||
roomTypeCode: room.roomTypeCode || "",
|
||||
},
|
||||
breakfast: breakfastPackage.success ? breakfastPackage.data : undefined,
|
||||
breakfastIncluded: room.rateDefinition.breakfastIncluded,
|
||||
childrenInRoom: room.childrenAges?.map((age) => ({
|
||||
age,
|
||||
bed: ChildBedMapEnum.UNKNOWN,
|
||||
})),
|
||||
packages,
|
||||
price,
|
||||
roomType: room.name,
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user