Files
web/apps/scandic-web/stores/booking-confirmation/index.ts
Arvid Norlin 0c7836fa59 Merged in fix/SW-2553-sidepeeks (pull request #1919)
Fix/SW-2553 sidepeeks

* fix(SW-2553): apply sidepeek display logic

* chore: move convertToChildType and getPriceType utils

* fix: apply PR requested changes

* fix(SW-2553): fix roomNumber for multiroom

* fix(SW-2553): fix sidepeek for my-stay page


Approved-by: Michael Zetterberg
Approved-by: Bianca Widstam
Approved-by: Matilda Landström
2025-05-02 15:10:34 +00:00

59 lines
1.8 KiB
TypeScript

import { useContext } from "react"
import { create, useStore } from "zustand"
import { BookingConfirmationContext } from "@/contexts/BookingConfirmation"
import type {
BookingConfirmationState,
InitialState,
} from "@/types/stores/booking-confirmation"
export function createBookingConfirmationStore(initialState: InitialState) {
return create<BookingConfirmationState>()((set) => ({
rooms: initialState.rooms,
bookingCode: initialState.bookingCode,
currencyCode: initialState.currencyCode,
fromDate: initialState.fromDate,
toDate: initialState.toDate,
roomCategories: initialState.roomCategories,
vat: initialState.vat,
formattedTotalCost: initialState.formattedTotalCost,
isVatCurrency: initialState.isVatCurrency,
totalBookingPrice: initialState.totalBookingPrice,
totalBookingCheques: initialState.totalBookingCheques,
actions: {
setRoom: (room, idx) => {
set((state) => {
const rooms = [...state.rooms]
rooms[idx] = room
const totalBookingPrice = rooms.reduce((acc, room) => {
return acc + (room?.totalPrice ?? 0)
}, 0)
const totalBookingCheques = rooms.reduce((acc, room) => {
return acc + (room?.cheques ?? 0)
}, 0)
return { rooms, totalBookingPrice, totalBookingCheques }
})
},
setFormattedTotalCost: (formattedTotalCost) => {
set(() => ({ formattedTotalCost }))
},
},
}))
}
export function useBookingConfirmationStore<T>(
selector: (store: BookingConfirmationState) => T
) {
const store = useContext(BookingConfirmationContext)
if (!store) {
throw new Error(
"useBookingConfirmationStore must be used within BookingConfirmationProvider"
)
}
return useStore(store, selector)
}