merge
This commit is contained in:
+21
-19
@@ -11,11 +11,12 @@ import {
|
||||
signedInDetailsSchema,
|
||||
} from "@/components/HotelReservation/EnterDetails/Details/schema"
|
||||
import { DetailsContext } from "@/contexts/Details"
|
||||
import { arrayMerge } from "@/utils/merge"
|
||||
|
||||
import { StepEnum } from "@/types/enums/step"
|
||||
import type { DetailsState, InitialState } from "@/types/stores/details"
|
||||
|
||||
export const storageName = "details-storage"
|
||||
export const detailsStorageName = "details-storage"
|
||||
export function createDetailsStore(
|
||||
initialState: InitialState,
|
||||
isMember: boolean
|
||||
@@ -27,13 +28,15 @@ export function createDetailsStore(
|
||||
* we cannot use the data as `defaultValues` for our forms.
|
||||
* RHF caches defaultValues on mount.
|
||||
*/
|
||||
const detailsStorageUnparsed = sessionStorage.getItem(storageName)
|
||||
const detailsStorageUnparsed = sessionStorage.getItem(detailsStorageName)
|
||||
if (detailsStorageUnparsed) {
|
||||
const detailsStorage: Record<
|
||||
"state",
|
||||
Pick<DetailsState, "data">
|
||||
> = JSON.parse(detailsStorageUnparsed)
|
||||
initialState = merge(initialState, detailsStorage.state.data)
|
||||
initialState = merge(detailsStorage.state.data, initialState, {
|
||||
arrayMerge,
|
||||
})
|
||||
}
|
||||
}
|
||||
return create<DetailsState>()(
|
||||
@@ -135,40 +138,39 @@ export function createDetailsStore(
|
||||
},
|
||||
|
||||
totalPrice: {
|
||||
euro: { currency: "", price: 0 },
|
||||
local: { currency: "", price: 0 },
|
||||
euro: { currency: "", amount: 0 },
|
||||
local: { currency: "", amount: 0 },
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: storageName,
|
||||
onRehydrateStorage() {
|
||||
name: detailsStorageName,
|
||||
onRehydrateStorage(prevState) {
|
||||
return function (state) {
|
||||
if (state) {
|
||||
const validatedBedType = bedTypeSchema.safeParse(state.data)
|
||||
if (validatedBedType.success) {
|
||||
state.actions.updateValidity(StepEnum.selectBed, true)
|
||||
} else {
|
||||
state.actions.updateValidity(StepEnum.selectBed, false)
|
||||
if (validatedBedType.success !== state.isValid["select-bed"]) {
|
||||
state.isValid["select-bed"] = validatedBedType.success
|
||||
}
|
||||
|
||||
const validatedBreakfast = breakfastStoreSchema.safeParse(
|
||||
state.data
|
||||
)
|
||||
if (validatedBreakfast.success) {
|
||||
state.actions.updateValidity(StepEnum.breakfast, true)
|
||||
} else {
|
||||
state.actions.updateValidity(StepEnum.breakfast, false)
|
||||
if (validatedBreakfast.success !== state.isValid.breakfast) {
|
||||
state.isValid.breakfast = validatedBreakfast.success
|
||||
}
|
||||
|
||||
const detailsSchema = isMember
|
||||
? signedInDetailsSchema
|
||||
: guestDetailsSchema
|
||||
const validatedDetails = detailsSchema.safeParse(state.data)
|
||||
if (validatedDetails.success) {
|
||||
state.actions.updateValidity(StepEnum.details, true)
|
||||
} else {
|
||||
state.actions.updateValidity(StepEnum.details, false)
|
||||
if (validatedDetails.success !== state.isValid.details) {
|
||||
state.isValid.details = validatedDetails.success
|
||||
}
|
||||
|
||||
const mergedState = merge(state.data, prevState.data, {
|
||||
arrayMerge,
|
||||
})
|
||||
state.data = mergedState
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ import {
|
||||
} from "@/components/HotelReservation/EnterDetails/Details/schema"
|
||||
import { StepsContext } from "@/contexts/Steps"
|
||||
|
||||
import { storageName as detailsStorageName } from "./details"
|
||||
import { detailsStorageName as detailsStorageName } from "./details"
|
||||
|
||||
import { StepEnum } from "@/types/enums/step"
|
||||
import type { DetailsState } from "@/types/stores/details"
|
||||
|
||||
Reference in New Issue
Block a user