Merged in feature/bookingwidget-client-side (pull request #1481)

Move more of BookingWidget to client SW-1639

* feat: move getLocations in booking widget to client side so that it's also cached on the client reducing the blinking when switching urls (and reducing duplicate calls)


Approved-by: Linus Flood
This commit is contained in:
Joakim Jäderberg
2025-03-05 13:37:33 +00:00
parent 916912a170
commit f36b90e474
10 changed files with 54 additions and 62 deletions

View File

@@ -140,3 +140,7 @@ export const getHotelsByCountryInput = z.object({
export const getHotelsByCityIdentifierInput = z.object({
cityIdentifier: z.string(),
})
export const getLocationsInput = z.object({
lang: z.nativeEnum(Lang),
})

View File

@@ -30,6 +30,7 @@ import {
getHotelsByCountryInput,
getHotelsByCSFilterInput,
getHotelsByHotelIdsAvailabilityInputSchema,
getLocationsInput,
getMeetingRoomsInputSchema,
hotelInputSchema,
hotelsAvailabilityInputSchema,
@@ -1272,9 +1273,14 @@ export const hotelQueryRouter = router({
return validateHotelData.data.map((id: string) => parseInt(id, 10))
}),
locations: router({
get: serviceProcedure.query(async function ({ ctx }) {
get: serviceProcedure.input(getLocationsInput).query(async function ({
input,
ctx,
}) {
const lang = input.lang ?? ctx.lang
const searchParams = new URLSearchParams()
searchParams.set("language", toApiLang(ctx.lang))
searchParams.set("language", toApiLang(lang))
const options: RequestOptionsWithOutBody = {
// needs to clear default option as only
@@ -1288,29 +1294,27 @@ export const hotelQueryRouter = router({
},
}
const countries = await getCountries(options, searchParams, ctx.lang)
const countries = await getCountries(options, searchParams, lang)
if (!countries) {
return null
throw new Error("Unable to fetch countries")
}
const countryNames = countries.data.map((country) => country.name)
const citiesByCountry = await getCitiesByCountry(
countryNames,
options,
searchParams,
ctx.lang
lang
)
const locations = await getLocations(
ctx.lang,
lang,
options,
searchParams,
citiesByCountry
)
if (Array.isArray(locations)) {
return {
data: locations,
}
if (!locations || "error" in locations) {
throw new Error("Unable to fetch locations")
}
return locations