Skip to content

Commit

Permalink
work to limit rerendering
Browse files Browse the repository at this point in the history
  • Loading branch information
charmingduchess committed Nov 20, 2023
1 parent c51a9f9 commit 254a2d5
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/components/ItemFilters/FiltersContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useEffect, useRef, useState } from "react"
import { useMemo, useRef, useState } from "react"
import { useRouter } from "next/router"
import React from "react"
import { Text, useCloseDropDown } from "@nypl/design-system-react-components"
import {
Heading,
Text,
useCloseDropDown,
} from "@nypl/design-system-react-components"

import styles from "../../../styles/components/ItemFilters.module.scss"
import type { AppliedFilters, ItemAggregation } from "../../types/filterTypes"
Expand All @@ -27,20 +31,17 @@ const ItemFilterContainer = ({ itemAggs }: ItemFilterContainerProps) => {
else return new ItemFilterData(agg)
})
).current

const [appliedFilters, setAppliedFilters] = useState({
location: [],
format: [],
status: [],
})

const parsedParams = parseItemFilterQueryParams(router.query)
const [appliedFilters, setAppliedFilters] = useState(parsedParams)
const appliedFiltersDisplay = buildAppliedFiltersString(
parsedParams,
filterData
)
const ref = useRef<HTMLDivElement>(null)
useCloseDropDown(() => setWhichFilterIsOpen(""), ref)

const [whichFilterIsOpen, setWhichFilterIsOpen] = useState("")

const [appliedFiltersDisplay, setAppliedFiltersDisplay] = useState("")

const [itemsMatched] = useState(buildItemsString(router.query))

const submitFilters = (selection: string[], field: string) => {
Expand All @@ -57,16 +58,12 @@ const ItemFilterContainer = ({ itemAggs }: ItemFilterContainerProps) => {
locationFilterData.recapLocations()
)
router.push("/search/advanced" + url)
setAppliedFiltersDisplay(url)
}

useEffect(() => {
useMemo(() => {
const parsedParams = parseItemFilterQueryParams(router.query)
setAppliedFilters(parsedParams)
setAppliedFiltersDisplay(
buildAppliedFiltersString(parsedParams, filterData)
)
}, [router.query, filterData])
}, [router.query])

return (
<div className={styles.filtersContainer}>
Expand All @@ -85,7 +82,7 @@ const ItemFilterContainer = ({ itemAggs }: ItemFilterContainerProps) => {
/>
))}
</div>
<Text>{itemsMatched}</Text>
<Heading level="h3">{itemsMatched}</Heading>
<Text>{appliedFiltersDisplay}</Text>
</div>
)
Expand Down

0 comments on commit 254a2d5

Please sign in to comment.