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

Fix custom routes not working when based on resources #4251

Merged
merged 2 commits into from
Jan 9, 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
10 changes: 7 additions & 3 deletions packages/ra-core/src/controller/useListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const defaultSort = {
order: SORT_ASC,
};

const defaultData = {};

export interface ListControllerProps {
basePath: string;
currentSort: Sort;
Expand Down Expand Up @@ -171,7 +173,7 @@ const useListController = (props: ListProps): ListControllerProps => {
(state: ReduxState) =>
state.admin.resources[resource]
? state.admin.resources[resource].data
: null,
: defaultData,
shallowEqual
);

Expand Down Expand Up @@ -209,7 +211,8 @@ const useListController = (props: ListProps): ListControllerProps => {
displayedFilters: query.displayedFilters,
filterValues: query.filterValues,
hasCreate,
ids,
// ids might be null if the resource has not been initialized yet (custom routes for example)
ids: ids || [],
loading,
loaded,
onSelect: selectionModifiers.select,
Expand All @@ -225,7 +228,8 @@ const useListController = (props: ListProps): ListControllerProps => {
setPage: queryModifiers.setPage,
setPerPage: queryModifiers.setPerPage,
setSort: queryModifiers.setSort,
total,
// total might be null if the resource has not been initialized yet (custom routes for example)
total: total != undefined ? total : 0, // eslint-disable-line eqeqeq
version,
};
};
Expand Down
7 changes: 6 additions & 1 deletion packages/ra-core/src/controller/useListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const defaultSort = {
order: SORT_ASC,
};

const defaultParams = {};

/**
* Get the list parameters (page, sort, filters) and modifiers.
*
Expand Down Expand Up @@ -114,7 +116,10 @@ const useListParams = ({
const history = useHistory();

const { params } = useSelector(
(reduxState: ReduxState) => reduxState.admin.resources[resource].list,
(reduxState: ReduxState) =>
reduxState.admin.resources[resource]
? reduxState.admin.resources[resource].list
: defaultParams,
shallowEqual
);

Expand Down
6 changes: 5 additions & 1 deletion packages/ra-core/src/controller/useRecordSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useSelector, useDispatch, shallowEqual } from 'react-redux';
import { setListSelectedIds, toggleListItem } from '../actions/listActions';
import { Identifier, ReduxState } from '../types';

const defaultRecords = [];

/**
* Get the list of selected items for a resource, and callbacks to change the selection
*
Expand All @@ -14,7 +16,9 @@ const useSelectItems = (resource: string) => {
const dispatch = useDispatch();
const selectedIds = useSelector(
(reduxState: ReduxState) =>
reduxState.admin.resources[resource].list.selectedIds,
reduxState.admin.resources[resource]
? reduxState.admin.resources[resource].list.selectedIds
: defaultRecords,
shallowEqual
);
const selectionModifiers = {
Expand Down