249 lines
6.8 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-05-16 16:32:54 +02:00
import { makeSelectModels } from 'containers/App/selectors';
import Container from 'components/Container';
import Table from 'components/Table';
import TableFooter from 'components/TableFooter';
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,
makeSelectCurrentModelNamePluralized,
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-05-11 10:54:44 +02:00
export class List extends React.Component {
componentWillMount() {
// Init the view
this.init(this.props.routeParams.slug);
}
componentWillReceiveProps(nextProps) {
// Check if the current slug changed in the url
const locationChanged =
nextProps.location.pathname !== this.props.location.pathname;
// If the location changed, init the view
if (locationChanged) {
this.init(nextProps.params.slug);
}
}
2017-01-23 20:04:12 +01:00
2017-05-09 17:03:58 +02:00
init(slug) {
// Set current model name
this.props.setCurrentModelName(slug.toLowerCase());
// Set default sort value
this.props.changeSort(this.props.models[slug.toLowerCase()].primaryKey);
// Load records
this.props.loadRecords();
2017-05-09 17:03:58 +02:00
// Get the records count
2017-04-11 11:34:59 +02:00
this.props.loadCount();
2017-05-05 11:40:52 +02:00
// Define the `create` route url
2017-05-09 17:03:58 +02:00
this.addRoute = `${this.props.route.path.replace(':slug', slug)}/create`;
}
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>
);
2017-05-03 17:36:07 +02:00
} else if (!this.props.records.length) {
content = <p>No results.</p>;
} 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
2017-05-11 10:54:44 +02:00
const displayedAttributes = _.pickBy(
currentModel.attributes,
attr => !attr.admin || attr.admin.displayed !== false
);
2017-04-10 16:35:51 +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-05-04 19:05:41 +02:00
// Add the primary key column
2017-04-11 15:38:15 +02:00
tableHeaders.unshift({
2017-05-04 19:05:41 +02:00
name: currentModel.primaryKey,
2017-04-11 15:38:15 +02:00
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-05-04 19:05:41 +02:00
primaryKey={currentModel.primaryKey || 'id'}
/>
2017-04-11 11:34:59 +02:00
);
}
2017-01-23 20:04:12 +01:00
2017-05-05 11:40:52 +02:00
// Define plugin header actions
2017-05-11 10:54:44 +02:00
const pluginHeaderActions = [
{
label: 'Add an entry',
class: 'btn-primary',
onClick: () => this.context.router.push(this.addRoute),
},
];
2017-05-05 11:40:52 +02:00
// Plugin header config
2017-05-11 10:54:44 +02:00
const pluginHeaderTitle =
_.upperFirst(this.props.currentModelNamePluralized) || 'Content Manager';
const pluginHeaderDescription = `Manage your ${this.props.currentModelNamePluralized}`;
return (
<div>
2017-01-23 20:04:12 +01:00
<div className={`container-fluid ${styles.containerFluid}`}>
<PluginHeader
title={{
id: 'plugin-content-manager-title',
2017-05-11 10:54:44 +02:00
defaultMessage: `${pluginHeaderTitle}`,
}}
description={{
id: 'plugin-content-manager-description',
2017-05-11 10:54:44 +02:00
defaultMessage: `${pluginHeaderDescription}`,
}}
2017-05-05 11:40:52 +02:00
actions={pluginHeaderActions}
/>
2017-01-23 20:04:12 +01:00
<Container>
{content}
2017-04-11 19:20:33 +02:00
<TableFooter
limit={this.props.limit}
currentPage={this.props.currentPage}
changePage={this.props.changePage}
count={this.props.count}
className="push-lg-right"
onLimitChange={this.props.onLimitChange}
/>
2017-01-23 20:04:12 +01:00
</Container>
</div>
</div>
);
}
}
2017-04-11 15:38:15 +02:00
List.contextTypes = {
2017-05-11 10:54:44 +02:00
router: React.PropTypes.object.isRequired,
2017-04-11 15:38:15 +02:00
};
2017-01-23 20:04:12 +01:00
List.propTypes = {
2017-05-11 11:20:01 +02:00
changePage: React.PropTypes.func.isRequired,
changeSort: React.PropTypes.func.isRequired,
count: React.PropTypes.oneOfType([
React.PropTypes.number,
React.PropTypes.bool,
]),
2017-05-11 10:54:44 +02:00
currentModelName: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.bool,
]),
currentModelNamePluralized: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.bool,
]),
2017-05-11 11:20:01 +02:00
currentPage: React.PropTypes.number.isRequired,
exposedComponents: React.PropTypes.object.isRequired,
history: React.PropTypes.object.isRequired,
limit: React.PropTypes.number.isRequired,
loadCount: React.PropTypes.func.isRequired,
loadingRecords: React.PropTypes.bool.isRequired,
loadRecords: React.PropTypes.func.isRequired,
location: React.PropTypes.object.isRequired,
models: React.PropTypes.oneOfType([
React.PropTypes.object,
React.PropTypes.bool,
]),
2017-05-11 10:54:44 +02:00
onLimitChange: React.PropTypes.func.isRequired,
2017-05-11 11:20:01 +02:00
records: React.PropTypes.oneOfType([
React.PropTypes.array,
2017-04-11 19:20:33 +02:00
React.PropTypes.bool,
]),
2017-05-11 10:54:44 +02:00
route: React.PropTypes.object.isRequired,
routeParams: React.PropTypes.object.isRequired,
2017-05-11 11:20:01 +02:00
setCurrentModelName: React.PropTypes.func.isRequired,
sort: React.PropTypes.string.isRequired,
2017-01-23 20:04:12 +01:00
};
function mapDispatchToProps(dispatch) {
return {
2017-05-11 10:54:44 +02:00
setCurrentModelName: modelName => dispatch(setCurrentModelName(modelName)),
loadRecords: () => dispatch(loadRecords()),
2017-04-11 11:34:59 +02:00
loadCount: () => dispatch(loadCount()),
2017-05-11 10:54:44 +02:00
changePage: page => {
2017-04-11 11:53:00 +02:00
dispatch(changePage(page));
2017-04-11 11:34:59 +02:00
dispatch(loadRecords());
dispatch(loadCount());
},
2017-05-11 10:54:44 +02:00
changeSort: sort => {
2017-04-11 13:44:47 +02:00
dispatch(changeSort(sort));
dispatch(loadRecords());
},
2017-05-11 10:54:44 +02:00
onLimitChange: e => {
2017-04-11 17:35:40 +02:00
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(),
currentModelNamePluralized: makeSelectCurrentModelNamePluralized(),
2017-01-23 20:04:12 +01:00
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(List));