Files
web/apps/scandic-web/components/HotelReservation/MyStay/stores/myStayTotalPrice.ts
Pontus Dreij 31a536b1f7 Merged in feat(SW-1722)-mystay-multiroom-view (pull request #1396)
Feat(SW-1722) mystay multiroom view

* feat(SW-1722) View all rooms on my stay

* feat(sW-1722) Show linked reservation

* feat(SW-1722) merged monorepo

* feat(SW-1722) yarn install

* feat(SW-1722) removed unused data

* feat(SW-1722) Streaming data from the server to the client


Approved-by: Niclas Edenvin
2025-02-27 07:24:56 +00:00

67 lines
1.5 KiB
TypeScript

import { create } from "zustand"
interface RoomPrice {
id: string
totalPrice: number
currencyCode: string
isMainBooking?: boolean
}
interface MyStayTotalPriceState {
rooms: RoomPrice[]
totalPrice: number
currencyCode: string
// Add a single room price
addRoomPrice: (room: RoomPrice) => void
// Get the calculated total
getTotalPrice: () => number
}
export const useMyStayTotalPriceStore = create<MyStayTotalPriceState>(
(set, get) => ({
rooms: [],
totalPrice: 0,
currencyCode: "",
addRoomPrice: (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
},
})
)