Files
web/apps/scandic-web/stores/my-stay/index.ts
Anton Gunnarsson 16fbdb7ae0 Merged in fix/refactor-currency-display (pull request #3434)
fix(SW-3616): Handle EuroBonus point type everywhere

* Add tests to formatPrice

* formatPrice

* More work replacing config with api points type

* More work replacing config with api points type

* More fixing with currency

* maybe actually fixed it

* Fix MyStay

* Clean up

* Fix comments

* Merge branch 'master' into fix/refactor-currency-display

* Fix calculateTotalPrice for EB points + SF points + cash


Approved-by: Joakim Jäderberg
2026-01-15 09:32:17 +00:00

109 lines
2.5 KiB
TypeScript

"use client"
import { produce } from "immer"
import { useContext } from "react"
import { create, useStore } from "zustand"
import { getHotelRoom } from "@scandic-hotels/trpc/routers/booking/helpers"
import { mapRoomDetails } from "@/components/HotelReservation/MyStay/utils/mapRoomDetails"
import { MyStayContext } from "@/contexts/MyStay"
import { calculateTotalPrice, isAllRoomsCancelled } from "./helpers"
import type { InitialState, MyStayState } from "@/types/stores/my-stay"
export function createMyStayStore({
breakfastPackages,
hotel,
intl,
refId,
hotelUrl,
roomCategories,
rooms,
savedCreditCards,
isLoggedIn,
isPastBooking,
}: InitialState) {
const rates = {
change: intl.formatMessage({
id: "booking.freeRebooking",
defaultMessage: "Free rebooking",
}),
flex: intl.formatMessage({
id: "booking.freeCancellation",
defaultMessage: "Free cancellation",
}),
save: intl.formatMessage({
id: "booking.nonRefundable",
defaultMessage: "Non-refundable",
}),
}
const mappedRooms = rooms.map((booking, idx) => {
const room = getHotelRoom(roomCategories, booking.roomTypeCode)
return mapRoomDetails({
booking,
rates,
room,
roomNumber: idx + 1,
})
})
const bookedRoom = mappedRooms[0]
const allRoomsAreCancelled = isAllRoomsCancelled(mappedRooms)
const totalPrice = calculateTotalPrice(
mappedRooms,
bookedRoom.currencyCode,
intl,
allRoomsAreCancelled
)
const mainRoom = mappedRooms.find((r) => r.mainRoom) ?? bookedRoom
return create<MyStayState>()((set) => {
return {
allRoomsAreCancelled,
bookedRoom,
breakfastPackages,
hotel,
hotelUrl,
isLoggedIn,
mainRoom,
manageStay: false,
refId,
rooms: mappedRooms,
savedCreditCards,
totalPrice,
isPastBooking,
actions: {
closeManageStay() {
return set(
produce((state: MyStayState) => {
state.manageStay = false
})
)
},
openManageStay() {
return set(
produce((state: MyStayState) => {
state.manageStay = true
})
)
},
},
}
})
}
export function useMyStayStore<T>(selector: (store: MyStayState) => T) {
const store = useContext(MyStayContext)
if (!store) {
throw new Error("useMyStayStore must be used within MyStayProvider")
}
return useStore(store, selector)
}