59 lines
1.3 KiB
TypeScript
59 lines
1.3 KiB
TypeScript
"use client"
|
|
import { useRouter } from "next/navigation"
|
|
import { useRef } from "react"
|
|
|
|
import { useDetailsStore } from "@/stores/details"
|
|
import { createStepsStore } from "@/stores/steps"
|
|
|
|
import { StepsContext } from "@/contexts/Steps"
|
|
|
|
import type { StepsStore } from "@/types/contexts/steps"
|
|
import type { StepsProviderProps } from "@/types/providers/steps"
|
|
|
|
export default function StepsProvider({
|
|
bedTypes,
|
|
breakfastPackages,
|
|
children,
|
|
isMember,
|
|
searchParams,
|
|
step,
|
|
}: StepsProviderProps) {
|
|
const storeRef = useRef<StepsStore>()
|
|
const updateBedType = useDetailsStore((state) => state.actions.updateBedType)
|
|
const updateBreakfast = useDetailsStore(
|
|
(state) => state.actions.updateBreakfast
|
|
)
|
|
const router = useRouter()
|
|
|
|
if (!storeRef.current) {
|
|
const noBedChoices = bedTypes.length === 1
|
|
const noBreakfast = !breakfastPackages?.length
|
|
|
|
if (noBedChoices) {
|
|
updateBedType({
|
|
description: bedTypes[0].description,
|
|
roomTypeCode: bedTypes[0].value,
|
|
})
|
|
}
|
|
|
|
if (noBreakfast) {
|
|
updateBreakfast(false)
|
|
}
|
|
|
|
storeRef.current = createStepsStore(
|
|
step,
|
|
isMember,
|
|
noBedChoices,
|
|
noBreakfast,
|
|
searchParams,
|
|
router.push
|
|
)
|
|
}
|
|
|
|
return (
|
|
<StepsContext.Provider value={storeRef.current}>
|
|
{children}
|
|
</StepsContext.Provider>
|
|
)
|
|
}
|