Files
web/packages/booking-flow/lib/bookingFlowConfig/bookingFlowConfigContext.tsx
Anton Gunnarsson 6ee262ad89 Merged in chore/booking-flow-cleaning (pull request #3354)
chore: Clean booking-flow

* Clean booking-flow

* Fix type issue


Approved-by: Joakim Jäderberg
Approved-by: Linus Flood
2025-12-17 13:04:26 +00:00

54 lines
1.3 KiB
TypeScript

"use client"
import { createContext, useContext } from "react"
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import type { BookingFlowConfig } from "./bookingFlowConfig"
type BookingFlowConfigContextData = BookingFlowConfig
const BookingFlowConfigContext = createContext<
BookingFlowConfigContextData | undefined
>(undefined)
export const useBookingFlowConfig = (): BookingFlowConfigContextData => {
const context = useContext(BookingFlowConfigContext)
if (!context) {
throw new Error(
"useBookingFlowConfig must be used within a BookingFlowConfigContextProvider. Did you forget to use BookingFlowConfig in the consuming app?"
)
}
return context
}
export const useGetPointsCurrency = () => {
const config = useBookingFlowConfig()
switch (config.variant) {
case "scandic":
return CurrencyEnum.POINTS
case "partner-sas":
return CurrencyEnum.EUROBONUS
default:
const _exhaustiveCheck: never = config.variant
throw new Error(`Unknown variant: ${config.variant}`)
}
}
export function BookingFlowConfigContextProvider({
children,
config,
}: {
children: React.ReactNode
config: BookingFlowConfig
}) {
return (
<BookingFlowConfigContext.Provider value={config}>
{children}
</BookingFlowConfigContext.Provider>
)
}