Feat/SW-1451 country page filtering and sorting * feat(SW-1451): implemented sorting and filtering on country pages * feat(SW-1451): Renamed hotel-data to destination-data because of its multi-purpose use * feat(SW-1451): Now filtering after change of url instead of inside the store after submit Approved-by: Fredrik Thorsson
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { useParams } from "next/navigation"
|
|
import { useEffect } from "react"
|
|
|
|
import { useDestinationDataStore } from "@/stores/destination-data"
|
|
|
|
export default function DestinationDataProviderContent({
|
|
children,
|
|
}: React.PropsWithChildren) {
|
|
const params = useParams()
|
|
const { basePath, updateActiveFiltersAndSort } = useDestinationDataStore(
|
|
(state) => ({
|
|
basePath: state.basePathnameWithoutFilters,
|
|
updateActiveFiltersAndSort: state.actions.updateActiveFiltersAndSort,
|
|
})
|
|
)
|
|
|
|
useEffect(() => {
|
|
const currentUrl = new URL(window.location.href)
|
|
const searchParams = currentUrl.searchParams
|
|
const currentPathname = currentUrl.pathname
|
|
const currentHash = currentUrl.hash
|
|
const sort = searchParams.get("sort")
|
|
const filters = []
|
|
const pathParts = currentPathname.split("/")
|
|
const lastPathPart = pathParts[pathParts.length - 1]
|
|
|
|
if (basePath !== currentPathname) {
|
|
filters.push(lastPathPart)
|
|
}
|
|
if (currentHash) {
|
|
const hashValue = currentHash.substring(1)
|
|
filters.push(...hashValue.split("&"))
|
|
}
|
|
|
|
updateActiveFiltersAndSort(filters, sort)
|
|
}, [params, updateActiveFiltersAndSort, basePath])
|
|
|
|
return <>{children}</>
|
|
}
|