Feat/SW-2271 hotel list filtering

* feat(SW-2271): Changes to hotel data types in preperation for filtering
* feat(SW-2271): Added filter and sort functionality

Approved-by: Matilda Landström
This commit is contained in:
Erik Tiekstra
2025-07-04 09:27:20 +00:00
parent 82e21af0d4
commit fa7214cb58
58 changed files with 1572 additions and 450 deletions

View File

@@ -1,29 +1,30 @@
import { SortOption } from "@scandic-hotels/trpc/enums/destinationFilterAndSort"
import {
type HotelListingHotelData,
type HotelSortItem,
HotelSortOption,
} from "@scandic-hotels/trpc/types/hotel"
import type { DestinationCityListItem } from "@scandic-hotels/trpc/types/destinationCityPage"
import type { DestinationPagesHotelData } from "@scandic-hotels/trpc/types/hotel"
import type { SortItem } from "@/types/components/destinationFilterAndSort"
const HOTEL_SORTING_STRATEGIES: Partial<
Record<
SortOption,
(a: DestinationPagesHotelData, b: DestinationPagesHotelData) => number
HotelSortOption,
(a: HotelListingHotelData, b: HotelListingHotelData) => number
>
> = {
[SortOption.Name]: function (a, b) {
[HotelSortOption.Name]: function (a, b) {
return a.hotel.name.localeCompare(b.hotel.name)
},
[SortOption.TripAdvisorRating]: function (a, b) {
[HotelSortOption.TripAdvisorRating]: function (a, b) {
return (b.hotel.tripadvisor ?? 0) - (a.hotel.tripadvisor ?? 0)
},
[SortOption.Distance]: function (a, b) {
[HotelSortOption.Distance]: function (a, b) {
return a.hotel.location.distanceToCentre - b.hotel.location.distanceToCentre
},
}
export function getFilteredHotels(
hotels: DestinationPagesHotelData[],
hotels: HotelListingHotelData[],
filters: string[]
) {
if (filters.length) {
@@ -37,7 +38,7 @@ export function getFilteredHotels(
}
export function getFilteredCities(
filteredHotels: DestinationPagesHotelData[],
filteredHotels: HotelListingHotelData[],
cities: DestinationCityListItem[]
) {
const filteredCityIdentifiers = filteredHotels.map(
@@ -52,8 +53,8 @@ export function getFilteredCities(
}
export function getSortedHotels(
hotels: DestinationPagesHotelData[],
sortOption: SortOption
hotels: HotelListingHotelData[],
sortOption: HotelSortOption
) {
const sortFn = HOTEL_SORTING_STRATEGIES[sortOption]
return sortFn ? [...hotels].sort(sortFn) : hotels
@@ -61,9 +62,9 @@ export function getSortedHotels(
export function isValidSortOption(
value: string,
sortItems: SortItem[]
): value is SortOption {
return sortItems.map((item) => item.value).includes(value as SortOption)
sortItems: HotelSortItem[]
): value is HotelSortOption {
return sortItems.map((item) => item.value).includes(value as HotelSortOption)
}
export function getBasePathNameWithoutFilters(