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

[RFR] Display a skeleton instead of empty datagrid when loading #2706

Merged
merged 4 commits into from
Jan 1, 2019
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
3 changes: 3 additions & 0 deletions cypress/integration/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('List Page', () => {

describe('Auto-hide AppBar', () => {
it('should hide/show the appBar when scroll action appears', () => {
// wait for the skeleton to disappear
cy.contains('1-10 of 13');

cy.viewport(1280, 500);

cy.scrollTo(0, 200);
Expand Down
5 changes: 5 additions & 0 deletions packages/ra-core/src/controller/ListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export class ListController extends Component {
hasCreate,
data,
ids,
loadedOnce,
total,
isLoading,
translate,
Expand Down Expand Up @@ -278,6 +279,7 @@ export class ListController extends Component {
hideFilter: this.hideFilter,
ids,
isLoading,
loadedOnce,
onSelect: this.handleSelect,
onToggleItem: this.handleToggleItem,
onUnselectItems: this.handleUnselectItems,
Expand Down Expand Up @@ -323,6 +325,7 @@ ListController.propTypes = {
hasList: PropTypes.bool,
hasShow: PropTypes.bool,
ids: PropTypes.array,
loadedOnce: PropTypes.bool,
selectedIds: PropTypes.array,
isLoading: PropTypes.bool.isRequired,
location: PropTypes.object.isRequired,
Expand Down Expand Up @@ -360,6 +363,7 @@ const injectedProps = [
'hideFilter',
'ids',
'isLoading',
'loadedOnce',
'onSelect',
'onToggleItem',
'onUnselectItems',
Expand Down Expand Up @@ -425,6 +429,7 @@ function mapStateToProps(state, props) {
query: selectQuery(props),
params: resourceState.list.params,
ids: resourceState.list.ids,
loadedOnce: resourceState.list.loadedOnce,
selectedIds: resourceState.list.selectedIds,
total: resourceState.list.total,
data: resourceState.data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class ReferenceManyFieldController extends Component {
setSort = field => {
const order =
this.state.sort.field === field &&
this.state.sort.order === SORT_ASC
this.state.sort.order === SORT_ASC
? SORT_DESC
: SORT_ASC;
this.setState({ sort: { field, order } }, this.fetchReferences);
Expand Down Expand Up @@ -135,7 +135,7 @@ export class ReferenceManyFieldController extends Component {
currentSort: this.state.sort,
data,
ids,
isLoading: typeof ids === 'undefined',
loadedOnce: typeof ids !== 'undefined',
Copy link
Member Author

Choose a reason for hiding this comment

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

this can be considered a BC break, but it was never documented, so I'm confident it should not break much.

page,
perPage,
referenceBasePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { render } from 'react-testing-library';
import { ReferenceManyFieldController } from './ReferenceManyFieldController';

describe('<ReferenceManyFieldController />', () => {
it('should set isLoading to true when related records are not yet fetched', () => {
it('should set loadedOnce to false when related records are not yet fetched', () => {
const children = jest.fn();
shallow(
<ReferenceManyFieldController
Expand All @@ -19,7 +19,7 @@ describe('<ReferenceManyFieldController />', () => {
</ReferenceManyFieldController>,
{ disableLifecycleMethods: true }
);
assert.equal(children.mock.calls[0][0].isLoading, true);
assert.equal(children.mock.calls[0][0].loadedOnce, false);
});

it('should pass data and ids to children function', () => {
Expand Down Expand Up @@ -117,27 +117,27 @@ describe('<ReferenceManyFieldController />', () => {

it('should call crudGetManyReference when its props changes', () => {
const crudGetManyReference = jest.fn();
const ControllerWrapper = (props) => (
<ReferenceManyFieldController record={{ id: 1 }}
resource="foo"
reference="bar"
target="foo_id"
basePath=""
data={{
1: { id: 1, title: 'hello' },
2: { id: 2, title: 'world' },
}}
ids={[1, 2]}
crudGetManyReference={crudGetManyReference}
{...props}>
const ControllerWrapper = props => (
<ReferenceManyFieldController
record={{ id: 1 }}
resource="foo"
reference="bar"
target="foo_id"
basePath=""
data={{
1: { id: 1, title: 'hello' },
2: { id: 2, title: 'world' },
}}
ids={[1, 2]}
crudGetManyReference={crudGetManyReference}
{...props}
>
{() => null}
</ReferenceManyFieldController>
);

const { rerender } = render(<ControllerWrapper />);
rerender(
<ControllerWrapper sort={{ field: 'id', order: 'ASC' }} />
);
rerender(<ControllerWrapper sort={{ field: 'id', order: 'ASC' }} />);

assert.deepEqual(crudGetManyReference.mock.calls[1], [
'bar',
Expand Down
12 changes: 12 additions & 0 deletions packages/ra-core/src/reducer/admin/resource/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'posts' },
},
Expand All @@ -45,6 +46,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'comments' },
},
Expand All @@ -71,6 +73,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'posts' },
},
Expand All @@ -87,6 +90,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'comments' },
},
Expand All @@ -103,6 +107,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'users', options: 'foo' },
},
Expand All @@ -127,6 +132,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'posts' },
},
Expand All @@ -143,6 +149,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'comments' },
},
Expand All @@ -166,6 +173,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'posts' },
},
Expand All @@ -190,6 +198,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'posts' },
},
Expand All @@ -206,6 +215,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'comments' },
},
Expand Down Expand Up @@ -236,6 +246,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'posts' },
},
Expand All @@ -252,6 +263,7 @@ describe('Resources Reducer', () => {
},
selectedIds: [],
total: 0,
loadedOnce: false,
},
props: { name: 'comments' },
},
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-core/src/reducer/admin/resource/list/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { combineReducers } from 'redux';
import ids from './ids';
import loadedOnce from './loadedOnce';
import params from './params';
import selectedIds from './selectedIds';
import total from './total';

export default combineReducers({
ids,
loadedOnce,
params,
selectedIds,
total,
Expand Down
20 changes: 20 additions & 0 deletions packages/ra-core/src/reducer/admin/resource/list/loadedOnce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Reducer } from 'redux';
import { CRUD_GET_LIST_SUCCESS } from '../../../../actions/dataActions';

type State = boolean;

/**
* This resource reducer is false until the list loads successfully
*/
const loadedOnce: Reducer<State> = (previousState = false, { type }) => {
// early return
if (previousState === true) {
return previousState;
}
if (type === CRUD_GET_LIST_SUCCESS) {
return true;
}
return previousState;
};

export default loadedOnce;
51 changes: 20 additions & 31 deletions packages/ra-ui-materialui/src/field/ReferenceManyField.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import React, { Fragment, cloneElement } from 'react';
import PropTypes from 'prop-types';
import LinearProgress from '@material-ui/core/LinearProgress';
import { withStyles } from '@material-ui/core/styles';
import { ReferenceManyFieldController } from 'ra-core';

const styles = {
progress: { marginTop: '1em' },
};

export const ReferenceManyFieldView = ({
children,
classes = {},
className,
currentSort,
data,
ids,
isLoading,
loadedOnce,
page,
pagination,
perPage,
Expand All @@ -25,38 +18,40 @@ export const ReferenceManyFieldView = ({
setPerPage,
setSort,
total,
}) => isLoading ? <LinearProgress className={classes.progress} /> :
<Fragment>
{cloneElement(children, {
className,
resource: reference,
ids,
data,
basePath: referenceBasePath,
currentSort,
setSort,
total,
})}
{pagination && cloneElement(pagination, {
}) => (
<Fragment>
{cloneElement(children, {
className,
resource: reference,
ids,
loadedOnce,
data,
basePath: referenceBasePath,
currentSort,
setSort,
total,
})}
{pagination &&
cloneElement(pagination, {
page,
perPage,
setPage,
setPerPage,
total,
})}
</Fragment>;
</Fragment>
);

ReferenceManyFieldView.propTypes = {
children: PropTypes.element,
classes: PropTypes.object,
className: PropTypes.string,
currentSort: PropTypes.shape({
field: PropTypes.string,
order: PropTypes.string,
}),
data: PropTypes.object,
ids: PropTypes.array,
isLoading: PropTypes.bool,
loadedOnce: PropTypes.bool,
pagination: PropTypes.element,
reference: PropTypes.string,
referenceBasePath: PropTypes.string,
Expand Down Expand Up @@ -154,13 +149,7 @@ ReferenceManyField.defaultProps = {
perPage: 25,
sort: { field: 'id', order: 'DESC' },
source: 'id',
};

const EnhancedReferenceManyField = withStyles(styles)(ReferenceManyField);

EnhancedReferenceManyField.defaultProps = {
addLabel: true,
source: 'id',
};

export default EnhancedReferenceManyField;
export default ReferenceManyField;
22 changes: 0 additions & 22 deletions packages/ra-ui-materialui/src/field/ReferenceManyField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,6 @@ import TextField from './TextField';
import SingleFieldList from '../list/SingleFieldList';

describe('<ReferenceManyField />', () => {
it('should render a loading indicator when isLoading is true', () => {
const wrapper = shallow(
<ReferenceManyFieldView
resource="foo"
reference="bar"
basePath=""
isLoading={true}
>
<SingleFieldList>
<TextField source="title" />
</SingleFieldList>
</ReferenceManyFieldView>,
{ disableLifecycleMethods: true }
);
const ProgressElements = wrapper.find('WithStyles(LinearProgress)');
assert.equal(ProgressElements.length, 1);
const SingleFieldListElement = wrapper.find(
'WithStyles(SingleFieldList)'
);
assert.equal(SingleFieldListElement.length, 0);
});

it('should render a list of the child component', () => {
const data = {
1: { id: 1, title: 'hello' },
Expand Down
Loading