2017-01-20 16:22:57 +01:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* 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';
|
2017-04-10 16:28:30 +02:00
|
|
|
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';
|
2017-01-20 16:22:57 +01:00
|
|
|
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
2017-01-26 18:53:52 +01:00
|
|
|
import {
|
2017-04-10 16:28:30 +02:00
|
|
|
setCurrentModelName,
|
2017-03-20 22:08:49 +01:00
|
|
|
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,
|
2017-04-10 16:28:30 +02:00
|
|
|
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';
|
|
|
|
|
|
2017-04-10 16:28:30 +02:00
|
|
|
import {
|
|
|
|
|
makeSelectModels,
|
|
|
|
|
} from 'containers/App/selectors';
|
2017-01-23 20:04:12 +01:00
|
|
|
|
2017-04-10 16:28:30 +02:00
|
|
|
export class List extends React.Component { // eslint-disable-line react/prefer-stateless-function
|
2017-01-23 20:04:12 +01:00
|
|
|
componentWillMount() {
|
2017-04-10 16:28:30 +02:00
|
|
|
this.props.setCurrentModelName(this.props.routeParams.slug.toLowerCase());
|
2017-03-20 22:08:49 +01:00
|
|
|
this.props.loadRecords();
|
2017-04-11 11:34:59 +02:00
|
|
|
this.props.loadCount();
|
2017-01-23 20:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 16:22:57 +01:00
|
|
|
render() {
|
2017-03-20 22:08:49 +01:00
|
|
|
const PluginHeader = this.props.exposedComponents.PluginHeader;
|
|
|
|
|
|
|
|
|
|
let content;
|
2017-04-11 11:34:59 +02:00
|
|
|
if (this.props.loadingRecords) {
|
2017-03-20 22:08:49 +01:00
|
|
|
content = (
|
2017-01-23 20:04:12 +01:00
|
|
|
<div>
|
|
|
|
|
<p>Loading...</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2017-03-20 22:08:49 +01:00
|
|
|
} else {
|
2017-04-10 16:28:30 +02:00
|
|
|
// 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));
|
|
|
|
|
|
2017-04-10 16:28:30 +02:00
|
|
|
// 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-01-26 17:53:47 +01:00
|
|
|
|
2017-04-11 15:38:15 +02:00
|
|
|
// Add `id` column
|
|
|
|
|
tableHeaders.unshift({
|
|
|
|
|
name: 'id',
|
|
|
|
|
label: 'ID',
|
|
|
|
|
type: 'string',
|
|
|
|
|
});
|
|
|
|
|
|
2017-03-20 22:08:49 +01:00
|
|
|
content = (
|
2017-04-10 16:28:30 +02:00
|
|
|
<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-10 16:28:30 +02:00
|
|
|
/>
|
2017-04-11 11:34:59 +02:00
|
|
|
);
|
2017-03-20 22:08:49 +01:00
|
|
|
}
|
2017-01-23 20:04:12 +01:00
|
|
|
|
2017-01-20 16:22:57 +01:00
|
|
|
return (
|
|
|
|
|
<div>
|
2017-01-23 20:04:12 +01:00
|
|
|
<div className={`container-fluid ${styles.containerFluid}`}>
|
2017-03-20 22:08:49 +01:00
|
|
|
<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}>
|
2017-03-20 22:08:49 +01:00
|
|
|
</PluginHeader>
|
2017-01-23 20:04:12 +01:00
|
|
|
<Container>
|
2017-03-20 22:08:49 +01:00
|
|
|
{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>
|
2017-01-20 16:22:57 +01:00
|
|
|
</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 = {
|
2017-04-10 16:28:30 +02:00
|
|
|
setCurrentModelName: React.PropTypes.func,
|
2017-03-20 22:08:49 +01:00
|
|
|
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,
|
2017-04-10 16:28:30 +02:00
|
|
|
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,
|
2017-04-10 16:28:30 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2017-01-20 16:22:57 +01:00
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
|
return {
|
2017-04-10 16:28:30 +02:00
|
|
|
setCurrentModelName: (modelName) => dispatch(setCurrentModelName(modelName)),
|
2017-03-20 22:08:49 +01:00
|
|
|
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));
|
2017-04-11 18:49:25 +02:00
|
|
|
dispatch(changePage(1));
|
2017-04-11 17:35:40 +02:00
|
|
|
dispatch(loadRecords());
|
|
|
|
|
e.target.blur();
|
|
|
|
|
},
|
2017-01-20 16:22:57 +01:00
|
|
|
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(),
|
2017-04-10 16:28:30 +02:00
|
|
|
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(),
|
2017-04-10 16:28:30 +02:00
|
|
|
currentModelName: makeSelectCurrentModelName(),
|
2017-01-23 20:04:12 +01:00
|
|
|
});
|
2017-01-20 16:22:57 +01:00
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(List));
|