feat(SW-1717): rewrite select-rate to show all variants of rate-cards
This commit is contained in:
committed by
Michael Zetterberg
parent
adde77eaa9
commit
ebaea78fb3
@@ -1,9 +1,14 @@
|
||||
"use client"
|
||||
import { useEffect } from "react"
|
||||
|
||||
import { trpc } from "@/lib/trpc/client"
|
||||
import { useRatesStore } from "@/stores/select-rate"
|
||||
|
||||
import { RoomContext } from "@/contexts/SelectRate/Room"
|
||||
import useLang from "@/hooks/useLang"
|
||||
|
||||
import { BookingCodeFilterEnum } from "@/types/enums/bookingCodeFilter"
|
||||
import { RateTypeEnum } from "@/types/enums/rateType"
|
||||
import type { RoomProviderProps } from "@/types/providers/select-rate/room"
|
||||
|
||||
export default function RoomProvider({
|
||||
@@ -11,38 +16,72 @@ export default function RoomProvider({
|
||||
idx,
|
||||
room,
|
||||
}: RoomProviderProps) {
|
||||
const activeRoom = useRatesStore((state) => state.activeRoom)
|
||||
const closeSection = useRatesStore((state) => state.actions.closeSection(idx))
|
||||
const modifyRate = useRatesStore((state) => state.actions.modifyRate(idx))
|
||||
const roomAvailability = useRatesStore(
|
||||
(state) => state.roomsAvailability?.[idx]
|
||||
)
|
||||
const selectFilter = useRatesStore((state) => state.actions.selectFilter(idx))
|
||||
const selectRate = useRatesStore((state) => state.actions.selectRate(idx))
|
||||
const selectRateRedemption = useRatesStore((state) =>
|
||||
state.actions.selectRateRedemption(idx)
|
||||
)
|
||||
const selectRateCheque = useRatesStore((state) =>
|
||||
state.actions.selectRateCheque(idx)
|
||||
)
|
||||
const selectRateVoucher = useRatesStore((state) =>
|
||||
state.actions.selectRateVoucher(idx)
|
||||
)
|
||||
const lang = useLang()
|
||||
const { activeRoom, booking, roomAvailability, selectedFilter } =
|
||||
useRatesStore((state) => ({
|
||||
activeRoom: state.activeRoom,
|
||||
booking: state.booking,
|
||||
roomAvailability: state.roomsAvailability?.[idx],
|
||||
selectedFilter: state.rooms[idx].selectedFilter,
|
||||
}))
|
||||
const { appendRegularRates, ...actions } = room.actions
|
||||
const roomNr = idx + 1
|
||||
|
||||
const hasRedemptionRates = room.rooms.some((room) => room.redemptions.length)
|
||||
const hasCorporateChequeOrVoucherRates = room.rooms.some((room) =>
|
||||
room.code.some((product) => {
|
||||
if ("corporateCheque" in product) {
|
||||
return product.corporateCheque.rateType === RateTypeEnum.CorporateCheque
|
||||
} else if ("voucher" in product) {
|
||||
return product.voucher.rateType === RateTypeEnum.Voucher
|
||||
}
|
||||
return false
|
||||
})
|
||||
)
|
||||
|
||||
const dontShowRegularRates =
|
||||
hasRedemptionRates || hasCorporateChequeOrVoucherRates
|
||||
|
||||
// Extra query needed to fetch regular rates upon user
|
||||
// selecting to view all rates.
|
||||
// TODO: Setup route to handle singular availability call
|
||||
const { data, isFetched, isFetching } =
|
||||
trpc.hotel.availability.roomsCombinedAvailability.useQuery(
|
||||
{
|
||||
adultsCount: [room.bookingRoom.adults],
|
||||
childArray: room.bookingRoom.childrenInRoom
|
||||
? [room.bookingRoom.childrenInRoom]
|
||||
: undefined,
|
||||
hotelId: booking.hotelId,
|
||||
lang,
|
||||
roomStayEndDate: booking.toDate,
|
||||
roomStayStartDate: booking.fromDate,
|
||||
},
|
||||
{
|
||||
enabled: !!(
|
||||
booking.bookingCode &&
|
||||
selectedFilter === BookingCodeFilterEnum.All &&
|
||||
!dontShowRegularRates
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (isFetched && !isFetching && data?.length) {
|
||||
const regularRates = data[0]
|
||||
if ("roomConfigurations" in regularRates) {
|
||||
appendRegularRates(regularRates.roomConfigurations)
|
||||
}
|
||||
}
|
||||
}, [appendRegularRates, data, isFetched, isFetching])
|
||||
|
||||
return (
|
||||
<RoomContext.Provider
|
||||
value={{
|
||||
...room,
|
||||
actions: {
|
||||
closeSection,
|
||||
modifyRate,
|
||||
selectFilter,
|
||||
selectRate,
|
||||
selectRateRedemption,
|
||||
selectRateCheque,
|
||||
selectRateVoucher,
|
||||
},
|
||||
actions,
|
||||
isActiveRoom: activeRoom === idx,
|
||||
isFetchingAdditionalRate: isFetched ? false : isFetching,
|
||||
isMainRoom: roomNr === 1,
|
||||
roomAvailability,
|
||||
roomNr,
|
||||
|
||||
Reference in New Issue
Block a user