Feat(SW-1274) modify date my stay * feat(SW-1676): Modify guest details step 1 * feat(SW-1676) Integration to api to update guest details * feat(SW-1676) Reuse of old modal * feat(SW-1676) updated modify guest * feat(SW-1676) cleanup * feat(SW-1274) modify stay modal and datepicker * feat(SW-1274) DatePicker from modify dates * feat(SW-1274) Modify dates fixes and merge conflicts * feat(SW-1274) handle modify for multiroom * feat(SW-1274) update manage stay * feat(SW-1274) fixed some comments * feat(SW-1274) use Modal instead * feat(SW-1274) fixed formatChildBedPreferences * feat(SW-1274) removed any as prop * feat(SW-1274) fix rebase conflicts * feat(SW-1274) fix flicker on modify modal * feat(SW-1274) CalendarButton * feat(SW-1274) fixed gap variable * feat(SW-1274) simplified code * feat(SW-1274) Split up DatePicker on mode * feat(SW-1274) Updated file structure for datepicker Approved-by: Arvid Norlin
69 lines
1.6 KiB
TypeScript
69 lines
1.6 KiB
TypeScript
import { create } from "zustand"
|
|
|
|
interface RoomPrice {
|
|
id: string
|
|
totalPrice: number
|
|
currencyCode: string
|
|
isMainBooking?: boolean
|
|
}
|
|
|
|
interface MyStayTotalPriceState {
|
|
rooms: RoomPrice[]
|
|
totalPrice: number
|
|
currencyCode: string
|
|
actions: {
|
|
// Add a single room price
|
|
setRoomPrice: (room: RoomPrice) => void
|
|
|
|
// Get the calculated total
|
|
getTotalPrice: () => number
|
|
}
|
|
}
|
|
|
|
export const useMyStayTotalPriceStore = create<MyStayTotalPriceState>(
|
|
(set, get) => ({
|
|
rooms: [],
|
|
totalPrice: 0,
|
|
currencyCode: "",
|
|
actions: {
|
|
setRoomPrice: (room) => {
|
|
set((state) => {
|
|
// Check if room with this ID already exists
|
|
const existingIndex = state.rooms.findIndex((r) => r.id === room.id)
|
|
let newRooms = [...state.rooms]
|
|
|
|
if (existingIndex >= 0) {
|
|
// Update existing room
|
|
newRooms[existingIndex] = room
|
|
} else {
|
|
// Add new room
|
|
newRooms.push(room)
|
|
}
|
|
|
|
// Get currency from main booking or first room
|
|
const mainRoom = newRooms.find((r) => r.isMainBooking) || newRooms[0]
|
|
const currencyCode = mainRoom?.currencyCode || ""
|
|
|
|
// Calculate total (only same currency for now)
|
|
const total = newRooms.reduce((sum, r) => {
|
|
if (r.currencyCode === currencyCode) {
|
|
return sum + r.totalPrice
|
|
}
|
|
return sum
|
|
}, 0)
|
|
|
|
return {
|
|
rooms: newRooms,
|
|
totalPrice: total,
|
|
currencyCode,
|
|
}
|
|
})
|
|
},
|
|
|
|
getTotalPrice: () => {
|
|
return get().totalPrice
|
|
},
|
|
},
|
|
})
|
|
)
|