145 lines
4.2 KiB
TypeScript
145 lines
4.2 KiB
TypeScript
"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 })),
|
|
}))
|