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:
@@ -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(
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
isValidSortOption,
|
||||
} from "./helper"
|
||||
|
||||
import type { Filter } from "@scandic-hotels/trpc/types/destinationFilterAndSort"
|
||||
import type { HotelFilter } from "@scandic-hotels/trpc/types/hotel"
|
||||
|
||||
import type {
|
||||
DestinationDataState,
|
||||
@@ -36,8 +36,8 @@ export function createDestinationDataStore({
|
||||
}: InitialState) {
|
||||
const defaultSort =
|
||||
sortItems.find((s) => s.isDefault)?.value ?? sortItems[0].value
|
||||
const allFilterSlugs = Object.values(allFilters).flatMap((filter: Filter[]) =>
|
||||
filter.map((f) => f.slug)
|
||||
const allFilterSlugs = Object.values(allFilters).flatMap(
|
||||
(filter: HotelFilter[]) => filter.map((f) => f.slug)
|
||||
)
|
||||
|
||||
const activeFilters: string[] = filterFromUrl ? [filterFromUrl] : []
|
||||
|
||||
Reference in New Issue
Block a user