@@ -19,6 +19,7 @@ import { useHistory, useLocation } from 'react-router-dom'
1919import { DEFAULT_BASE_PAGE_SIZE , EXCLUDED_FALSY_VALUES , SortingOrder } from '../../Constants'
2020import { DEFAULT_PAGE_NUMBER , URL_FILTER_KEYS } from './constants'
2121import { UseUrlFiltersProps , UseUrlFiltersReturnType } from './types'
22+ import { setItemInLocalStorageIfKeyExists } from './utils'
2223
2324const { 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 (
0 commit comments