Skip to content

Commit

Permalink
Check that minimum required props are passed to controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kmaschta committed Jan 17, 2019
1 parent 987014d commit 03c90a5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
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;

0 comments on commit 03c90a5

Please sign in to comment.