152 lines
4.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-04-11 11:34:59 +02:00
import _ from 'lodash';
2017-01-23 20:04:12 +01:00
import Container from 'components/Container';
import Table from 'components/Table';
2017-04-11 11:34:59 +02:00
import Pagination from 'components/Pagination';
import styles from './styles.scss';
2017-01-26 18:53:52 +01:00
import {
setCurrentModelName,
loadRecords,
2017-04-11 11:34:59 +02:00
loadCount,
2017-04-11 11:53:00 +02:00
changePage,
2017-01-26 18:53:52 +01:00
} from './actions';
2017-01-23 20:04:12 +01:00
import {
2017-04-11 11:34:59 +02:00
makeSelectRecords,
makeSelectLoadingRecords,
makeSelectCurrentModelName,
2017-04-11 11:34:59 +02:00
makeSelectCount,
makeSelectCurrentPage,
makeSelectLimitPerPage,
makeSelectLoadingCount,
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-04-11 11:34:59 +02:00
this.props.loadCount();
2017-01-23 20:04:12 +01:00
}
render() {
const PluginHeader = this.props.exposedComponents.PluginHeader;
let content;
2017-04-11 11:34:59 +02:00
if (this.props.loadingRecords) {
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
2017-04-10 16:35:51 +02:00
// Hide non displayed attributes
const displayedAttributes = _.pickBy(currentModel.attributes, (attr) => (!attr.admin || attr.admin.displayed !== false));
// Define table headers
2017-04-10 16:35:51 +02:00
const tableHeaders = _.map(displayedAttributes, (value, key) => ({
2017-04-11 11:34:59 +02:00
name: key,
label: key,
type: value.type,
}));
content = (
<Table
records={this.props.records}
route={this.props.route}
routeParams={this.props.routeParams}
headers={tableHeaders}
/>
2017-04-11 11:34:59 +02:00
);
}
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-04-11 11:34:59 +02:00
<Pagination
limitPerPage={this.props.limitPerPage}
currentPage={this.props.currentPage}
2017-04-11 11:53:00 +02:00
changePage={this.props.changePage}
2017-04-11 11:34:59 +02:00
count={this.props.count}
/>
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,
2017-04-11 11:34:59 +02:00
loadingRecords: React.PropTypes.bool,
models: React.PropTypes.oneOfType([
React.PropTypes.object,
React.PropTypes.bool,
]),
2017-04-11 11:34:59 +02:00
currentPage: React.PropTypes.number,
limitPerPage: React.PropTypes.number,
currentModelName: React.PropTypes.string,
2017-04-11 11:53:00 +02:00
goPage: React.PropTypes.func,
2017-01-23 20:04:12 +01:00
};
function mapDispatchToProps(dispatch) {
return {
setCurrentModelName: (modelName) => dispatch(setCurrentModelName(modelName)),
loadRecords: () => dispatch(loadRecords()),
2017-04-11 11:34:59 +02:00
loadCount: () => dispatch(loadCount()),
2017-04-11 11:53:00 +02:00
changePage: (page) => {
dispatch(changePage(page));
2017-04-11 11:34:59 +02:00
dispatch(loadRecords());
dispatch(loadCount());
},
dispatch,
};
}
2017-01-23 20:04:12 +01:00
const mapStateToProps = createStructuredSelector({
2017-04-11 11:34:59 +02:00
records: makeSelectRecords(),
loadingRecords: makeSelectLoadingRecords(),
count: makeSelectCount(),
loadingCount: makeSelectLoadingCount(),
models: makeSelectModels(),
2017-04-11 11:34:59 +02:00
currentPage: makeSelectCurrentPage(),
limitPerPage: makeSelectLimitPerPage(),
currentModelName: makeSelectCurrentModelName(),
2017-01-23 20:04:12 +01:00
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(List));