Merge branch 'develop' into feature/tracking
This commit is contained in:
@@ -7,6 +7,7 @@ import { breakfastSchema } from "@/components/HotelReservation/EnterDetails/Brea
|
||||
import { detailsSchema } from "@/components/HotelReservation/EnterDetails/Details/schema"
|
||||
|
||||
import { DetailsSchema } from "@/types/components/enterDetails/details"
|
||||
import { SidePeekEnum } from "@/types/components/enterDetails/sidePeek"
|
||||
import { StepEnum } from "@/types/components/enterDetails/step"
|
||||
import { bedTypeEnum } from "@/types/enums/bedType"
|
||||
import { breakfastEnum } from "@/types/enums/breakfast"
|
||||
@@ -18,9 +19,15 @@ interface EnterDetailsState {
|
||||
} & DetailsSchema
|
||||
steps: StepEnum[]
|
||||
currentStep: StepEnum
|
||||
activeSidePeek: SidePeekEnum | null
|
||||
isValid: Record<StepEnum, boolean>
|
||||
completeStep: (updatedData: Partial<EnterDetailsState["data"]>) => void
|
||||
navigate: (step: StepEnum, searchParams?: Record<string, string>) => void
|
||||
navigate: (
|
||||
step: StepEnum,
|
||||
searchParams?: Record<string, string | boolean>
|
||||
) => void
|
||||
openSidePeek: (key: SidePeekEnum | null) => void
|
||||
closeSidePeek: () => void
|
||||
}
|
||||
|
||||
export function initEditDetailsState(currentStep: StepEnum) {
|
||||
@@ -33,26 +40,34 @@ export function initEditDetailsState(currentStep: StepEnum) {
|
||||
breakfast: undefined,
|
||||
countryCode: "",
|
||||
email: "",
|
||||
firstname: "",
|
||||
lastname: "",
|
||||
firstName: "",
|
||||
lastName: "",
|
||||
phoneNumber: "",
|
||||
join: false,
|
||||
zipCode: "",
|
||||
dateOfBirth: undefined,
|
||||
termsAccepted: false,
|
||||
}
|
||||
|
||||
let inputData = {}
|
||||
if (search?.size) {
|
||||
const searchParams: Record<string, string> = {}
|
||||
const searchParams: Record<string, string | boolean> = {}
|
||||
search.forEach((value, key) => {
|
||||
searchParams[key] = value
|
||||
// Handle boolean values
|
||||
|
||||
if (value === "true" || value === "false") {
|
||||
searchParams[key] = JSON.parse(value) as true | false
|
||||
} else {
|
||||
searchParams[key] = value
|
||||
}
|
||||
})
|
||||
|
||||
inputData = searchParams
|
||||
} else if (sessionData) {
|
||||
inputData = JSON.parse(sessionData)
|
||||
}
|
||||
|
||||
const validPaths = [StepEnum.selectBed]
|
||||
|
||||
let initialData = defaultData
|
||||
let initialData: EnterDetailsState["data"] = defaultData
|
||||
|
||||
const isValid = {
|
||||
[StepEnum.selectBed]: false,
|
||||
@@ -96,7 +111,7 @@ export function initEditDetailsState(currentStep: StepEnum) {
|
||||
const query = new URLSearchParams(window.location.search)
|
||||
if (searchParams) {
|
||||
Object.entries(searchParams).forEach(([key, value]) => {
|
||||
query.set(key, value)
|
||||
query.set(key, value ? value.toString() : "")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -104,7 +119,10 @@ export function initEditDetailsState(currentStep: StepEnum) {
|
||||
window.history.pushState({}, "", step + "?" + query.toString())
|
||||
})
|
||||
),
|
||||
openSidePeek: (key) => set({ activeSidePeek: key }),
|
||||
closeSidePeek: () => set({ activeSidePeek: null }),
|
||||
currentStep,
|
||||
activeSidePeek: null,
|
||||
isValid,
|
||||
completeStep: (updatedData) =>
|
||||
set(
|
||||
|
||||
144
stores/guests-rooms.ts
Normal file
144
stores/guests-rooms.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
"use client"
|
||||
|
||||
import { produce } from "immer"
|
||||
import { create } from "zustand"
|
||||
|
||||
import { BedTypeEnum } from "@/types/components/bookingWidget/enums"
|
||||
import { Child } from "@/types/components/bookingWidget/guestsRoomsPicker"
|
||||
|
||||
interface GuestsRooms {
|
||||
rooms: [
|
||||
{
|
||||
adults: number
|
||||
children: Child[]
|
||||
childrenInAdultsBed: number
|
||||
},
|
||||
]
|
||||
adultCount: number
|
||||
childCount: number
|
||||
isValidated: boolean
|
||||
increaseAdults: (roomIndex: number) => void
|
||||
decreaseAdults: (roomIndex: number) => void
|
||||
increaseChildren: (roomIndex: number) => void
|
||||
decreaseChildren: (roomIndex: number) => Child[]
|
||||
updateChildAge: (age: number, roomIndex: number, childIndex: number) => void
|
||||
updateChildBed: (bed: number, roomIndex: number, childIndex: number) => void
|
||||
increaseChildInAdultsBed: (roomIndex: number) => void
|
||||
decreaseChildInAdultsBed: (roomIndex: number) => void
|
||||
increaseRoom: () => void
|
||||
decreaseRoom: (roomIndex: number) => void
|
||||
setIsValidated: (isValidated: boolean) => void
|
||||
}
|
||||
|
||||
export const useGuestsRoomsStore = create<GuestsRooms>((set, get) => ({
|
||||
rooms: [
|
||||
{
|
||||
adults: 1,
|
||||
children: [],
|
||||
childrenInAdultsBed: 0,
|
||||
},
|
||||
],
|
||||
adultCount: 1,
|
||||
childCount: 0,
|
||||
isValidated: false,
|
||||
increaseAdults: (roomIndex) =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms[roomIndex].adults = state.rooms[roomIndex].adults + 1
|
||||
state.adultCount = state.adultCount + 1
|
||||
})
|
||||
),
|
||||
decreaseAdults: (roomIndex) =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms[roomIndex].adults = state.rooms[roomIndex].adults - 1
|
||||
state.adultCount = state.adultCount - 1
|
||||
if (
|
||||
state.rooms[roomIndex].childrenInAdultsBed >
|
||||
state.rooms[roomIndex].adults
|
||||
) {
|
||||
const toUpdateIndex = state.rooms[roomIndex].children.findIndex(
|
||||
(child) => child.bed == BedTypeEnum.IN_ADULTS_BED
|
||||
)
|
||||
if (toUpdateIndex != -1) {
|
||||
state.rooms[roomIndex].children[toUpdateIndex].bed =
|
||||
state.rooms[roomIndex].children[toUpdateIndex].age < 3
|
||||
? BedTypeEnum.IN_CRIB
|
||||
: BedTypeEnum.IN_EXTRA_BED
|
||||
state.rooms[roomIndex].childrenInAdultsBed =
|
||||
state.rooms[roomIndex].adults
|
||||
}
|
||||
}
|
||||
})
|
||||
),
|
||||
increaseChildren: (roomIndex) =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms[roomIndex].children.push({
|
||||
age: -1,
|
||||
bed: -1,
|
||||
})
|
||||
state.childCount = state.childCount + 1
|
||||
})
|
||||
),
|
||||
decreaseChildren: (roomIndex) => {
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
const roomChildren = state.rooms[roomIndex].children
|
||||
if (
|
||||
roomChildren.length &&
|
||||
roomChildren[roomChildren.length - 1].bed == BedTypeEnum.IN_ADULTS_BED
|
||||
) {
|
||||
state.rooms[roomIndex].childrenInAdultsBed =
|
||||
state.rooms[roomIndex].childrenInAdultsBed - 1
|
||||
}
|
||||
state.rooms[roomIndex].children.pop()
|
||||
state.childCount = state.childCount - 1
|
||||
})
|
||||
)
|
||||
return get().rooms[roomIndex].children
|
||||
},
|
||||
updateChildAge: (age, roomIndex, childIndex) =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms[roomIndex].children[childIndex].age = age
|
||||
})
|
||||
),
|
||||
updateChildBed: (bed, roomIndex, childIndex) =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms[roomIndex].children[childIndex].bed = bed
|
||||
})
|
||||
),
|
||||
increaseChildInAdultsBed: (roomIndex) =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms[roomIndex].childrenInAdultsBed =
|
||||
state.rooms[roomIndex].childrenInAdultsBed + 1
|
||||
})
|
||||
),
|
||||
decreaseChildInAdultsBed: (roomIndex) =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms[roomIndex].childrenInAdultsBed =
|
||||
state.rooms[roomIndex].childrenInAdultsBed - 1
|
||||
})
|
||||
),
|
||||
increaseRoom: () =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms.push({
|
||||
adults: 1,
|
||||
children: [],
|
||||
childrenInAdultsBed: 0,
|
||||
})
|
||||
})
|
||||
),
|
||||
decreaseRoom: (roomIndex) =>
|
||||
set(
|
||||
produce((state: GuestsRooms) => {
|
||||
state.rooms.splice(roomIndex, 1)
|
||||
})
|
||||
),
|
||||
setIsValidated: (isValidated) => set(() => ({ isValidated })),
|
||||
}))
|
||||
Reference in New Issue
Block a user