52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
"use client"
|
|
|
|
import { createContext, useContext } from "react"
|
|
|
|
import type { ScandicPartnersEnum } from "@scandic-hotels/common/constants/scandicPartners"
|
|
|
|
import type { BookingFlowConfig } from "./bookingFlowConfig"
|
|
|
|
type BookingFlowConfigContextData = BookingFlowConfig
|
|
|
|
const BookingFlowConfigContext = createContext<
|
|
BookingFlowConfigContextData | undefined
|
|
>(undefined)
|
|
|
|
export const useIsPartner = (partner: ScandicPartnersEnum) => {
|
|
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.partner === partner
|
|
}
|
|
|
|
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 function BookingFlowConfigContextProvider({
|
|
children,
|
|
config,
|
|
}: {
|
|
children: React.ReactNode
|
|
config: BookingFlowConfig
|
|
}) {
|
|
return (
|
|
<BookingFlowConfigContext.Provider value={config}>
|
|
{children}
|
|
</BookingFlowConfigContext.Provider>
|
|
)
|
|
}
|