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] Check minimum required props are passed to controllers #2782

Merged
merged 1 commit into from
Jan 17, 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
8 changes: 7 additions & 1 deletion packages/ra-core/src/controller/CreateController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { parse } from 'query-string';

import translate from '../i18n/translate';
import { crudCreate as crudCreateAction } from '../actions';
import checkMinimumRequiredProps from './checkMinimumRequiredProps';

/**
* Page component for the Create view
Expand Down Expand Up @@ -139,9 +140,14 @@ function mapStateToProps(state) {
}

export default compose(
checkMinimumRequiredProps('Create', [
'basePath',
'location',
'resource',
]),
connect(
mapStateToProps,
{ crudCreate: crudCreateAction }
),
translate
translate,
)(CreateController);
6 changes: 6 additions & 0 deletions packages/ra-core/src/controller/EditController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { reset } from 'redux-form';
import translate from '../i18n/translate';
import { crudGetOne, crudUpdate, startUndoable } from '../actions';
import { REDUX_FORM_NAME } from '../form';
import checkMinimumRequiredProps from './checkMinimumRequiredProps';

/**
* Page component for the Edit view
Expand Down Expand Up @@ -175,6 +176,11 @@ function mapStateToProps(state, props) {
}

export default compose(
checkMinimumRequiredProps('Edit', [
'basePath',
'location',
'resource',
]),
connect(
mapStateToProps,
{
Expand Down
8 changes: 8 additions & 0 deletions packages/ra-core/src/controller/ListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '../actions/listActions';
import translate from '../i18n/translate';
import removeKey from '../util/removeKey';
import checkMinimumRequiredProps from './checkMinimumRequiredProps';

/**
* List page component
Expand Down Expand Up @@ -439,7 +440,14 @@ function mapStateToProps(state, props) {
};
}



export default compose(
checkMinimumRequiredProps('List', [
'basePath',
'location',
'resource',
]),
connect(
mapStateToProps,
{
Expand Down
6 changes: 6 additions & 0 deletions packages/ra-core/src/controller/ShowController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import compose from 'recompose/compose';
import inflection from 'inflection';
import translate from '../i18n/translate';
import { crudGetOne as crudGetOneAction } from '../actions';
import checkMinimumRequiredProps from './checkMinimumRequiredProps';

/**
* Page component for the Show view
Expand Down Expand Up @@ -132,6 +133,11 @@ function mapStateToProps(state, props) {
}

export default compose(
checkMinimumRequiredProps('Show', [
'basePath',
'location',
'resource',
]),
connect(
mapStateToProps,
{ crudGetOne: crudGetOneAction }
Expand Down
22 changes: 22 additions & 0 deletions packages/ra-core/src/controller/checkMinimumRequiredProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';

const checkMinimumRequiredProps = (displayName, requiredProps) => WrappedComponent => (props) => {
const propNames = Object.keys(props);
const missingProps = requiredProps.filter(prop => !propNames.includes(prop));

if (missingProps.length > 0) {
throw new Error(
`<${displayName}> component is not properly configured, some essential props are missing.
Be sure to pass the props from the parent. Example:

const My${displayName} = props => (
<${displayName} {...props}></${displayName}>
);

The missing props are: ${missingProps.join(', ')}`)
}

return <WrappedComponent {...props} />;
};

export default checkMinimumRequiredProps;