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:
@@ -0,0 +1,38 @@
|
||||
"use client"
|
||||
|
||||
import { useParams } from "next/navigation"
|
||||
import { useEffect } from "react"
|
||||
|
||||
import { useHotelListingDataStore } from "@/stores/hotel-listing-data"
|
||||
|
||||
export default function HotelListingDataProviderContent({
|
||||
children,
|
||||
}: React.PropsWithChildren) {
|
||||
const params = useParams()
|
||||
const { updateActiveFiltersAndSort, allFilterSlugs } =
|
||||
useHotelListingDataStore((state) => ({
|
||||
allFilterSlugs: state.allFilterSlugs,
|
||||
updateActiveFiltersAndSort: state.actions.updateActiveFiltersAndSort,
|
||||
}))
|
||||
|
||||
useEffect(() => {
|
||||
const currentUrl = new URL(window.location.href)
|
||||
const searchParams = currentUrl.searchParams
|
||||
const sort = searchParams.get("sort")
|
||||
const filterParam = searchParams.get("filter")
|
||||
const activeFilters: string[] = []
|
||||
|
||||
if (filterParam) {
|
||||
const filters = filterParam.split(",")
|
||||
filters.forEach((filter) => {
|
||||
if (allFilterSlugs.includes(filter)) {
|
||||
activeFilters.push(filter)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
updateActiveFiltersAndSort(activeFilters, sort)
|
||||
}, [params, updateActiveFiltersAndSort, allFilterSlugs])
|
||||
|
||||
return <>{children}</>
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
"use client"
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import { useRef } from "react"
|
||||
|
||||
import { createHotelListingDataStore } from "@/stores/hotel-listing-data"
|
||||
|
||||
import { HotelListingDataContext } from "@/contexts/HotelListingData"
|
||||
|
||||
import HotelListingDataProviderContent from "./Content"
|
||||
|
||||
import type { HotelListingDataStore } from "@/types/contexts/hotel-listing-data"
|
||||
import type { HotelListingDataProviderProps } from "@/types/providers/hotel-listing-data"
|
||||
|
||||
export default function HotelListingDataProvider({
|
||||
allHotels,
|
||||
allFilters,
|
||||
sortItems,
|
||||
children,
|
||||
}: HotelListingDataProviderProps) {
|
||||
const storeRef = useRef<HotelListingDataStore>(undefined)
|
||||
const searchParams = useSearchParams()
|
||||
|
||||
if (!storeRef.current) {
|
||||
storeRef.current = createHotelListingDataStore({
|
||||
allHotels,
|
||||
allFilters,
|
||||
sortItems,
|
||||
searchParams,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<HotelListingDataContext.Provider value={storeRef.current}>
|
||||
<HotelListingDataProviderContent>
|
||||
{children}
|
||||
</HotelListingDataProviderContent>
|
||||
</HotelListingDataContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user