Merged in chore/SW-3395-move-bookingconfirmationprovider (pull request #2757)
chore(SW-3395): Moved BookingConfirmationProvider to booking-flow package * chore(SW-3395): Moved BookingConfirmationProvider to booking-flow package Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import { createContext } from "react"
|
||||
|
||||
import type { BookingConfirmationStore } from "../types/contexts/booking-confirmation"
|
||||
|
||||
export const BookingConfirmationContext =
|
||||
createContext<BookingConfirmationStore | null>(null)
|
||||
@@ -0,0 +1,110 @@
|
||||
"use client"
|
||||
|
||||
import { useRef } from "react"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
||||
import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting"
|
||||
|
||||
import { BookingConfirmationContext } from "../contexts/BookingConfirmation"
|
||||
import { createBookingConfirmationStore } from "../stores/booking-confirmation"
|
||||
|
||||
import type { BookingConfirmationStore } from "../types/contexts/booking-confirmation"
|
||||
import type { BookingConfirmationProviderProps } from "../types/providers/booking-confirmation"
|
||||
|
||||
export default function BookingConfirmationProvider({
|
||||
bookingCode,
|
||||
children,
|
||||
currencyCode,
|
||||
fromDate,
|
||||
toDate,
|
||||
roomCategories,
|
||||
rooms,
|
||||
vat,
|
||||
}: BookingConfirmationProviderProps) {
|
||||
const intl = useIntl()
|
||||
const storeRef = useRef<BookingConfirmationStore>(undefined)
|
||||
|
||||
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)
|
||||
const totalBookingCheques = rooms.reduce((acc, room) => {
|
||||
return acc + (room?.cheques ?? 0)
|
||||
}, 0)
|
||||
const totalBookingVouchers = rooms.reduce((acc, room) => {
|
||||
return acc + (room?.vouchers ?? 0)
|
||||
}, 0)
|
||||
|
||||
let isVatCurrency = true
|
||||
if (totalBookingPoints) {
|
||||
isVatCurrency = false
|
||||
formattedTotalCost = formatPrice(
|
||||
intl,
|
||||
totalBookingPoints,
|
||||
CurrencyEnum.POINTS,
|
||||
totalBookingPrice,
|
||||
currencyCode
|
||||
)
|
||||
} else if (totalBookingCheques) {
|
||||
isVatCurrency = false
|
||||
formattedTotalCost = formatPrice(
|
||||
intl,
|
||||
totalBookingCheques,
|
||||
CurrencyEnum.CC,
|
||||
totalBookingPrice,
|
||||
currencyCode
|
||||
)
|
||||
} else if (totalBookingVouchers) {
|
||||
const room = rooms?.[0]
|
||||
if (room?.packages) {
|
||||
const pkgsSum = room.packages.reduce(
|
||||
(total, pkg) => total + pkg.totalPrice,
|
||||
0
|
||||
)
|
||||
const currency = room.packages.find((pkg) => pkg.currency)?.currency
|
||||
isVatCurrency = false
|
||||
formattedTotalCost = formatPrice(
|
||||
intl,
|
||||
totalBookingVouchers,
|
||||
CurrencyEnum.Voucher,
|
||||
pkgsSum,
|
||||
currency
|
||||
)
|
||||
} else {
|
||||
isVatCurrency = false
|
||||
formattedTotalCost = formatPrice(
|
||||
intl,
|
||||
totalBookingVouchers,
|
||||
CurrencyEnum.Voucher
|
||||
)
|
||||
}
|
||||
}
|
||||
const initialData = {
|
||||
bookingCode,
|
||||
currencyCode,
|
||||
fromDate,
|
||||
toDate,
|
||||
roomCategories,
|
||||
rooms,
|
||||
vat,
|
||||
isVatCurrency,
|
||||
formattedTotalCost,
|
||||
totalBookingPrice,
|
||||
totalBookingCheques,
|
||||
}
|
||||
|
||||
storeRef.current = createBookingConfirmationStore(initialData)
|
||||
}
|
||||
|
||||
return (
|
||||
<BookingConfirmationContext.Provider value={storeRef.current}>
|
||||
{children}
|
||||
</BookingConfirmationContext.Provider>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { useContext } from "react"
|
||||
import { create, useStore } from "zustand"
|
||||
|
||||
import { BookingConfirmationContext } from "../../contexts/BookingConfirmation"
|
||||
|
||||
import type {
|
||||
BookingConfirmationState,
|
||||
InitialState,
|
||||
} from "../../types/stores/booking-confirmation"
|
||||
|
||||
export function createBookingConfirmationStore(initialState: InitialState) {
|
||||
return create<BookingConfirmationState>()((set) => ({
|
||||
rooms: initialState.rooms,
|
||||
bookingCode: initialState.bookingCode,
|
||||
currencyCode: initialState.currencyCode,
|
||||
fromDate: initialState.fromDate,
|
||||
toDate: initialState.toDate,
|
||||
roomCategories: initialState.roomCategories,
|
||||
vat: initialState.vat,
|
||||
formattedTotalCost: initialState.formattedTotalCost,
|
||||
isVatCurrency: initialState.isVatCurrency,
|
||||
totalBookingPrice: initialState.totalBookingPrice,
|
||||
totalBookingCheques: initialState.totalBookingCheques,
|
||||
actions: {
|
||||
setRoom: (room, idx) => {
|
||||
set((state) => {
|
||||
const rooms = [...state.rooms]
|
||||
rooms[idx] = room
|
||||
|
||||
const totalBookingPrice = rooms.reduce((acc, room) => {
|
||||
return acc + (room?.totalPrice ?? 0)
|
||||
}, 0)
|
||||
const totalBookingCheques = rooms.reduce((acc, room) => {
|
||||
return acc + (room?.cheques ?? 0)
|
||||
}, 0)
|
||||
return { rooms, totalBookingPrice, totalBookingCheques }
|
||||
})
|
||||
},
|
||||
setFormattedTotalCost: (formattedTotalCost) => {
|
||||
set(() => ({ formattedTotalCost }))
|
||||
},
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
export function useBookingConfirmationStore<T>(
|
||||
selector: (store: BookingConfirmationState) => T
|
||||
) {
|
||||
const store = useContext(BookingConfirmationContext)
|
||||
|
||||
if (!store) {
|
||||
throw new Error(
|
||||
"useBookingConfirmationStore must be used within BookingConfirmationProvider"
|
||||
)
|
||||
}
|
||||
|
||||
return useStore(store, selector)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { createBookingConfirmationStore } from "../../stores/booking-confirmation"
|
||||
|
||||
export type BookingConfirmationStore = ReturnType<
|
||||
typeof createBookingConfirmationStore
|
||||
>
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
||||
import type { RoomCategories } from "@scandic-hotels/trpc/types/hotel"
|
||||
|
||||
import type { Room } from "../stores/booking-confirmation"
|
||||
|
||||
export interface BookingConfirmationProviderProps
|
||||
extends React.PropsWithChildren {
|
||||
bookingCode: string | null
|
||||
currencyCode: CurrencyEnum
|
||||
fromDate: string
|
||||
roomCategories: RoomCategories
|
||||
rooms: (Room | null)[]
|
||||
toDate: string
|
||||
vat: number
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import type { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
||||
import type { ChildBedTypeEnum } from "@scandic-hotels/trpc/enums/childBedTypeEnum"
|
||||
import type {
|
||||
BookingConfirmation,
|
||||
PackageSchema,
|
||||
} from "@scandic-hotels/trpc/types/bookingConfirmation"
|
||||
import type { RoomCategories } from "@scandic-hotels/trpc/types/hotel"
|
||||
|
||||
export interface ChildBedPreference {
|
||||
quantity: number
|
||||
bedType: ChildBedTypeEnum
|
||||
}
|
||||
|
||||
export interface Room {
|
||||
adults: number
|
||||
bedDescription: string
|
||||
bedType: string
|
||||
bookingCode: string | null
|
||||
breakfast: PackageSchema | false | undefined
|
||||
breakfastIncluded: boolean
|
||||
cheques: number
|
||||
childBedPreferences: ChildBedPreference[]
|
||||
childrenAges?: number[]
|
||||
confirmationNumber: string
|
||||
currencyCode: CurrencyEnum
|
||||
fromDate: string
|
||||
name: string
|
||||
packages: BookingConfirmation["booking"]["packages"]
|
||||
formattedRoomCost: string
|
||||
rateDefinition: BookingConfirmation["booking"]["rateDefinition"]
|
||||
rateCodeType: BookingConfirmation["booking"]["bookingType"]
|
||||
refId: string
|
||||
roomFeatures?: PackageSchema[] | null
|
||||
roomPoints: number
|
||||
roomPrice: number
|
||||
roomTypeCode: string | null
|
||||
toDate: string
|
||||
totalPrice: number
|
||||
totalPriceExVat: number
|
||||
vatAmount: number
|
||||
vouchers: number
|
||||
}
|
||||
|
||||
export interface InitialState {
|
||||
bookingCode: string | null
|
||||
fromDate: string
|
||||
rooms: (Room | null)[]
|
||||
toDate: string
|
||||
currencyCode: CurrencyEnum
|
||||
roomCategories: RoomCategories
|
||||
vat: number
|
||||
isVatCurrency: boolean
|
||||
formattedTotalCost: string
|
||||
totalBookingPrice: number
|
||||
totalBookingCheques: number
|
||||
}
|
||||
|
||||
export interface BookingConfirmationState extends InitialState {
|
||||
actions: {
|
||||
setRoom: (room: Room, idx: number) => void
|
||||
setFormattedTotalCost: (formattedTotalCost: string) => void
|
||||
}
|
||||
}
|
||||
@@ -45,11 +45,14 @@
|
||||
"./contexts/SelectRate/types": "./lib/contexts/SelectRate/types.ts",
|
||||
"./hooks/useSearchHistory": "./lib/hooks/useSearchHistory.ts",
|
||||
"./pages/*": "./lib/pages/*.tsx",
|
||||
"./providers/BookingConfirmationProvider": "./lib/providers/BookingConfirmationProvider.tsx",
|
||||
"./searchType": "./lib/misc/searchType.ts",
|
||||
"./stores/bookingCode-filter": "./lib/stores/bookingCode-filter.ts",
|
||||
"./stores/hotels-map": "./lib/stores/hotels-map.ts",
|
||||
"./stores/booking-confirmation": "./lib/stores/booking-confirmation/index.ts",
|
||||
"./types/components/selectRate/selectRate": "./lib/types/components/selectRate/selectRate.ts",
|
||||
"./types/stores/rates": "./lib/types/stores/rates.ts",
|
||||
"./types/stores/booking-confirmation": "./lib/types/stores/booking-confirmation.ts",
|
||||
"./utils/isSameBooking": "./lib/utils/isSameBooking.ts",
|
||||
"./utils/url": "./lib/utils/url.ts",
|
||||
"./utils/SelectRate": "./lib/utils/SelectRate/index.tsx",
|
||||
|
||||
Reference in New Issue
Block a user