Skip to content

Commit

Permalink
Shanbady/clicking item routes away from list fix (#1280)
Browse files Browse the repository at this point in the history
* adding suggested change to prevent re-load

* fixing function

* fixing lint

* getting rid of search params
  • Loading branch information
shanbady authored Jul 17, 2024
1 parent 8b03ad6 commit 65b7739
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import styled from "@emotion/styled"
import type { DrawerProps } from "@mui/material/Drawer"
import { ActionButton } from "../Button/Button"
import { RiCloseLargeLine } from "@remixicon/react"
import { useSearchParams, useLocation } from "react-router-dom"
import { useLocation, useNavigate } from "react-router-dom"
import { useToggle } from "ol-utilities"

const closeSx: React.CSSProperties = {
Expand Down Expand Up @@ -38,16 +38,17 @@ const RoutedDrawer = <K extends string, R extends K = K>(
) => {
const { requiredParams, children, onView, ...others } = props
const { params = requiredParams } = props
const [searchParams, setSearchParams] = useSearchParams()

const [open, setOpen] = useToggle(false)
const { hash } = useLocation()
const location = useLocation()
const navigate = useNavigate()

const childParams = useMemo(() => {
const searchParams = new URLSearchParams(location.search)
return Object.fromEntries(
params.map((name) => [name, searchParams.get(name)] as const),
) as Record<K, string | null>
}, [searchParams, params])
}, [location, params])

const requiredArePresent = requiredParams.every(
(name) => childParams[name] !== null,
Expand All @@ -62,15 +63,19 @@ const RoutedDrawer = <K extends string, R extends K = K>(
}, [requiredArePresent, setOpen, requiredParams])

const removeUrlParams = useCallback(() => {
setSearchParams((current) => {
const getNewParams = (current: string) => {
const newSearchParams = new URLSearchParams(current)
params.forEach((param) => {
newSearchParams.delete(param)
})
return newSearchParams
}
const newParams = getNewParams(location.search)
navigate({
...location,
search: newParams.toString(),
})
document.location.hash = hash.substring(1)
}, [setSearchParams, params, hash])
}, [params, navigate, location])

return (
<Drawer
Expand Down

0 comments on commit 65b7739

Please sign in to comment.