Skip to content

Commit 457ed64

Browse files
committed
fix: not to update url from local storage in case of primary params
1 parent 017ea72 commit 457ed64

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/Common/Hooks/useUrlFilters/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface UseUrlFiltersProps<T, K> {
2525
* Callback function for parsing the search params
2626
*/
2727
parseSearchParams?: (searchParams: URLSearchParams) => K
28-
localStorageKey?: string
28+
localStorageKey?: `${string}__${string}`
2929
}
3030

3131
export type UseUrlFiltersReturnType<T, K = unknown> = K & {

src/Common/Hooks/useUrlFilters/useUrlFilters.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { useHistory, useLocation } from 'react-router-dom'
1919
import { DEFAULT_BASE_PAGE_SIZE, EXCLUDED_FALSY_VALUES, SortingOrder } from '../../Constants'
2020
import { DEFAULT_PAGE_NUMBER, URL_FILTER_KEYS } from './constants'
2121
import { UseUrlFiltersProps, UseUrlFiltersReturnType } from './types'
22+
import { setItemInLocalStorageIfKeyExists } from './utils'
2223

2324
const { PAGE_SIZE, PAGE_NUMBER, SEARCH_KEY, SORT_BY, SORT_ORDER } = URL_FILTER_KEYS
2425

@@ -127,7 +128,7 @@ const useUrlFilters = <T = string, K = unknown>({
127128

128129
const clearFilters = () => {
129130
history.replace({ search: '' })
130-
localStorage.setItem(localStorageKey, '')
131+
setItemInLocalStorageIfKeyExists(localStorageKey, '')
131132
}
132133

133134
const updateSearchParams = (paramsToSerialize: Partial<K>) => {
@@ -145,13 +146,26 @@ const useUrlFilters = <T = string, K = unknown>({
145146
searchParams.delete(key)
146147
}
147148
})
148-
localStorage.setItem(localStorageKey, JSON.stringify(parseSearchParams(searchParams)))
149+
// Skipping primary params => pageSize, pageNumber, searchKey, sortBy, sortOrder
150+
setItemInLocalStorageIfKeyExists(localStorageKey, JSON.stringify(parseSearchParams(searchParams)))
149151
// Not replacing the params as it is being done by _resetPageNumber
150152
_resetPageNumber()
151153
}
152154

153155
useEffect(() => {
154-
if (!localStorageKey) {
156+
// If we have pageSize || pageNumber || searchKey || sortBy || sortOrder in params, no need to change other filters
157+
const paramsSortByKey = searchParams.get(SORT_BY) || ''
158+
const paramsSortByOrder = searchParams.get(SORT_ORDER) || ''
159+
const paramsPageNumber = searchParams.get(PAGE_NUMBER) || 0
160+
const paramsPageSize = searchParams.get(PAGE_SIZE) || 0
161+
if (
162+
!localStorageKey ||
163+
!!paramsPageSize ||
164+
!!paramsPageNumber ||
165+
!!searchKey ||
166+
!!paramsSortByKey ||
167+
!!paramsSortByOrder
168+
) {
155169
return
156170
}
157171
if (
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const setItemInLocalStorageIfKeyExists = (localStorageKey: string, value: string) => {
2+
if (localStorageKey) {
3+
localStorage.setItem(localStorageKey, value)
4+
}
5+
}

0 commit comments

Comments
 (0)