Skip to content

Commit

Permalink
fix: updating params twice in local filters
Browse files Browse the repository at this point in the history
  • Loading branch information
arunjaindev committed Nov 22, 2024
1 parent 8d6bdb3 commit 269aab7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtron-labs/devtron-fe-common-lib",
"version": "0.6.5-patch-1",
"version": "0.6.5-patch-2",
"description": "Supporting common component library",
"type": "module",
"main": "dist/index.js",
Expand Down
32 changes: 29 additions & 3 deletions src/Common/Hooks/useUrlFilters/useUrlFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

import { useEffect, useMemo } from 'react'
import { useEffect, useMemo, useRef } from 'react'
import { useHistory, useLocation } from 'react-router-dom'
import { getUrlWithSearchParams } from '@Common/Helper'
import { DEFAULT_BASE_PAGE_SIZE, EXCLUDED_FALSY_VALUES, SortingOrder } from '../../Constants'
import { DEFAULT_PAGE_NUMBER, URL_FILTER_KEYS } from './constants'
import { UseUrlFiltersProps, UseUrlFiltersReturnType } from './types'
Expand Down Expand Up @@ -47,7 +48,30 @@ const useUrlFilters = <T = string, K = unknown>({
}: UseUrlFiltersProps<T, K> = {}): UseUrlFiltersReturnType<T, K> => {
const location = useLocation()
const history = useHistory()
const searchParams = new URLSearchParams(location.search)

const isAlreadyReadFromLocalStorage = useRef<boolean>(false)

const getSearchParams = () => {
if (!isAlreadyReadFromLocalStorage.current && !location.search && localStorageKey) {
isAlreadyReadFromLocalStorage.current = true
const localStorageValue = localStorage.getItem(localStorageKey)
if (localStorageValue) {
try {
const localSearchString = getUrlWithSearchParams('', JSON.parse(localStorageValue))
const localSearchParams = new URLSearchParams(localSearchString.split('?')[1] ?? '')

history.replace({ search: localSearchParams.toString() })
return localSearchParams
} catch {
localStorage.removeItem(localStorageKey)
}
}
}

return new URLSearchParams(location.search)
}

const searchParams = getSearchParams()

const getParsedSearchParams: UseUrlFiltersProps<T, K>['parseSearchParams'] = (searchParamsToParse) => {
if (parseSearchParams) {
Expand Down Expand Up @@ -136,7 +160,9 @@ const useUrlFilters = <T = string, K = unknown>({

const clearFilters = () => {
history.replace({ search: '' })
setItemInLocalStorageIfKeyExists(localStorageKey, '')
if (localStorageKey) {
localStorage.removeItem(localStorageKey)
}
}

const updateSearchParams = (paramsToSerialize: Partial<K>) => {
Expand Down

0 comments on commit 269aab7

Please sign in to comment.