196 lines
5.2 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';
2017-04-11 17:35:40 +02:00
import LimitSelect from 'components/LimitSelect';
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-04-11 13:44:47 +02:00
changeSort,
2017-04-11 17:35:40 +02:00
changeLimit,
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,
2017-04-11 17:35:40 +02:00
makeSelectLimit,
2017-04-11 13:44:47 +02:00
makeSelectSort,
2017-04-11 11:34:59 +02:00
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,
}));
2017-04-11 15:38:15 +02:00
// Add `id` column
tableHeaders.unshift({
name: 'id',
label: 'ID',
type: 'string',
});
content = (
<Table
records={this.props.records}
route={this.props.route}
routeParams={this.props.routeParams}
headers={tableHeaders}
2017-04-11 13:44:47 +02:00
changeSort={this.props.changeSort}
sort={this.props.sort}
2017-04-11 15:38:15 +02:00
history={this.props.history}
/>
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>
{content}
2017-04-11 17:35:40 +02:00
<div className="row">
<div className="col-md-6">
<Pagination
limit={this.props.limit}
currentPage={this.props.currentPage}
changePage={this.props.changePage}
count={this.props.count}
/>
</div>
<div className="col-md-6">
<div className="pull-xs-right">
<LimitSelect
className="push-lg-right"
onLimitChange={this.props.onLimitChange}
limit={this.props.limit}
/>
</div>
</div>
</div>
2017-01-23 20:04:12 +01:00
</Container>
</div>
</div>
);
}
}
2017-04-11 15:38:15 +02:00
List.contextTypes = {
router: React.PropTypes.object.isRequired
};
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,
2017-04-11 17:35:40 +02:00
limit: React.PropTypes.number,
2017-04-11 13:44:47 +02:00
sort: React.PropTypes.string,
currentModelName: React.PropTypes.string,
2017-04-11 13:44:47 +02:00
changeSort: React.PropTypes.func,
2017-04-11 17:35:40 +02:00
onLimitChange: 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());
},
2017-04-11 13:44:47 +02:00
changeSort: (sort) => {
dispatch(changeSort(sort));
dispatch(loadRecords());
},
2017-04-11 17:35:40 +02:00
onLimitChange: (e) => {
const newLimit = Number(e.target.value);
dispatch(changeLimit(newLimit));
dispatch(changePage(1));
2017-04-11 17:35:40 +02:00
dispatch(loadRecords());
e.target.blur();
},
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(),
2017-04-11 17:35:40 +02:00
limit: makeSelectLimit(),
2017-04-11 13:44:47 +02:00
sort: makeSelectSort(),
currentModelName: makeSelectCurrentModelName(),
2017-01-23 20:04:12 +01:00
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(List));