2018-02-20 11:34:15 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* ListPage
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2018-02-20 14:25:45 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2018-02-20 11:34:15 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
|
|
|
import { createStructuredSelector } from 'reselect';
|
2018-02-20 16:58:05 +01:00
|
|
|
import { capitalize, get, map, toInteger } from 'lodash';
|
2018-02-20 11:53:18 +01:00
|
|
|
import cn from 'classnames';
|
2018-02-20 11:34:15 +01:00
|
|
|
|
2018-02-20 15:51:20 +01:00
|
|
|
// App selectors
|
2018-02-20 16:58:05 +01:00
|
|
|
import { makeSelectModels, makeSelectSchema } from 'containers/App/selectors';
|
2018-02-20 15:51:20 +01:00
|
|
|
|
2018-02-20 11:53:18 +01:00
|
|
|
// You can find these components in either
|
|
|
|
// ./node_modules/strapi-helper-plugin/lib/src
|
|
|
|
// or strapi/packages/strapi-helper-plugin/lib/src
|
|
|
|
import PluginHeader from 'components/PluginHeader';
|
2018-02-20 11:34:15 +01:00
|
|
|
|
2018-02-20 16:58:05 +01:00
|
|
|
// Components from the plugin itself
|
|
|
|
import Table from 'components/Table';
|
|
|
|
|
2018-02-20 11:34:15 +01:00
|
|
|
// Utils located in `strapi/packages/strapi-helper-plugin/lib/src/utils`;
|
2018-02-20 14:25:45 +01:00
|
|
|
import getQueryParameters from 'utils/getQueryParameters';
|
2018-02-20 11:34:15 +01:00
|
|
|
import injectReducer from 'utils/injectReducer';
|
|
|
|
import injectSaga from 'utils/injectSaga';
|
2018-02-20 14:25:45 +01:00
|
|
|
|
|
|
|
import {
|
2018-02-20 15:51:20 +01:00
|
|
|
changeParams,
|
2018-02-20 14:25:45 +01:00
|
|
|
getData,
|
2018-02-20 15:51:20 +01:00
|
|
|
setParams,
|
2018-02-20 14:25:45 +01:00
|
|
|
} from './actions';
|
2018-02-20 11:34:15 +01:00
|
|
|
|
|
|
|
import reducer from './reducer';
|
|
|
|
import saga from './saga';
|
|
|
|
import makeSelectListPage from './selectors';
|
|
|
|
|
2018-02-20 11:53:18 +01:00
|
|
|
import styles from './styles.scss';
|
2018-02-20 11:34:15 +01:00
|
|
|
|
2018-02-20 14:25:45 +01:00
|
|
|
export class ListPage extends React.Component {
|
2018-02-20 15:51:20 +01:00
|
|
|
componentWillMount() {
|
|
|
|
// Init search params
|
|
|
|
const limit = toInteger(getQueryParameters(this.props.location.search, 'limit')) || 10;
|
|
|
|
const page = toInteger(getQueryParameters(this.props.location.search, 'page')) || 1;
|
|
|
|
const sort = this.findPageSort(this.props);
|
|
|
|
const source = getQueryParameters(this.props.location.search, 'source') || 'content-manager';
|
|
|
|
const params = { limit, page, sort, source };
|
|
|
|
this.props.setParams(params);
|
|
|
|
}
|
|
|
|
|
2018-02-20 14:25:45 +01:00
|
|
|
componentDidMount() {
|
|
|
|
this.getData(this.props);
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:58:05 +01:00
|
|
|
/**
|
|
|
|
* Helper to retrieve the current model data
|
|
|
|
* @return {Object} the current model
|
|
|
|
*/
|
|
|
|
getCurrentModel = () => get(this.props.models, ['models', this.getCurrentModelName()]) || get(this.props.models, ['plugins', this.getSource(), 'models', this.getCurrentModelName()]);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to retrieve the current model name
|
|
|
|
* @return {String} the current model's name
|
|
|
|
*/
|
|
|
|
getCurrentModelName = () => this.props.match.params.slug;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to fetch data
|
|
|
|
* @param {Object} props
|
|
|
|
*/
|
2018-02-20 14:25:45 +01:00
|
|
|
getData = (props) => {
|
2018-02-20 15:51:20 +01:00
|
|
|
this.props.getData(props.match.params.slug);
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:58:05 +01:00
|
|
|
/**
|
|
|
|
* Helper to retrieve the model's source
|
|
|
|
* @return {String} the model's source
|
|
|
|
*/
|
|
|
|
getSource = () => getQueryParameters(this.props.location.search, 'source') || 'content-manager';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to generate the Table's headers
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
|
|
|
generateTableHeaders = () => {
|
|
|
|
const currentSchema = get(this.props.schema, [this.getCurrentModelName()]) || get(this.props.schema, ['plugins', this.getSource(), this.getCurrentModelName()]);
|
|
|
|
const tableHeaders = map(currentSchema.list, (value) => ({
|
|
|
|
name: value,
|
|
|
|
label: currentSchema.fields[value].label,
|
|
|
|
type: currentSchema.fields[value].type,
|
|
|
|
}));
|
|
|
|
|
|
|
|
tableHeaders.splice(0, 0, { name: this.getCurrentModel().primaryKey || 'id', label: 'Id', type: 'string' });
|
|
|
|
|
|
|
|
return tableHeaders;
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:51:20 +01:00
|
|
|
/**
|
|
|
|
* [findPageSort description]
|
|
|
|
* @param {Object} props [description]
|
|
|
|
* @return {String} the model's primaryKey
|
|
|
|
*/
|
|
|
|
findPageSort = (props) => {
|
|
|
|
const { match: { params: { slug } } } = props;
|
2018-02-20 16:58:05 +01:00
|
|
|
const source = this.getSource();
|
2018-02-20 15:51:20 +01:00
|
|
|
const modelPrimaryKey = get(
|
|
|
|
this.props.models,
|
|
|
|
['models', slug.toLowerCase(), 'primaryKey'],
|
|
|
|
);
|
|
|
|
// Check if the model is in a plugin
|
|
|
|
const pluginModelPrimaryKey = get(
|
|
|
|
this.props.models.plugins,
|
|
|
|
[source, 'models', slug.toLowerCase(), 'primaryKey'],
|
|
|
|
);
|
|
|
|
|
|
|
|
return getQueryParameters(props.location.search, 'sort') || modelPrimaryKey || pluginModelPrimaryKey || 'id';
|
2018-02-20 14:25:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pluginHeaderActions = [
|
|
|
|
{
|
|
|
|
label: 'content-manager.containers.List.addAnEntry',
|
|
|
|
labelValues: {
|
|
|
|
entity: capitalize(this.props.match.params.slug) || 'Content Manager',
|
|
|
|
},
|
|
|
|
kind: 'primaryAddShape',
|
|
|
|
onClick: () => this.props.history.push({
|
|
|
|
pathname: `${this.props.location.pathname}/create`,
|
2018-02-20 16:58:05 +01:00
|
|
|
search: `?source=${this.props.listPage.params.source}`,
|
2018-02-20 14:25:45 +01:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2018-02-20 11:34:15 +01:00
|
|
|
render() {
|
2018-02-20 16:58:05 +01:00
|
|
|
const { listPage, listPage: { params } } = this.props;
|
|
|
|
|
2018-02-20 11:34:15 +01:00
|
|
|
return (
|
|
|
|
<div>
|
2018-02-20 11:53:18 +01:00
|
|
|
<div className={cn('container-fluid', styles.containerFluid)}>
|
|
|
|
<PluginHeader
|
2018-02-20 14:25:45 +01:00
|
|
|
actions={this.pluginHeaderActions}
|
|
|
|
description={{
|
|
|
|
id: listPage.count > 1 ? 'content-manager.containers.List.pluginHeaderDescription' : 'content-manager.containers.List.pluginHeaderDescription.singular',
|
|
|
|
values: {
|
|
|
|
label: listPage.count,
|
|
|
|
},
|
|
|
|
}}
|
2018-02-20 11:53:18 +01:00
|
|
|
title={{
|
2018-02-20 14:25:45 +01:00
|
|
|
id: listPage.currentModel || 'Content Manager',
|
2018-02-20 11:53:18 +01:00
|
|
|
}}
|
|
|
|
/>
|
2018-02-20 16:58:05 +01:00
|
|
|
<div className={cn('row', styles.row)}>
|
|
|
|
<div className="col-lg-12">
|
|
|
|
<Table
|
|
|
|
records={listPage.records}
|
|
|
|
route={this.props.match}
|
|
|
|
routeParams={this.props.match.params}
|
|
|
|
headers={this.generateTableHeaders()}
|
|
|
|
onChangeSort={() => {}}
|
|
|
|
sort={listPage.params.sort}
|
|
|
|
history={this.props.history}
|
|
|
|
primaryKey={this.getCurrentModel().primaryKey || 'id'}
|
|
|
|
handleDelete={() => {}}
|
|
|
|
redirectUrl={`?redirectUrl=/plugins/content-manager/${this.getCurrentModelName().toLowerCase()}?page=${params.page}&limit=${params.limit}&sort=${params.sort}&source=${this.getSource()}`}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-02-20 11:53:18 +01:00
|
|
|
</div>
|
2018-02-20 11:34:15 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ListPage.contextTypes = {};
|
|
|
|
|
|
|
|
ListPage.defaultProps = {};
|
|
|
|
|
2018-02-20 14:25:45 +01:00
|
|
|
ListPage.propTypes = {
|
|
|
|
getData: PropTypes.func.isRequired,
|
|
|
|
history: PropTypes.object.isRequired,
|
|
|
|
listPage: PropTypes.object.isRequired,
|
|
|
|
location: PropTypes.object.isRequired,
|
|
|
|
match: PropTypes.object.isRequired,
|
2018-02-20 15:51:20 +01:00
|
|
|
models: PropTypes.object.isRequired,
|
2018-02-20 16:58:05 +01:00
|
|
|
schema: PropTypes.object.isRequired,
|
2018-02-20 15:51:20 +01:00
|
|
|
setParams: PropTypes.func.isRequired,
|
2018-02-20 14:25:45 +01:00
|
|
|
};
|
2018-02-20 11:34:15 +01:00
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(
|
|
|
|
{
|
2018-02-20 15:51:20 +01:00
|
|
|
changeParams,
|
2018-02-20 14:25:45 +01:00
|
|
|
getData,
|
2018-02-20 15:51:20 +01:00
|
|
|
setParams,
|
2018-02-20 11:34:15 +01:00
|
|
|
},
|
|
|
|
dispatch,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = createStructuredSelector({
|
|
|
|
listPage: makeSelectListPage(),
|
2018-02-20 15:51:20 +01:00
|
|
|
models: makeSelectModels(),
|
2018-02-20 16:58:05 +01:00
|
|
|
schema: makeSelectSchema(),
|
2018-02-20 11:34:15 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const withConnect = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
|
|
|
|
const withReducer = injectReducer({ key: 'listPage', reducer });
|
|
|
|
const withSaga = injectSaga({ key: 'listPage', saga });
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withReducer,
|
|
|
|
withSaga,
|
|
|
|
withConnect,
|
|
|
|
)(ListPage);
|