fix(SW-3186): Replaced hash functionality for filters with query parameters
Approved-by: Linus Flood Approved-by: Matilda Landström
This commit is contained in:
@@ -1,41 +0,0 @@
|
|||||||
"use client"
|
|
||||||
|
|
||||||
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}</>
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
"use client"
|
|
||||||
import { useSearchParams } from "next/navigation"
|
|
||||||
import { useRef } from "react"
|
|
||||||
|
|
||||||
import { createDestinationDataStore } from "@/stores/destination-data"
|
|
||||||
|
|
||||||
import { DestinationDataContext } from "@/contexts/DestinationData"
|
|
||||||
|
|
||||||
import DestinationDataProviderContent from "./Content"
|
|
||||||
|
|
||||||
import type { DestinationDataStore } from "@/types/contexts/destination-data"
|
|
||||||
import type { DestinationDataProviderProps } from "@/types/providers/destination-data"
|
|
||||||
|
|
||||||
export default function DestinationDataProvider({
|
|
||||||
allCities = [],
|
|
||||||
allHotels,
|
|
||||||
allFilters,
|
|
||||||
filterFromUrl,
|
|
||||||
sortItems,
|
|
||||||
pathname,
|
|
||||||
children,
|
|
||||||
}: DestinationDataProviderProps) {
|
|
||||||
const storeRef = useRef<DestinationDataStore>(undefined)
|
|
||||||
const searchParams = useSearchParams()
|
|
||||||
|
|
||||||
if (!storeRef.current) {
|
|
||||||
storeRef.current = createDestinationDataStore({
|
|
||||||
allCities,
|
|
||||||
allHotels,
|
|
||||||
allFilters,
|
|
||||||
filterFromUrl,
|
|
||||||
pathname,
|
|
||||||
sortItems,
|
|
||||||
searchParams,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<DestinationDataContext.Provider value={storeRef.current}>
|
|
||||||
<DestinationDataProviderContent>
|
|
||||||
{children}
|
|
||||||
</DestinationDataProviderContent>
|
|
||||||
</DestinationDataContext.Provider>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -99,9 +99,9 @@ export default function DestinationFilterAndSort({
|
|||||||
parsedUrl.pathname += `/${firstFilter}`
|
parsedUrl.pathname += `/${firstFilter}`
|
||||||
}
|
}
|
||||||
if (remainingFilters.length > 0) {
|
if (remainingFilters.length > 0) {
|
||||||
parsedUrl.hash = `#${remainingFilters.join("&")}`
|
searchParams.set("filter", remainingFilters.join("&"))
|
||||||
} else {
|
} else {
|
||||||
parsedUrl.hash = ""
|
searchParams.delete("filter")
|
||||||
}
|
}
|
||||||
|
|
||||||
router.push(parsedUrl.toString(), { scroll: false })
|
router.push(parsedUrl.toString(), { scroll: false })
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ export default function DestinationDataProviderContent({
|
|||||||
const currentUrl = new URL(window.location.href)
|
const currentUrl = new URL(window.location.href)
|
||||||
const searchParams = currentUrl.searchParams
|
const searchParams = currentUrl.searchParams
|
||||||
const currentPathname = currentUrl.pathname
|
const currentPathname = currentUrl.pathname
|
||||||
const currentHash = currentUrl.hash
|
|
||||||
const sort = searchParams.get("sort")
|
const sort = searchParams.get("sort")
|
||||||
|
const filterParam = searchParams.get("filter")
|
||||||
const filters = []
|
const filters = []
|
||||||
const pathParts = currentPathname.split("/")
|
const pathParts = currentPathname.split("/")
|
||||||
const lastPathPart = pathParts[pathParts.length - 1]
|
const lastPathPart = pathParts[pathParts.length - 1]
|
||||||
@@ -29,9 +29,8 @@ export default function DestinationDataProviderContent({
|
|||||||
if (basePath !== currentPathname) {
|
if (basePath !== currentPathname) {
|
||||||
filters.push(lastPathPart)
|
filters.push(lastPathPart)
|
||||||
}
|
}
|
||||||
if (currentHash) {
|
if (filterParam) {
|
||||||
const hashValue = currentHash.substring(1)
|
filters.push(...filterParam.split("&"))
|
||||||
filters.push(...hashValue.split("&"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateActiveFiltersAndSort(filters, sort)
|
updateActiveFiltersAndSort(filters, sort)
|
||||||
|
|||||||
Reference in New Issue
Block a user