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

Feat reducer payload #10381

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
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
78 changes: 52 additions & 26 deletions packages/ra-core/src/controller/list/queryReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,54 @@ export const HIDE_FILTER = 'HIDE_FILTER';
const oppositeOrder = direction =>
direction === SORT_DESC ? SORT_ASC : SORT_DESC;

type SetSortPayload = {
field: string;
order?: typeof SORT_ASC | typeof SORT_DESC;
};

type SetPagePayload = number;

type SetPerPagePayload = number;

type SetFilterPayload = {
filter: any;
displayedFilters?: { [key: string]: boolean };
};

type SetShowFilterPayload = { filterName: string; defaultValue?: any };

type SetHideFilterPayload = string;

type SetDefaultPayload = ListParams;

type ActionTypes =
| {
type: typeof SET_SORT;
payload: {
field: string;
order?: typeof SORT_ASC | typeof SORT_DESC;
};
payload: SetSortPayload;
}
| {
type: typeof SET_PAGE;
payload: number;
payload: SetPagePayload;
}
| {
type: typeof SET_PER_PAGE;
payload: number;
payload: SetPerPagePayload;
}
| {
type: typeof SET_FILTER;
payload: {
filter: any;
displayedFilters?: { [key: string]: boolean };
};
payload: SetFilterPayload;
}
| {
type: typeof SHOW_FILTER;
payload: { filterName: string; defaultValue?: any };
payload: SetShowFilterPayload;
}
| {
type: typeof HIDE_FILTER;
payload: string;
payload: SetHideFilterPayload;
}
| {
type: undefined;
payload: SetDefaultPayload;
};

/**
Expand All @@ -57,10 +75,11 @@ type ActionTypes =
export const queryReducer: Reducer<ListParams, ActionTypes> = (
previousState,
action
) => {
): ListParams => {
switch (action.type) {
case SET_SORT:
if (action.payload.field === previousState.sort) {
const { field } = action.payload as SetSortPayload;
if (field === previousState.sort) {
return {
...previousState,
order: oppositeOrder(previousState.order),
Expand All @@ -70,16 +89,20 @@ export const queryReducer: Reducer<ListParams, ActionTypes> = (

return {
...previousState,
sort: action.payload.field,
sort: field,
order: action.payload.order || SORT_ASC,
page: 1,
};

case SET_PAGE:
return { ...previousState, page: action.payload };
return { ...previousState, page: action.payload as SetPagePayload };

case SET_PER_PAGE:
return { ...previousState, page: 1, perPage: action.payload };
return {
...previousState,
page: 1,
perPage: action.payload as SetPerPagePayload,
};

case SET_FILTER: {
return {
Expand All @@ -93,28 +116,28 @@ export const queryReducer: Reducer<ListParams, ActionTypes> = (
}

case SHOW_FILTER: {
const {
filterName,
defaultValue,
} = action.payload as SetShowFilterPayload;
if (
previousState.displayedFilters &&
previousState.displayedFilters[action.payload.filterName]
previousState.displayedFilters[filterName]
) {
// the filter is already shown
return previousState;
}
return {
...previousState,
filter:
typeof action.payload.defaultValue !== 'undefined'
? set(
previousState.filter,
action.payload.filterName,
action.payload.defaultValue
)
typeof defaultValue !== 'undefined'
? set(previousState.filter, filterName, defaultValue)
: previousState.filter,
// we don't use lodash.set() for displayed filters
// to avoid problems with compound filter names (e.g. 'author.name')
displayedFilters: {
...previousState.displayedFilters,
[action.payload.filterName]: true,
[filterName]: true,
},
};
}
Expand All @@ -141,7 +164,10 @@ export const queryReducer: Reducer<ListParams, ActionTypes> = (
}

default:
return previousState;
return (
((action as ActionTypes).payload as SetDefaultPayload) ??
previousState
);
}
};

Expand Down
6 changes: 6 additions & 0 deletions packages/ra-core/src/controller/list/useListController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isValidElement, useEffect, useMemo } from 'react';
import { UseQueryOptions } from 'react-query';
import { omit } from 'lodash';

import { useAuthenticated } from '../../auth';
import { useTranslate } from '../../i18n';
Expand Down Expand Up @@ -85,6 +86,11 @@ export const useListController = <RecordType extends RaRecord = any>(
} = useGetList<RecordType>(
resource,
{
...omit(query, [
'requestSignature',
'displayedFilters',
'filterValues',
]),
pagination: {
page: query.page,
perPage: query.perPage,
Expand Down
5 changes: 1 addition & 4 deletions packages/ra-core/src/controller/list/useListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,7 @@ const parseObject = (query, field) => {
};

export const parseQueryFromLocation = ({ search }): Partial<ListParams> => {
const query = pickBy(
parse(search),
(v, k) => validQueryParams.indexOf(k) !== -1
);
const query = parse(search);
parseObject(query, 'filter');
parseObject(query, 'displayedFilters');
return query;
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-core/src/dataProvider/useGetList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const useGetList = <RecordType extends RaRecord = any>(
sort = { field: 'id', order: 'DESC' },
filter = {},
meta,
...rest
} = params;
const dataProvider = useDataProvider();
const queryClient = useQueryClient();
Expand All @@ -71,10 +72,11 @@ export const useGetList = <RecordType extends RaRecord = any>(
Error,
GetListResult<RecordType>
>(
[resource, 'getList', { pagination, sort, filter, meta }],
[resource, 'getList', { ...rest, pagination, sort, filter, meta }],
() =>
dataProvider
.getList<RecordType>(resource, {
...rest,
pagination,
sort,
filter,
Expand Down
Loading