fix: move crunching of data to trpc layer
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
import { notFound } from "next/navigation"
|
||||
|
||||
import {
|
||||
getHotelData,
|
||||
getProfileSafely,
|
||||
getRoomAvailability,
|
||||
getSelectedRoomAvailability,
|
||||
} from "@/lib/trpc/memoizedRequests"
|
||||
import { HotelIncludeEnum } from "@/server/routers/hotels/input"
|
||||
|
||||
import Summary from "@/components/HotelReservation/EnterDetails/Summary"
|
||||
import { getQueryParamsForEnterDetails } from "@/components/HotelReservation/SelectRate/RoomSelection/utils"
|
||||
import { formatNumber } from "@/utils/format"
|
||||
|
||||
import { SelectRateSearchParams } from "@/types/components/hotelReservation/selectRate/selectRate"
|
||||
import { LangParams, PageArgs, SearchParams } from "@/types/params"
|
||||
@@ -20,68 +19,61 @@ export default async function SummaryPage({
|
||||
const { hotel, adults, children, roomTypeCode, rateCode, fromDate, toDate } =
|
||||
getQueryParamsForEnterDetails(selectRoomParams)
|
||||
|
||||
const [user, hotelData, availability] = await Promise.all([
|
||||
if (!roomTypeCode || !rateCode) {
|
||||
console.log("No roomTypeCode or rateCode")
|
||||
return notFound()
|
||||
}
|
||||
|
||||
const [user, availability] = await Promise.all([
|
||||
getProfileSafely(),
|
||||
getHotelData({
|
||||
hotelId: hotel,
|
||||
language: params.lang,
|
||||
include: [HotelIncludeEnum.RoomCategories],
|
||||
}),
|
||||
getRoomAvailability({
|
||||
getSelectedRoomAvailability({
|
||||
hotelId: parseInt(hotel),
|
||||
adults,
|
||||
children,
|
||||
roomStayStartDate: fromDate,
|
||||
roomStayEndDate: toDate,
|
||||
rateCode,
|
||||
roomTypeCode,
|
||||
}),
|
||||
])
|
||||
|
||||
if (!hotelData?.data || !hotelData?.included || !availability) {
|
||||
console.error("No hotel or availability data", hotelData, availability)
|
||||
if (!availability) {
|
||||
console.error("No hotel or availability data", availability)
|
||||
// TODO: handle this case
|
||||
return null
|
||||
}
|
||||
|
||||
const cancellationText =
|
||||
availability?.rateDefinitions.find((rate) => rate.rateCode === rateCode)
|
||||
?.cancellationText ?? ""
|
||||
const chosenRoom = availability.roomConfigurations.find(
|
||||
(availRoom) => availRoom.roomTypeCode === roomTypeCode
|
||||
)
|
||||
|
||||
if (!chosenRoom) {
|
||||
// TODO: handle this case
|
||||
console.error("No chosen room", chosenRoom)
|
||||
return null
|
||||
}
|
||||
|
||||
const memberRate = chosenRoom.products.find(
|
||||
(rate) => rate.productType.member?.rateCode === rateCode
|
||||
)?.productType.member
|
||||
|
||||
const publicRate = chosenRoom.products.find(
|
||||
(rate) => rate.productType.public?.rateCode === rateCode
|
||||
)?.productType.public
|
||||
|
||||
const prices = user
|
||||
? {
|
||||
local: memberRate?.localPrice.pricePerStay,
|
||||
euro: memberRate?.requestedPrice?.pricePerStay,
|
||||
local: {
|
||||
price: availability.memberRate?.localPrice.pricePerStay,
|
||||
currency: availability.memberRate?.localPrice.currency,
|
||||
},
|
||||
euro: {
|
||||
price: availability.memberRate?.requestedPrice?.pricePerStay,
|
||||
currency: availability.memberRate?.requestedPrice?.currency,
|
||||
},
|
||||
}
|
||||
: {
|
||||
local: publicRate?.localPrice.pricePerStay,
|
||||
euro: publicRate?.requestedPrice?.pricePerStay,
|
||||
local: {
|
||||
price: availability.publicRate?.localPrice.pricePerStay,
|
||||
currency: availability.publicRate?.localPrice.currency,
|
||||
},
|
||||
euro: {
|
||||
price: availability.publicRate?.requestedPrice?.pricePerStay,
|
||||
currency: availability.publicRate?.requestedPrice?.currency,
|
||||
},
|
||||
}
|
||||
|
||||
return (
|
||||
<Summary
|
||||
isMember={!!user}
|
||||
room={{
|
||||
roomType: chosenRoom.roomType,
|
||||
localPrice: formatNumber(parseInt(prices.local ?? "0")),
|
||||
euroPrice: formatNumber(parseInt(prices.euro ?? "0")),
|
||||
roomType: availability.selectedRoom.roomType,
|
||||
localPrice: prices.local,
|
||||
euroPrice: prices.euro,
|
||||
adults,
|
||||
cancellationText,
|
||||
cancellationText: availability.cancellationText,
|
||||
}}
|
||||
/>
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
getHotelData,
|
||||
getProfileSafely,
|
||||
getRoomAvailability,
|
||||
getSelectedRoomAvailability,
|
||||
} from "@/lib/trpc/memoizedRequests"
|
||||
import { HotelIncludeEnum } from "@/server/routers/hotels/input"
|
||||
|
||||
@@ -36,9 +37,7 @@ export default async function StepPage({
|
||||
searchParams,
|
||||
}: PageArgs<LangParams & { step: StepEnum }, SelectRateSearchParams>) {
|
||||
const { lang } = params
|
||||
if (!searchParams.hotel) {
|
||||
redirect(`/${lang}`)
|
||||
}
|
||||
|
||||
void getBreakfastPackages(searchParams.hotel)
|
||||
|
||||
const intl = await getIntl()
|
||||
@@ -62,24 +61,37 @@ export default async function StepPage({
|
||||
rateCode
|
||||
})
|
||||
|
||||
const hotelData = await getHotelData({
|
||||
hotelId,
|
||||
language: lang,
|
||||
include: [HotelIncludeEnum.RoomCategories],
|
||||
})
|
||||
|
||||
const user = await getProfileSafely()
|
||||
const savedCreditCards = await getCreditCardsSafely()
|
||||
const breakfastPackages = await getBreakfastPackages(searchParams.hotel)
|
||||
if (!rateCode || !roomTypeCode) {
|
||||
return notFound()
|
||||
}
|
||||
|
||||
const roomAvailability = await getRoomAvailability({
|
||||
hotelId: parseInt(hotelId),
|
||||
adults,
|
||||
children,
|
||||
roomStayStartDate: fromDate,
|
||||
roomStayEndDate: toDate,
|
||||
rateCode
|
||||
})
|
||||
const [
|
||||
hotelData,
|
||||
user,
|
||||
savedCreditCards,
|
||||
breakfastPackages,
|
||||
roomAvailability,
|
||||
] = await Promise.all([
|
||||
getHotelData({
|
||||
hotelId,
|
||||
language: lang,
|
||||
include: [HotelIncludeEnum.RoomCategories],
|
||||
}),
|
||||
|
||||
getProfileSafely(),
|
||||
getCreditCardsSafely(),
|
||||
getBreakfastPackages(searchParams.hotel),
|
||||
getSelectedRoomAvailability({
|
||||
hotelId: parseInt(searchParams.hotel),
|
||||
adults,
|
||||
children,
|
||||
roomStayStartDate: fromDate,
|
||||
roomStayEndDate: toDate,
|
||||
rateCode,
|
||||
roomTypeCode,
|
||||
}),
|
||||
])
|
||||
|
||||
if (!isValidStep(params.step) || !hotelData || !roomAvailability) {
|
||||
return notFound()
|
||||
@@ -100,10 +112,8 @@ export default async function StepPage({
|
||||
id: "Select payment method",
|
||||
})
|
||||
|
||||
const availableRoom = roomAvailability?.roomConfigurations
|
||||
.filter((room) => room.status === "Available")
|
||||
.find((room) => room.roomTypeCode === roomTypeCode)?.roomType
|
||||
const roomTypes = hotelData.included
|
||||
const availableRoom = roomAvailability.selectedRoom?.roomType
|
||||
const bedTypes = hotelData.included
|
||||
?.find((room) => room.name === availableRoom)
|
||||
?.roomTypes.map((room) => ({
|
||||
description: room.mainBed.description,
|
||||
@@ -116,13 +126,13 @@ export default async function StepPage({
|
||||
<HistoryStateManager />
|
||||
|
||||
{/* TODO: How to handle no beds found? */}
|
||||
{roomTypes ? (
|
||||
{bedTypes ? (
|
||||
<SectionAccordion
|
||||
header="Select bed"
|
||||
step={StepEnum.selectBed}
|
||||
label={intl.formatMessage({ id: "Request bedtype" })}
|
||||
>
|
||||
<BedType roomTypes={roomTypes} />
|
||||
<BedType bedTypes={bedTypes} />
|
||||
</SectionAccordion>
|
||||
) : null}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user