Skip to content

Commit 3558214

Browse files
committed
feat: add support for local storage ilters in use url filters
1 parent a0e2669 commit 3558214

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.5.8",
3+
"version": "0.5.8-beta-6",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Hooks/useUrlFilters/types.ts

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

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

src/Common/Hooks/useUrlFilters/useUrlFilters.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useMemo } from 'react'
17+
import { useEffect, useMemo } from 'react'
1818
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'
@@ -42,6 +42,7 @@ const { PAGE_SIZE, PAGE_NUMBER, SEARCH_KEY, SORT_BY, SORT_ORDER } = URL_FILTER_K
4242
const useUrlFilters = <T = string, K = unknown>({
4343
initialSortKey,
4444
parseSearchParams,
45+
localStorageKey,
4546
}: UseUrlFiltersProps<T, K> = {}): UseUrlFiltersReturnType<T, K> => {
4647
const location = useLocation()
4748
const history = useHistory()
@@ -126,6 +127,7 @@ const useUrlFilters = <T = string, K = unknown>({
126127

127128
const clearFilters = () => {
128129
history.replace({ search: '' })
130+
localStorage.setItem(localStorageKey, '')
129131
}
130132

131133
const updateSearchParams = (paramsToSerialize: Partial<K>) => {
@@ -143,10 +145,31 @@ const useUrlFilters = <T = string, K = unknown>({
143145
searchParams.delete(key)
144146
}
145147
})
148+
localStorage.setItem(localStorageKey, JSON.stringify(parseSearchParams(searchParams)))
146149
// Not replacing the params as it is being done by _resetPageNumber
147150
_resetPageNumber()
148151
}
149152

153+
useEffect(() => {
154+
if (!localStorageKey) {
155+
return
156+
}
157+
if (
158+
Object.keys(parsedParams).some(
159+
(key) =>
160+
(Array.isArray(parsedParams[key]) && parsedParams[key].length) ||
161+
(typeof parsedParams[key] === 'string' && !!parsedParams[key]),
162+
)
163+
) {
164+
localStorage.setItem(localStorageKey, JSON.stringify(parsedParams))
165+
} else {
166+
const localStorageValue = localStorage.getItem(localStorageKey)
167+
if (localStorageValue) {
168+
updateSearchParams(JSON.parse(localStorageValue))
169+
}
170+
}
171+
}, [])
172+
150173
return {
151174
pageSize,
152175
changePage,

src/Shared/Components/SelectPicker/FilterSelectPicker.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import { useEffect, useMemo, useState } from 'react'
66
import { ReactComponent as ICFilter } from '@Icons/ic-filter.svg'
77
import { ReactComponent as ICFilterApplied } from '@Icons/ic-filter-applied.svg'
8+
import { ComponentSizeType } from '@Shared/constants'
89
import SelectPicker from './SelectPicker.component'
910
import { FilterSelectPickerProps, SelectPickerOptionType, SelectPickerProps } from './type'
11+
import { Button } from '../Button'
1012

1113
const FilterSelectPicker = ({
1214
appliedFilterOptions,
@@ -58,14 +60,13 @@ const FilterSelectPicker = ({
5860

5961
return (
6062
<div className="p-8 dc__border-top-n1">
61-
<button
62-
type="button"
63-
className="dc__unset-button-styles w-100 br-4 h-28 flex bcb-5 cn-0 fw-6 lh-28 fs-12 h-28 br-4 pt-5 pr-12 pb-5 pl-12"
63+
<Button
64+
text="Apply"
65+
dataTestId="filter-select-picker-apply"
6466
onClick={handleApplyClick}
65-
aria-label="Apply filters"
66-
>
67-
Apply
68-
</button>
67+
size={ComponentSizeType.small}
68+
fullWidth
69+
/>
6970
</div>
7071
)
7172
}

src/Shared/Components/SelectPicker/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ export interface FilterSelectPickerProps
266266
| 'shouldMenuAlignRight'
267267
| 'optionListError'
268268
| 'reloadOptionList'
269+
| 'isOptionDisabled'
269270
> {
270271
appliedFilterOptions: SelectPickerOptionType[]
271272
handleApplyFilter: (filtersToApply: SelectPickerOptionType<number | string>[]) => void

0 commit comments

Comments
 (0)