122 lines
3.0 KiB
JavaScript
Raw Normal View History

/*
*
* List
*
*/
import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { injectIntl } from 'react-intl';
2017-01-23 20:04:12 +01:00
import Container from 'components/Container';
import Table from 'components/Table';
import _ from 'lodash';
import styles from './styles.scss';
2017-01-26 18:53:52 +01:00
import {
setCurrentModelName,
loadRecords,
2017-01-26 18:53:52 +01:00
} from './actions';
2017-01-23 20:04:12 +01:00
import {
makeSelectLoading,
makeSelectModelRecords,
makeSelectCurrentModelName,
2017-01-23 20:04:12 +01:00
} from './selectors';
import {
makeSelectModels,
} from 'containers/App/selectors';
2017-01-23 20:04:12 +01:00
export class List extends React.Component { // eslint-disable-line react/prefer-stateless-function
2017-01-23 20:04:12 +01:00
componentWillMount() {
this.props.setCurrentModelName(this.props.routeParams.slug.toLowerCase());
this.props.loadRecords();
2017-01-23 20:04:12 +01:00
}
render() {
const PluginHeader = this.props.exposedComponents.PluginHeader;
let content;
2017-01-23 20:04:12 +01:00
if (this.props.loading) {
content = (
2017-01-23 20:04:12 +01:00
<div>
<p>Loading...</p>
</div>
);
} else {
// Detect current model structure from models list
const currentModel = this.props.models[this.props.currentModelName];
2017-01-23 20:04:12 +01:00
// Define table headers
const tableHeaders = _.map(currentModel.attributes, (value, key) => ({
name: key,
label: key,
type: value.type,
})
);
content = (
<Table
records={this.props.records}
route={this.props.route}
routeParams={this.props.routeParams}
headers={tableHeaders}
/>
)
}
2017-01-23 20:04:12 +01:00
return (
<div>
2017-01-23 20:04:12 +01:00
<div className={`container-fluid ${styles.containerFluid}`}>
<PluginHeader title={{
2017-01-23 20:04:12 +01:00
id: 'plugin-content-manager-title',
defaultMessage: `Content Manager > ${this.props.routeParams.slug}`
}} description={{
id: 'plugin-content-manager-description',
defaultMessage: `Manage your ${this.props.routeParams.slug}`
}} noActions={false}>
</PluginHeader>
2017-01-23 20:04:12 +01:00
<Container>
<p></p>
{content}
2017-01-23 20:04:12 +01:00
</Container>
</div>
</div>
);
}
}
2017-01-23 20:04:12 +01:00
List.propTypes = {
setCurrentModelName: React.PropTypes.func,
records: React.PropTypes.oneOfType([
React.PropTypes.array,
React.PropTypes.bool,
]),
2017-01-23 20:04:12 +01:00
loadRecords: React.PropTypes.func,
loading: React.PropTypes.bool,
models: React.PropTypes.oneOfType([
React.PropTypes.object,
React.PropTypes.bool,
]),
currentModelName: React.PropTypes.string,
2017-01-23 20:04:12 +01:00
};
function mapDispatchToProps(dispatch) {
return {
setCurrentModelName: (modelName) => dispatch(setCurrentModelName(modelName)),
loadRecords: () => dispatch(loadRecords()),
dispatch,
};
}
2017-01-23 20:04:12 +01:00
const mapStateToProps = createStructuredSelector({
records: makeSelectModelRecords(),
2017-03-18 17:34:00 +01:00
loading: makeSelectLoading(),
models: makeSelectModels(),
currentModelName: makeSelectCurrentModelName(),
2017-01-23 20:04:12 +01:00
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(List));