Files
web/apps/scandic-web/stores/my-stay/index.ts
Matilda Haneling 86331a6e10 Merged in fix/book-607-fix-old-links-my-stay (pull request #3365)
fix: updated to use new link structure on My Stay

* fix: updated to use new link structure on My Stay

* fix(BOOK-607): hide hotel page link if link prop is missing

* comment fixes

* fix(BOOK-607): read href from window (lcalhost not allowed)


Approved-by: Matilda Landström
2025-12-30 08:33:52 +00:00

116 lines
2.6 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 {
calculateTotalPoints,
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 totalPoints = calculateTotalPoints(mappedRooms, allRoomsAreCancelled)
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,
totalPoints,
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)
}