refactor: create SortDropdownButton

This commit is contained in:
Sammy Teillet
2023-04-24 17:35:01 +02:00
parent 7cf1f913b0
commit 795f9e7af8
2 changed files with 50 additions and 18 deletions

View File

@ -0,0 +1,38 @@
import { useCallback } from 'react';
import DropdownButton from './DropdownButton';
import { SortType } from './SortAndFilterBar';
type OwnProps = {
sorts: SortType[];
setSorts: any;
sortsAvailable: any;
};
export function SortDropdownButton({
sortsAvailable,
setSorts,
sorts,
}: OwnProps) {
const onSortItemSelect = useCallback(
(sortId: string) => {
const newSorts = [
{
label: 'Created at',
order: 'asc',
id: sortId,
} satisfies SortType,
];
setSorts(newSorts);
},
[setSorts],
);
return (
<DropdownButton
label="Sort"
options={sortsAvailable}
onSortSelect={onSortItemSelect}
isActive={sorts.length > 0}
/>
);
}

View File

@ -4,6 +4,7 @@ import DropdownButton from './DropdownButton';
import { IconProp } from '@fortawesome/fontawesome-svg-core';
import SortAndFilterBar, { SortType } from './SortAndFilterBar';
import { useCallback, useState } from 'react';
import { SortDropdownButton } from './SortDropdownButton';
type OwnProps = {
viewName: string;
@ -50,19 +51,12 @@ function TableHeader({
onSortsUpdate,
sortsAvailable,
}: OwnProps) {
const [sorts, setSorts] = useState([] as Array<SortType>);
const [sorts, innerSetSorts] = useState([] as Array<SortType>);
const onSortItemSelect = useCallback(
(sortId: string) => {
const newSorts = [
{
label: 'Created at',
order: 'asc',
id: sortId,
} satisfies SortType,
];
setSorts(newSorts);
onSortsUpdate && onSortsUpdate(newSorts);
const setSorts = useCallback(
(sorts: SortType[]) => {
innerSetSorts(sorts);
onSortsUpdate && onSortsUpdate(sorts);
},
[onSortsUpdate],
);
@ -70,7 +64,7 @@ function TableHeader({
const onSortItemUnSelect = useCallback(
(sortId: string) => {
const newSorts = [] as SortType[];
setSorts(newSorts);
innerSetSorts(newSorts);
onSortsUpdate && onSortsUpdate(newSorts);
},
[onSortsUpdate],
@ -87,12 +81,12 @@ function TableHeader({
</StyledViewSection>
<StyledFilters>
<DropdownButton label="Filter" options={[]} isActive={false} />
<DropdownButton
label="Sort"
options={sortsAvailable}
onSortSelect={onSortItemSelect}
isActive={sorts.length > 0}
<SortDropdownButton
setSorts={setSorts}
sorts={sorts}
sortsAvailable={sortsAvailable}
/>
<DropdownButton label="Settings" options={[]} isActive={false} />
</StyledFilters>
</StyledTableHeader>