@@ -19,6 +19,7 @@ import { useHistory, useLocation } from 'react-router-dom'
19
19
import { DEFAULT_BASE_PAGE_SIZE , EXCLUDED_FALSY_VALUES , SortingOrder } from '../../Constants'
20
20
import { DEFAULT_PAGE_NUMBER , URL_FILTER_KEYS } from './constants'
21
21
import { UseUrlFiltersProps , UseUrlFiltersReturnType } from './types'
22
+ import { setItemInLocalStorageIfKeyExists } from './utils'
22
23
23
24
const { PAGE_SIZE , PAGE_NUMBER , SEARCH_KEY , SORT_BY , SORT_ORDER } = URL_FILTER_KEYS
24
25
@@ -127,7 +128,7 @@ const useUrlFilters = <T = string, K = unknown>({
127
128
128
129
const clearFilters = ( ) => {
129
130
history . replace ( { search : '' } )
130
- localStorage . setItem ( localStorageKey , '' )
131
+ setItemInLocalStorageIfKeyExists ( localStorageKey , '' )
131
132
}
132
133
133
134
const updateSearchParams = ( paramsToSerialize : Partial < K > ) => {
@@ -145,13 +146,26 @@ const useUrlFilters = <T = string, K = unknown>({
145
146
searchParams . delete ( key )
146
147
}
147
148
} )
148
- localStorage . setItem ( localStorageKey , JSON . stringify ( parseSearchParams ( searchParams ) ) )
149
+ // Skipping primary params => pageSize, pageNumber, searchKey, sortBy, sortOrder
150
+ setItemInLocalStorageIfKeyExists ( localStorageKey , JSON . stringify ( parseSearchParams ( searchParams ) ) )
149
151
// Not replacing the params as it is being done by _resetPageNumber
150
152
_resetPageNumber ( )
151
153
}
152
154
153
155
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
+ ) {
155
169
return
156
170
}
157
171
if (
0 commit comments