feat(BOOK-463): Fetching hotel filters from CMS and using these inside the destination pages and select hotel page
* feat(BOOK-463): Fetching hotel filters from CMS and using these inside the destination pages * fix(BOOK-698): fetch hotel filters from CMS on select hotel page Approved-by: Bianca Widstam
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import {
|
||||
type HotelFilter,
|
||||
type HotelListingHotelData,
|
||||
type HotelSortItem,
|
||||
HotelSortOption,
|
||||
} from "@scandic-hotels/trpc/types/hotel"
|
||||
|
||||
import type {
|
||||
HotelFilter,
|
||||
HotelFilters,
|
||||
} from "@scandic-hotels/trpc/routers/hotels/filters/output"
|
||||
import type { DestinationCityListItem } from "@scandic-hotels/trpc/types/destinationCityPage"
|
||||
import type { DestinationFilter } from "@scandic-hotels/trpc/types/destinationsData"
|
||||
|
||||
@@ -32,7 +35,9 @@ export function getFilteredHotels(
|
||||
if (filters.length) {
|
||||
return hotels.filter(({ hotel }) =>
|
||||
filters.every((filter) =>
|
||||
hotel.detailedFacilities.some((facility) => facility.id === filter.id)
|
||||
hotel.detailedFacilities.some(
|
||||
(facility) => facility.id === Number(filter.id)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -96,3 +101,81 @@ export function getActiveDestinationFilter(
|
||||
}
|
||||
return allSeoFilters.find((f) => f.filter.id === filterFromUrl.id) || null
|
||||
}
|
||||
|
||||
export function getFiltersWithHotelAndCityCount(
|
||||
hotelFilters: HotelFilters,
|
||||
hotels: HotelListingHotelData[],
|
||||
cities: DestinationCityListItem[]
|
||||
): HotelFilters {
|
||||
const flattenedFilters = Object.values(hotelFilters).flat()
|
||||
const filterHotelCounts: Record<string, number> = {}
|
||||
const filterCityIdentifiers: Record<string, Set<string>> = {}
|
||||
|
||||
hotels.forEach(({ hotel }) => {
|
||||
hotel.detailedFacilities.forEach((facility) => {
|
||||
filterHotelCounts[facility.id] = (filterHotelCounts[facility.id] || 0) + 1
|
||||
|
||||
if (!filterCityIdentifiers[facility.id]) {
|
||||
filterCityIdentifiers[facility.id] = new Set()
|
||||
}
|
||||
if (hotel.cityIdentifier) {
|
||||
filterCityIdentifiers[facility.id].add(hotel.cityIdentifier)
|
||||
}
|
||||
})
|
||||
|
||||
if (hotel.countryCode) {
|
||||
filterHotelCounts[hotel.countryCode] =
|
||||
(filterHotelCounts[hotel.countryCode] || 0) + 1
|
||||
|
||||
if (!filterCityIdentifiers[hotel.countryCode]) {
|
||||
filterCityIdentifiers[hotel.countryCode] = new Set()
|
||||
}
|
||||
if (hotel.cityIdentifier) {
|
||||
filterCityIdentifiers[hotel.countryCode].add(hotel.cityIdentifier)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Count cities that match the cityIdentifiers for each filter
|
||||
const filterCityCounts: Record<string, number> = {}
|
||||
Object.entries(filterCityIdentifiers).forEach(
|
||||
([filterId, cityIdentifiers]) => {
|
||||
filterCityCounts[filterId] = cities.filter(
|
||||
(city) =>
|
||||
city.destination_settings.city &&
|
||||
cityIdentifiers.has(city.destination_settings.city)
|
||||
).length
|
||||
}
|
||||
)
|
||||
|
||||
return flattenedFilters.reduce(
|
||||
(acc, filter) => {
|
||||
if (filter.filterType === "facility") {
|
||||
acc.facilityFilters.push({
|
||||
...filter,
|
||||
hotelCount: filterHotelCounts[filter.id] ?? 0,
|
||||
cityCount: filterCityCounts[filter.id] ?? 0,
|
||||
})
|
||||
} else if (filter.filterType === "surroundings") {
|
||||
acc.surroundingsFilters.push({
|
||||
...filter,
|
||||
hotelCount: filterHotelCounts[filter.id] ?? 0,
|
||||
cityCount: filterCityCounts[filter.id] ?? 0,
|
||||
})
|
||||
} else if (filter.filterType === "country") {
|
||||
acc.countryFilters.push({
|
||||
...filter,
|
||||
filterType: "country",
|
||||
hotelCount: filterHotelCounts[filter.id] ?? 0,
|
||||
cityCount: filterCityCounts[filter.id] ?? 0,
|
||||
})
|
||||
}
|
||||
return acc
|
||||
},
|
||||
{
|
||||
facilityFilters: [],
|
||||
surroundingsFilters: [],
|
||||
countryFilters: [],
|
||||
} as HotelFilters
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user