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

Speed up show & hide filter #5411

Merged
merged 3 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion packages/ra-core/src/controller/useListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ export interface ListControllerProps<RecordType extends Record = Record> {
perPage: number;
resource: string;
selectedIds: Identifier[];
setFilters: (filters: any, displayedFilters: any) => void;
setFilters: (
filters: any,
displayedFilters: any,
debounce?: boolean
) => void;
setPage: (page: number) => void;
setPerPage: (page: number) => void;
setSort: (sort: string, order?: string) => void;
Expand Down
79 changes: 45 additions & 34 deletions packages/ra-core/src/controller/useListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,52 +197,63 @@ const useListParams = ({
);
const displayedFilterValues = query.displayedFilters || emptyObject;

const debouncedSetFilters = lodashDebounce(
(newFilters, newDisplayedFilters) => {
let payload = {
filter: removeEmpty(newFilters),
displayedFilters: undefined,
};
if (newDisplayedFilters) {
payload.displayedFilters = Object.keys(
newDisplayedFilters
).reduce((filters, filter) => {
return newDisplayedFilters[filter]
? { ...filters, [filter]: true }
: filters;
}, {});
}
changeParams({
type: SET_FILTER,
payload,
});
},
debounce
);
const debouncedSetFilters = lodashDebounce((filter, displayedFilters) => {
changeParams({
type: SET_FILTER,
payload: {
filter: removeEmpty(filter),
displayedFilters,
},
});
}, debounce);

const setFilters = useCallback(
(filters, displayedFilters) =>
debouncedSetFilters(filters, displayedFilters),
(filter, displayedFilters, debounce = true) =>
debounce
? debouncedSetFilters(filter, displayedFilters)
: changeParams({
type: SET_FILTER,
payload: {
filter: removeEmpty(filter),
displayedFilters,
},
}),
requestSignature // eslint-disable-line react-hooks/exhaustive-deps
);

const hideFilter = useCallback((filterName: string) => {
const newFilters = removeKey(filterValues, filterName);
const newDisplayedFilters = {
...displayedFilterValues,
[filterName]: undefined,
};

setFilters(newFilters, newDisplayedFilters);
// we don't use lodash.set() for displayed filters
// to avoid problems with compound filter names (e.g. 'author.name')
const displayedFilters = Object.keys(displayedFilterValues).reduce(
(filters, filter) => {
return filter !== filterName
? { ...filters, [filter]: true }
: filters;
},
{}
);
Comment on lines +227 to +234
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it is more easy to read with a simple loop ?

Suggested change
const displayedFilters = Object.keys(displayedFilterValues).reduce(
(filters, filter) => {
return filter !== filterName
? { ...filters, [filter]: true }
: filters;
},
{}
);
const displayedFilters = {}
for(filter in displayedFilterValues) {
if(filter !== filterName) displayedFilters[filter] = true
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a matter of taste... and I don't particularly like for loops.

const filter = removeEmpty(removeKey(filterValues, filterName));
changeParams({
type: SET_FILTER,
payload: { filter, displayedFilters },
});
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps

const showFilter = useCallback((filterName: string, defaultValue: any) => {
const newFilters = set(filterValues, filterName, defaultValue);
const newDisplayedFilters = {
// we don't use lodash.set() for displayed filters
// to avoid problems with compound filter names (e.g. 'author.name')
const displayedFilters = {
...displayedFilterValues,
[filterName]: true,
};
setFilters(newFilters, newDisplayedFilters);
const filter = set(filterValues, filterName, defaultValue);
changeParams({
type: SET_FILTER,
payload: {
filter,
displayedFilters,
},
});
}, requestSignature); // eslint-disable-line react-hooks/exhaustive-deps

return [
Expand Down
18 changes: 12 additions & 6 deletions packages/ra-ui-materialui/src/list/filter/FilterListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,20 @@ const FilterListItem: FC<{ label: string; value: any }> = props => {
);

const addFilter = () => {
setFilters({ ...filterValues, ...value }, null);
setFilters({ ...filterValues, ...value }, null, false);
};

const removeFilter = () => {
const inverseValue = Object.keys(value).reduce((acc, key) => {
acc[key] = undefined;
return acc;
}, {} as any);
setFilters({ ...filterValues, ...inverseValue }, null);
const keysToRemove = Object.keys(value);
const filters = Object.keys(filterValues).reduce(
(acc, key) =>
keysToRemove.includes(key)
? acc
: { ...acc, [key]: filterValues[key] },
{}
);
Comment on lines +161 to +167
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are agree with the previous comment, this one can also be written with a loop:

Suggested change
const filters = Object.keys(filterValues).reduce(
(acc, key) =>
keysToRemove.includes(key)
? acc
: { ...acc, [key]: filterValues[key] },
{}
);
const filters = {}
for(filter in filters) {
if(keysToRemove.includes(filter)) filters[key] = filterValues[filter]
}


setFilters(filters, null, false);
};

const toggleFilter = () => (isSelected ? removeFilter() : addFilter());
Expand Down