80 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-01-17 13:40:59 +01:00
/**
*
* This component is the skeleton around the actual pages, and should only
* contain code that should be seen on all pages. (e.g. navigation bar)
*
*/
import React from 'react';
import { connect } from 'react-redux';
2017-03-18 17:34:00 +01:00
import { createStructuredSelector } from 'reselect';
2017-07-06 17:51:13 +02:00
import _ from 'lodash';
import { loadModels, updateSchema } from './actions';
2017-07-06 17:51:13 +02:00
import { makeSelectLoading } from './selectors';
const tryRequire = (path) => {
try {
return require(`containers/${path}.js`); // eslint-disable-line global-require
} catch (err) {
return null;
}
};
2017-01-17 13:40:59 +01:00
2017-05-11 10:54:44 +02:00
class App extends React.Component {
2017-03-18 17:34:00 +01:00
componentWillMount() {
2017-07-06 17:51:13 +02:00
const config = tryRequire('../../../../config/admin.json');
if (!_.isEmpty(_.get(config, 'admin.schema'))) {
this.props.updateSchema(config.admin.schema);
} else {
this.props.loadModels();
}
2017-03-18 17:34:00 +01:00
}
2017-01-17 13:40:59 +01:00
render() {
2017-05-11 10:54:44 +02:00
let content = <div />;
2017-05-04 19:05:41 +02:00
2017-07-06 17:51:13 +02:00
if (!this.props.loading) {
2017-05-04 19:05:41 +02:00
// Assign plugin component to children
2017-05-11 10:54:44 +02:00
content = React.Children.map(this.props.children, child =>
React.cloneElement(child, {
exposedComponents: this.props.exposedComponents,
2017-05-04 19:05:41 +02:00
})
);
}
2017-01-17 13:40:59 +01:00
return (
2017-05-11 10:54:44 +02:00
<div className="content-manager">
2017-05-04 19:05:41 +02:00
{React.Children.toArray(content)}
2017-03-18 17:34:00 +01:00
</div>
2017-01-17 13:40:59 +01:00
);
}
}
2017-03-18 17:34:00 +01:00
2017-04-11 15:38:15 +02:00
App.contextTypes = {
router: React.PropTypes.object.isRequired,
};
2017-03-18 17:34:00 +01:00
App.propTypes = {
2017-05-11 10:54:44 +02:00
children: React.PropTypes.node.isRequired,
exposedComponents: React.PropTypes.object.isRequired,
2017-07-06 17:51:13 +02:00
loading: React.PropTypes.bool,
2017-05-11 11:20:01 +02:00
loadModels: React.PropTypes.func.isRequired,
updateSchema: React.PropTypes.func.isRequired,
2017-03-18 17:34:00 +01:00
};
export function mapDispatchToProps(dispatch) {
return {
loadModels: () => dispatch(loadModels()),
updateSchema: (schema) => dispatch(updateSchema(schema)),
2017-03-18 17:34:00 +01:00
dispatch,
};
}
const mapStateToProps = createStructuredSelector({
2017-05-04 19:05:41 +02:00
loading: makeSelectLoading(),
2017-03-18 17:34:00 +01:00
});
// Wrap the component to inject dispatch and state into it
export default connect(mapStateToProps, mapDispatchToProps)(App);