feat: merge stores, fix auto navigation, split summary
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
"use client"
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import { useRef } from "react"
|
||||
|
||||
import { createDetailsStore } from "@/stores/details"
|
||||
|
||||
import { getQueryParamsForEnterDetails } from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
|
||||
import { DetailsContext } from "@/contexts/Details"
|
||||
|
||||
import type { DetailsStore } from "@/types/contexts/details"
|
||||
import type { DetailsProviderProps } from "@/types/providers/details"
|
||||
|
||||
export default function DetailsProvider({
|
||||
children,
|
||||
isMember,
|
||||
}: DetailsProviderProps) {
|
||||
const storeRef = useRef<DetailsStore>()
|
||||
const searchParams = useSearchParams()
|
||||
|
||||
if (!storeRef.current) {
|
||||
const booking = getQueryParamsForEnterDetails(searchParams)
|
||||
storeRef.current = createDetailsStore({ booking }, isMember)
|
||||
}
|
||||
|
||||
return (
|
||||
<DetailsContext.Provider value={storeRef.current}>
|
||||
{children}
|
||||
</DetailsContext.Provider>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
"use client"
|
||||
import { useRef } from "react"
|
||||
|
||||
import { createDetailsStore } from "@/stores/enter-details"
|
||||
|
||||
import { DetailsContext } from "@/contexts/Details"
|
||||
|
||||
import type { DetailsStore } from "@/types/contexts/enter-details"
|
||||
import type { DetailsProviderProps } from "@/types/providers/enter-details"
|
||||
import type { InitialState } from "@/types/stores/enter-details"
|
||||
|
||||
export default function EnterDetailsProvider({
|
||||
bedTypes,
|
||||
booking,
|
||||
breakfastPackages,
|
||||
children,
|
||||
packages,
|
||||
roomRate,
|
||||
searchParamsStr,
|
||||
step,
|
||||
user,
|
||||
}: DetailsProviderProps) {
|
||||
const storeRef = useRef<DetailsStore>()
|
||||
|
||||
if (!storeRef.current) {
|
||||
const initialData: InitialState = { booking, packages, roomRate }
|
||||
if (bedTypes.length === 1) {
|
||||
initialData.bedType = {
|
||||
description: bedTypes[0].description,
|
||||
roomTypeCode: bedTypes[0].value,
|
||||
}
|
||||
}
|
||||
if (!breakfastPackages?.length) {
|
||||
initialData.breakfast = false
|
||||
}
|
||||
|
||||
storeRef.current = createDetailsStore(
|
||||
initialData,
|
||||
step,
|
||||
searchParamsStr,
|
||||
user
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<DetailsContext.Provider value={storeRef.current}>
|
||||
{children}
|
||||
</DetailsContext.Provider>
|
||||
)
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
"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>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user