feat: SW-2079 Show points in confirmation page * feat: SW-2079 Show points in confirmation page * feat: SW-2079 Optimized code * feat: SW-2079 Updated Body to Typography * feat: SW-2079 Multi-room total cost display * feat: SW-2079 Add reward nights condition rate title * feat: SW-2079 Removed extra checks * feat: SW-2079 Optimmized formatPrice function * feat: SW-2079 Typo fix Approved-by: Christian Andolf
68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
"use client"
|
|
|
|
import { useRef } from "react"
|
|
import { useIntl } from "react-intl"
|
|
|
|
import { createBookingConfirmationStore } from "@/stores/booking-confirmation"
|
|
|
|
import { BookingConfirmationContext } from "@/contexts/BookingConfirmation"
|
|
import { formatPrice } from "@/utils/numberFormatting"
|
|
|
|
import type { BookingConfirmationStore } from "@/types/contexts/booking-confirmation"
|
|
import { CurrencyEnum } from "@/types/enums/currency"
|
|
import type { BookingConfirmationProviderProps } from "@/types/providers/booking-confirmation"
|
|
|
|
export default function BookingConfirmationProvider({
|
|
bookingCode,
|
|
children,
|
|
currencyCode,
|
|
fromDate,
|
|
toDate,
|
|
rooms,
|
|
vat,
|
|
}: BookingConfirmationProviderProps) {
|
|
const intl = useIntl()
|
|
const storeRef = useRef<BookingConfirmationStore>()
|
|
|
|
if (!storeRef.current) {
|
|
const totalBookingPrice = rooms.reduce((acc, room) => {
|
|
const reservationTotalPrice = room?.totalPrice || 0
|
|
return acc + reservationTotalPrice
|
|
}, 0)
|
|
let formattedTotalCost = formatPrice(intl, totalBookingPrice, currencyCode)
|
|
const totalBookingPoints = rooms.reduce((acc, room) => {
|
|
return acc + (room?.roomPoints ?? 0)
|
|
}, 0)
|
|
let isVatCurrency = true
|
|
if (totalBookingPoints) {
|
|
isVatCurrency = false
|
|
formattedTotalCost = formatPrice(
|
|
intl,
|
|
totalBookingPoints,
|
|
CurrencyEnum.POINTS,
|
|
totalBookingPrice,
|
|
currencyCode
|
|
)
|
|
}
|
|
const initialData = {
|
|
bookingCode,
|
|
currencyCode,
|
|
fromDate,
|
|
toDate,
|
|
rooms,
|
|
vat,
|
|
isVatCurrency,
|
|
formattedTotalCost,
|
|
totalBookingPrice,
|
|
}
|
|
|
|
storeRef.current = createBookingConfirmationStore(initialData)
|
|
}
|
|
|
|
return (
|
|
<BookingConfirmationContext.Provider value={storeRef.current}>
|
|
{children}
|
|
</BookingConfirmationContext.Provider>
|
|
)
|
|
}
|