-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/devtron-labs/devtron-fe-com…
…mon-lib into user-gitops-repo
- Loading branch information
Showing
22 changed files
with
316 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { useSuperAdmin } from './UseSuperAdmin/UseSuperAdmin' | ||
export { useClickOutside } from './UseClickOutside/UseClickOutside' | ||
export { useWindowSize } from './UseWindowSize/UseWindowSize' | ||
export * from './useUrlFilters' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const DEFAULT_PAGE_NUMBER = 1 | ||
|
||
export const URL_FILTER_KEYS = { | ||
PAGE_SIZE: 'pageSize', | ||
PAGE_NUMBER: 'pageNumber', | ||
SEARCH_KEY: 'searchKey', | ||
SORT_BY: 'sortBy', | ||
SORT_ORDER: 'sortOrder', | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as useUrlFilters } from './useUrlFilters' | ||
export type { UseUrlFiltersProps } from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface UseUrlFiltersProps<T> { | ||
/** | ||
* The key on which the sorting should be applied | ||
*/ | ||
initialSortKey?: T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { useMemo } from 'react' | ||
import { useHistory, useLocation } from 'react-router-dom' | ||
import { DEFAULT_BASE_PAGE_SIZE, SortingOrder } from '../../Constants' | ||
import { DEFAULT_PAGE_NUMBER, URL_FILTER_KEYS } from './constants' | ||
import { UseUrlFiltersProps } from './types' | ||
|
||
const { PAGE_SIZE, PAGE_NUMBER, SEARCH_KEY, SORT_BY, SORT_ORDER } = URL_FILTER_KEYS | ||
|
||
/** | ||
* Generic hook for implementing URL based filters. | ||
* eg: pagination, search, sort. | ||
* | ||
* The exposed handlers can be consumed directly without the need for explicit state management | ||
* | ||
* @example Default Usage: | ||
* ```tsx | ||
* const { pageSize, changePage, ...rest } = useUrlFilters() | ||
* ``` | ||
* | ||
* @example Usage with custom type for sort keys and initial sort key: | ||
* ```tsx | ||
* const { sortBy, sortOrder } = useUrlFilters<'email' | 'name'>({ initialSortKey: 'email' }) | ||
* ``` | ||
* | ||
*/ | ||
const useUrlFilters = <T = string>({ initialSortKey }: UseUrlFiltersProps<T> = {}) => { | ||
const location = useLocation() | ||
const history = useHistory() | ||
const searchParams = new URLSearchParams(location.search) | ||
|
||
const { pageSize, pageNumber, searchKey, sortBy, sortOrder } = useMemo(() => { | ||
const _pageSize = searchParams.get(PAGE_SIZE) | ||
const _pageNumber = searchParams.get(PAGE_NUMBER) | ||
const _searchKey = searchParams.get(SEARCH_KEY) | ||
const _sortOrder = searchParams.get(SORT_ORDER) as SortingOrder | ||
const _sortBy = searchParams.get(SORT_BY) | ||
|
||
const sortByKey = (_sortBy || initialSortKey || '') as T | ||
// Fallback to ascending order | ||
const sortByOrder = Object.values(SortingOrder).includes(_sortOrder) ? _sortOrder : SortingOrder.ASC | ||
|
||
return { | ||
pageSize: Number(_pageSize) || DEFAULT_BASE_PAGE_SIZE, | ||
pageNumber: Number(_pageNumber) || DEFAULT_PAGE_NUMBER, | ||
searchKey: _searchKey || '', | ||
sortBy: sortByKey, | ||
// sort order should only be applied if the key is available | ||
sortOrder: (sortByKey ? sortByOrder : '') as SortingOrder, | ||
} | ||
}, [searchParams]) | ||
|
||
/** | ||
* Used for getting the required result from the API | ||
*/ | ||
const offset = pageSize * (pageNumber - 1) | ||
|
||
/** | ||
* Update and replace the search params in the URL. | ||
* | ||
* Note: Currently only primitive data types are supported | ||
*/ | ||
const _updateSearchParam = (key: string, value) => { | ||
searchParams.set(key, String(value)) | ||
history.replace({ search: searchParams.toString() }) | ||
} | ||
|
||
const _resetPageNumber = () => { | ||
_updateSearchParam(PAGE_NUMBER, DEFAULT_PAGE_NUMBER) | ||
} | ||
|
||
const changePage = (page: number) => { | ||
_updateSearchParam(PAGE_NUMBER, page) | ||
} | ||
|
||
const changePageSize = (_pageSize: number) => { | ||
_updateSearchParam(PAGE_SIZE, _pageSize) | ||
_resetPageNumber() | ||
} | ||
|
||
const handleSearch = (searchTerm: string) => { | ||
_updateSearchParam(SEARCH_KEY, searchTerm) | ||
_resetPageNumber() | ||
} | ||
|
||
const handleSorting = (_sortBy: T) => { | ||
let order: SortingOrder | ||
if (_sortBy === sortBy && sortOrder === SortingOrder.ASC) { | ||
order = SortingOrder.DESC | ||
} else { | ||
order = SortingOrder.ASC | ||
} | ||
|
||
_updateSearchParam(SORT_BY, _sortBy) | ||
_updateSearchParam(SORT_ORDER, order) | ||
|
||
// Reset page number on sorting change | ||
_resetPageNumber() | ||
} | ||
|
||
const clearFilters = () => { | ||
Object.values(URL_FILTER_KEYS).forEach((key) => { | ||
searchParams.delete(key) | ||
}) | ||
history.replace({ search: searchParams.toString() }) | ||
} | ||
|
||
return { | ||
pageSize, | ||
changePage, | ||
changePageSize, | ||
searchKey, | ||
handleSearch, | ||
offset, | ||
sortBy, | ||
sortOrder, | ||
handleSorting, | ||
clearFilters, | ||
} | ||
} | ||
|
||
export default useUrlFilters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Common/SortableTableHeaderCell/SortableTableHeaderCell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ReactComponent as SortIcon } from '../../Assets/Icon/ic-arrow-up-down.svg' | ||
import { ReactComponent as SortArrowDown } from '../../Assets/Icon/ic-sort-arrow-down.svg' | ||
import { SortingOrder } from '../Constants' | ||
import { SortableTableHeaderCellProps } from './types' | ||
|
||
/** | ||
* Reusable component for the table header cell with support for sorting icons | ||
* | ||
* @example Usage | ||
* ```tsx | ||
* <SortableTableHeaderCell | ||
* isSorted={currentSortedCell === 'cell'} | ||
* triggerSorting={() => {}} | ||
* sortOrder={SortingOrder.ASC} | ||
* title="Header Cell" | ||
* disabled={isDisabled} | ||
* /> | ||
* ``` | ||
*/ | ||
const SortableTableHeaderCell = ({ | ||
isSorted, | ||
triggerSorting, | ||
sortOrder, | ||
title, | ||
disabled, | ||
}: SortableTableHeaderCellProps) => ( | ||
<button | ||
type="button" | ||
className="dc__transparent p-0 bcn-0 cn-7 flex dc__content-start dc__gap-4 cursor" | ||
onClick={triggerSorting} | ||
disabled={disabled} | ||
> | ||
<span className="dc__uppercase dc__ellipsis-right">{title}</span> | ||
{isSorted ? ( | ||
<SortArrowDown | ||
className={`icon-dim-12 mw-12 scn-7 ${sortOrder === SortingOrder.DESC ? 'dc__flip-180' : ''}`} | ||
/> | ||
) : ( | ||
<SortIcon className="icon-dim-12 mw-12 scn-7" /> | ||
)} | ||
</button> | ||
) | ||
|
||
export default SortableTableHeaderCell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as SortableTableHeaderCell } from './SortableTableHeaderCell' | ||
export type { SortableTableHeaderCellProps } from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { SortingOrder } from '../Constants' | ||
|
||
export interface SortableTableHeaderCellProps { | ||
/** | ||
* If true, the cell is sorted | ||
*/ | ||
isSorted: boolean | ||
/** | ||
* Callback for handling the sorting of the cell | ||
*/ | ||
triggerSorting: () => void | ||
/** | ||
* Current sort order | ||
* | ||
* Note: On click, the sort order should be updated as required | ||
*/ | ||
sortOrder: SortingOrder | ||
/** | ||
* Label for the cell | ||
*/ | ||
title: string | ||
/** | ||
* If true, the cell is disabled | ||
*/ | ||
disabled: boolean | ||
} |
Oops, something went wrong.