feat: SW-2079 Show points in confirmation page * feat: SW-2079 Show points in confirmation page * feat: SW-2079 Optimized code * feat: SW-2079 Updated Body to Typography * feat: SW-2079 Multi-room total cost display * feat: SW-2079 Add reward nights condition rate title * feat: SW-2079 Removed extra checks * feat: SW-2079 Optimmized formatPrice function * feat: SW-2079 Typo fix Approved-by: Christian Andolf
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import { create } from "zustand"
|
|
|
|
import { CurrencyEnum } from "@/types/enums/currency"
|
|
|
|
interface RoomPrice {
|
|
id: string
|
|
totalPrice: number
|
|
currencyCode: CurrencyEnum
|
|
isMainBooking?: boolean
|
|
roomPoints: number
|
|
}
|
|
|
|
interface MyStayTotalPriceState {
|
|
rooms: RoomPrice[]
|
|
totalPrice: number | null
|
|
currencyCode: CurrencyEnum
|
|
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: CurrencyEnum.Unknown,
|
|
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 ?? CurrencyEnum.Unknown
|
|
|
|
// 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)
|
|
}, 0)
|
|
|
|
return {
|
|
rooms: newRooms,
|
|
totalPrice: total,
|
|
currencyCode,
|
|
totalPoints,
|
|
}
|
|
})
|
|
},
|
|
},
|
|
})
|
|
)
|