Fix/STAY-65 manage stay * fix: Disable manage stay for past bookings * fix: handle past and cancelled stay the same * fix: indentify past booking * fix: refactor to use design system components Approved-by: Erik Tiekstra
114 lines
2.6 KiB
TypeScript
114 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,
|
|
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,
|
|
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)
|
|
}
|