Files
web/apps/scandic-web/stores/my-stay/myStayTotalPrice.ts
Pontus Dreij b48053b8b4 Merged in feat(SW-2083)-missing-booking-codes-scenarios-my-stay (pull request #1680)
Feat(SW-2083) missing booking codes scenarios my stay

* feat(SW-2083) Show points instead of reward nights

* feat(SW-2083) added support for cheque and voucher for totalPrice


Approved-by: Niclas Edenvin
2025-03-31 11:42:47 +00:00

72 lines
1.8 KiB
TypeScript

import { create } from "zustand"
interface RoomPrice {
id: string
totalPrice: number
currencyCode: string
isMainBooking?: boolean
roomPoints: number
}
interface MyStayTotalPriceState {
rooms: RoomPrice[]
totalPrice: number | null
currencyCode: string
totalPoints: number
actions: {
// Add a single room price
addRoomPrice: (room: RoomPrice) => void
}
}
export const useMyStayTotalPriceStore = create<MyStayTotalPriceState>(
(set) => ({
rooms: [],
totalPrice: null,
totalPoints: 0,
totalCheques: 0,
totalVouchers: 0,
currencyCode: "",
actions: {
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)
const totalPoints = newRooms.reduce((sum, r) => {
return sum + r.roomPoints
}, 0)
return {
rooms: newRooms,
totalPrice: total,
currencyCode,
totalPoints,
}
})
},
},
})
)