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:
Hrishikesh Vaipurkar
2025-09-04 08:14:50 +00:00
parent 7e585b2d9a
commit 8e00769c64
19 changed files with 24 additions and 31 deletions

View File

@@ -2,12 +2,11 @@
import { useIntl } from "react-intl"
import { useBookingConfirmationStore } from "@scandic-hotels/booking-flow/stores/booking-confirmation"
import Body from "@scandic-hotels/design-system/Body"
import SkeletonShimmer from "@scandic-hotels/design-system/SkeletonShimmer"
import Subtitle from "@scandic-hotels/design-system/Subtitle"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
import styles from "./paymentDetails.module.css"
export default function PaymentDetails() {

View File

@@ -1,10 +1,9 @@
"use client"
import PriceDetailsModal from "@scandic-hotels/booking-flow/components/PriceDetailsModal"
import { useBookingConfirmationStore } from "@scandic-hotels/booking-flow/stores/booking-confirmation"
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import { dt } from "@scandic-hotels/common/dt"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
import { mapToPrice } from "./mapToPrice"
import type { Price } from "@/types/components/hotelReservation/price"

View File

@@ -7,10 +7,9 @@ import {
packageSchema,
} from "@scandic-hotels/trpc/routers/hotels/schemas/packages"
import type { Room } from "@scandic-hotels/booking-flow/types/stores/booking-confirmation"
import type { Package } from "@scandic-hotels/trpc/types/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)

View File

@@ -3,6 +3,7 @@
import { cx } from "class-variance-authority"
import { useIntl } from "react-intl"
import { useBookingConfirmationStore } from "@scandic-hotels/booking-flow/stores/booking-confirmation"
import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting"
import { Button } from "@scandic-hotels/design-system/Button"
import { Divider } from "@scandic-hotels/design-system/Divider"
@@ -12,7 +13,6 @@ import { Typography } from "@scandic-hotels/design-system/Typography"
import { ChildBedTypeEnum } from "@scandic-hotels/trpc/enums/childBedTypeEnum"
import { CancellationRuleEnum } from "@/constants/booking"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
import { getFeatureDescription } from "@/components/HotelReservation/utils/getRoomFeatureDescription"

View File

@@ -3,13 +3,12 @@
import { cx } from "class-variance-authority"
import { useIntl } from "react-intl"
import { useBookingConfirmationStore } from "@scandic-hotels/booking-flow/stores/booking-confirmation"
import { BookingCodeChip } from "@scandic-hotels/design-system/BookingCodeChip"
import { Divider } from "@scandic-hotels/design-system/Divider"
import SkeletonShimmer from "@scandic-hotels/design-system/SkeletonShimmer"
import { Typography } from "@scandic-hotels/design-system/Typography"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
import PriceDetails from "../../PriceDetails"
import styles from "./totalPrice.module.css"

View File

@@ -2,14 +2,13 @@
import { useIntl } from "react-intl"
import { useBookingConfirmationStore } from "@scandic-hotels/booking-flow/stores/booking-confirmation"
import { longDateFormat } from "@scandic-hotels/common/constants/dateFormats"
import { dt } from "@scandic-hotels/common/dt"
import { Divider } from "@scandic-hotels/design-system/Divider"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import { Typography } from "@scandic-hotels/design-system/Typography"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
import useLang from "@/hooks/useLang"
import Room from "./Room"

View File

@@ -3,12 +3,11 @@
import { useEffect } from "react"
import { useIntl } from "react-intl"
import { useBookingConfirmationStore } from "@scandic-hotels/booking-flow/stores/booking-confirmation"
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting"
import { trpc } from "@scandic-hotels/trpc/client"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
import useLang from "@/hooks/useLang"
import { mapRoomState } from "../../utils"

View File

@@ -3,6 +3,7 @@
import { useIntl } from "react-intl"
import { RoomDetailsSidePeek } from "@scandic-hotels/booking-flow/components/RoomDetailsSidePeek"
import { useBookingConfirmationStore } from "@scandic-hotels/booking-flow/stores/booking-confirmation"
import {
changeOrCancelDateFormat,
longDateFormat,
@@ -15,7 +16,6 @@ import { Typography } from "@scandic-hotels/design-system/Typography"
import { getHotelRoom } from "@scandic-hotels/trpc/routers/booking/helpers"
import { CancellationRuleEnum } from "@/constants/booking"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
import useLang from "@/hooks/useLang"

View File

@@ -3,8 +3,7 @@
import { useEffect, useState } from "react"
import { useSearchHistory } from "@scandic-hotels/booking-flow/hooks/useSearchHistory"
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
import { useBookingConfirmationStore } from "@scandic-hotels/booking-flow/stores/booking-confirmation"
import TrackingSDK from "@/components/TrackingSDK"
import useLang from "@/hooks/useLang"
@@ -12,10 +11,9 @@ import useLang from "@/hooks/useLang"
import { clearPaymentInfoSessionStorage } from "../../EnterDetails/Payment/helpers"
import { getTracking } from "./tracking"
import type { Room } from "@scandic-hotels/booking-flow/types/stores/booking-confirmation"
import type { BookingConfirmation } from "@scandic-hotels/trpc/types/bookingConfirmation"
import type { Room } from "@/types/stores/booking-confirmation"
export default function Tracking({
bookingConfirmation,
refId,

View File

@@ -19,12 +19,11 @@ import { CancellationRuleEnum } from "@/constants/booking"
import { readPaymentInfoFromSessionStorage } from "@/components/HotelReservation/EnterDetails/Payment/helpers"
import { getSpecialRoomType } from "@/utils/specialRoomType"
import type { Room } from "@scandic-hotels/booking-flow/types/stores/booking-confirmation"
import type { Lang } from "@scandic-hotels/common/constants/language"
import type { BookingConfirmation } from "@scandic-hotels/trpc/types/bookingConfirmation"
import type { RateDefinition } from "@scandic-hotels/trpc/types/roomAvailability"
import type { Room } from "@/types/stores/booking-confirmation"
function getRate(cancellationRule: RateDefinition["cancellationRule"] | null) {
switch (cancellationRule) {
case "CancellableBefore6PM":

View File

@@ -1,5 +1,6 @@
import { notFound } from "next/navigation"
import BookingConfirmationProvider from "@scandic-hotels/booking-flow/providers/BookingConfirmationProvider"
import { filterOverlappingDates } from "@scandic-hotels/booking-flow/utils/SelectRate"
import { AlertTypeEnum } from "@scandic-hotels/common/constants/alert"
import { dt } from "@scandic-hotels/common/dt"
@@ -15,7 +16,6 @@ import Receipt from "@/components/HotelReservation/BookingConfirmation/Receipt"
import Rooms from "@/components/HotelReservation/BookingConfirmation/Rooms"
import SidePanel from "@/components/HotelReservation/SidePanel"
import { getIntl } from "@/i18n"
import BookingConfirmationProvider from "@/providers/BookingConfirmationProvider"
import Confirmation from "./Confirmation"
import Tracking from "./Tracking"

View File

@@ -1,6 +0,0 @@
import { createContext } from "react"
import type { BookingConfirmationStore } from "@/types/contexts/booking-confirmation"
export const BookingConfirmationContext =
createContext<BookingConfirmationStore | null>(null)

View File

@@ -1,111 +0,0 @@
"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 { createBookingConfirmationStore } from "@/stores/booking-confirmation"
import { BookingConfirmationContext } from "@/contexts/BookingConfirmation"
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>
)
}

View File

@@ -1,58 +0,0 @@
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)
}

View File

@@ -1,4 +1,4 @@
import type { Room } from "@/types/stores/booking-confirmation"
import type { Room } from "@scandic-hotels/booking-flow/types/stores/booking-confirmation"
export interface BookingConfirmationReceiptRoomProps {
room: Room

View File

@@ -1,5 +0,0 @@
import type { createBookingConfirmationStore } from "@/stores/booking-confirmation"
export type BookingConfirmationStore = ReturnType<
typeof createBookingConfirmationStore
>

View File

@@ -1,15 +0,0 @@
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
}

View File

@@ -1,63 +0,0 @@
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
}
}