Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add permanentSorter for useTable and useSimpleList #1430

Merged
merged 5 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add permanentSorter for useTable and useSimpleList
  • Loading branch information
omeraplak committed Jan 13, 2022
commit 9f116a97e9610241d45fee7392282a98759a2604
24 changes: 24 additions & 0 deletions packages/core/src/definitions/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CrudOperators,
CrudSorting,
CrudFilter,
CrudSort,
} from "../../interfaces";
import {
SortOrder,
Expand Down Expand Up @@ -188,6 +189,9 @@ export const mapAntdFilterToCrudFilter = (
export const compareFilters = (left: CrudFilter, right: CrudFilter): boolean =>
left.field == right.field && left.operator == right.operator;

export const compareSorters = (left: CrudSort, right: CrudSort): boolean =>
left.field == right.field && left.order == right.order;

// Keep only one CrudFilter per type according to compareFilters
// Items in the array that is passed first to unionWith have higher priority
// CrudFilter items with undefined values are necessary to signify no filter
Expand All @@ -205,6 +209,18 @@ export const unionFilters = (
crudFilter.value !== undefined && crudFilter.value !== null,
);

export const unionSorters = (
permanentSorter: CrudSorting,
newSorters: CrudSorting,
prevSorters: CrudSorting,
): CrudSorting =>
reverse(
unionWith(permanentSorter, newSorters, prevSorters, compareSorters),
).filter(
(crudSorter) =>
crudSorter.order !== undefined && crudSorter.order !== null,
);

// Prioritize filters in the permanentFilter and put it at the end of result array
export const setInitialFilters = (
permanentFilter: CrudFilters,
Expand All @@ -213,3 +229,11 @@ export const setInitialFilters = (
...differenceWith(defaultFilter, permanentFilter, compareFilters),
...permanentFilter,
];

export const setInitialSorters = (
permanentSorter: CrudSorting,
defaultSorter: CrudSorting,
): CrudSorting => [
...differenceWith(defaultSorter, permanentSorter, compareSorters),
...permanentSorter,
];
12 changes: 9 additions & 3 deletions packages/core/src/hooks/list/useSimpleList/useSimpleList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ import {
stringifyTableParams,
unionFilters,
setInitialFilters,
setInitialSorters,
unionSorters,
} from "@definitions/table";

export type useSimpleListProps<TData, TError, TSearchVariables = unknown> =
ListProps<TData> & {
permanentFilter?: CrudFilters;
syncWithLocation?: boolean;
resource?: string;
initialFilter?: CrudFilters;
permanentFilter?: CrudFilters;
initialSorter?: CrudSorting;
permanentSorter?: CrudSorting;
onSearch?: (
data: TSearchVariables,
) => CrudFilters | Promise<CrudFilters>;
Expand Down Expand Up @@ -76,6 +79,7 @@ export const useSimpleList = <
initialSorter,
initialFilter,
permanentFilter = [],
permanentSorter = [],
onSearch,
queryOptions,
syncWithLocation: syncWithLocationProp,
Expand Down Expand Up @@ -140,7 +144,9 @@ export const useSimpleList = <
const [filters, setFilters] = useState<CrudFilters>(
setInitialFilters(permanentFilter, defaultFilter ?? []),
);
const [sorter, setSorter] = useState<CrudSorting>(defaultSorter ?? []);
const [sorter, setSorter] = useState<CrudSorting>(
setInitialSorters(permanentSorter, defaultSorter ?? []),
);

useEffect(() => {
if (syncWithLocation) {
Expand All @@ -165,7 +171,7 @@ export const useSimpleList = <
pageSize,
},
filters: unionFilters(permanentFilter, [], filters),
sort: sorter,
sort: unionSorters(permanentSorter, [], sorter),
},
queryOptions,
successNotification,
Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/hooks/table/useTable/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
mapAntdFilterToCrudFilter,
unionFilters,
setInitialFilters,
setInitialSorters,
unionSorters,
} from "@definitions/table";

import {
Expand All @@ -38,12 +40,13 @@ import {
} from "../../../interfaces";

export type useTableProps<TData, TError, TSearchVariables = unknown> = {
permanentFilter?: CrudFilters;
resource?: string;
initialCurrent?: number;
initialPageSize?: number;
initialSorter?: CrudSorting;
permanentSorter?: CrudSorting;
initialFilter?: CrudFilters;
permanentFilter?: CrudFilters;
syncWithLocation?: boolean;
onSearch?: (data: TSearchVariables) => CrudFilters | Promise<CrudFilters>;
queryOptions?: UseQueryOptions<GetListResponse<TData>, TError>;
Expand Down Expand Up @@ -71,18 +74,20 @@ export type useTableReturnType<
*/

const defaultPermanentFilter: CrudFilters = [];
const defaultPermanentSorter: CrudSorting = [];

export const useTable = <
TData extends BaseRecord = BaseRecord,
TError extends HttpError = HttpError,
TSearchVariables = unknown,
>({
onSearch,
permanentFilter = defaultPermanentFilter,
initialCurrent = 1,
initialPageSize = 10,
initialSorter,
initialFilter,
permanentSorter = defaultPermanentSorter,
permanentFilter = defaultPermanentFilter,
syncWithLocation: syncWithLocationProp,
resource: resourceFromProp,
successNotification,
Expand Down Expand Up @@ -140,7 +145,9 @@ export const useTable = <

const resource = resourceWithRoute(resourceFromProp ?? routeResourceName);

const [sorter, setSorter] = useState<CrudSorting>(defaultSorter || []);
const [sorter, setSorter] = useState<CrudSorting>(
setInitialSorters(permanentSorter, defaultSorter ?? []),
);
const [filters, setFilters] = useState<CrudFilters>(
setInitialFilters(permanentFilter, defaultFilter ?? []),
);
Expand Down Expand Up @@ -183,7 +190,7 @@ export const useTable = <
pageSize: pageSizeSF,
},
filters: unionFilters(permanentFilter, [], filters),
sort: sorter,
sort: unionSorters(permanentSorter, [], sorter),
},
queryOptions,
successNotification,
Expand Down Expand Up @@ -212,7 +219,10 @@ export const useTable = <

// Map Antd:Sorter -> refine:CrudSorting
const crudSorting = mapAntdSorterToCrudSorting(sorter);
setSorter(crudSorting);
console.log("crudSorting", crudSorting);
setSorter(() => unionSorters(permanentSorter, crudSorting, []));

console.log("sorter", { sorter });

tablePropsSunflower.onChange(pagination, filters, sorter);
};
Expand Down