314 lines
9.6 KiB
JavaScript
Raw Normal View History

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 19:20:31 +01:00
import { capitalize, get, isUndefined, 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
2018-02-20 19:20:31 +01:00
import PageFooter from 'components/PageFooter';
2018-02-20 11:53:18 +01:00
import PluginHeader from 'components/PluginHeader';
2018-02-20 19:20:31 +01:00
import PopUpWarning from 'components/PopUpWarning';
2018-02-20 11:34:15 +01:00
2018-02-20 16:58:05 +01:00
// Components from the plugin itself
2018-05-15 17:01:47 +02:00
import AddFilterCTA from 'components/AddFilterCTA';
import FiltersWrapper from 'components/FiltersWrapper';
2018-02-20 16:58:05 +01:00
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
2018-03-27 10:13:13 +02:00
import { changeParams, deleteData, getData, setParams } 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 19:20:31 +01:00
state = { showWarning: false, target: '' };
2018-02-20 15:51:20 +01:00
2018-02-20 14:25:45 +01:00
componentDidMount() {
this.getData(this.props);
}
2018-02-20 19:20:31 +01:00
componentWillReceiveProps(nextProps) {
if (nextProps.location.pathname !== this.props.location.pathname) {
this.getData(nextProps);
}
if (nextProps.location.search !== this.props.location.search) {
this.getData(nextProps);
}
}
2018-02-20 16:58:05 +01:00
/**
* Helper to retrieve the current model data
* @return {Object} the current model
*/
2018-03-27 10:13:13 +02:00
getCurrentModel = () =>
get(this.props.models, ['models', this.getCurrentModelName()]) ||
get(this.props.models, ['plugins', this.getSource(), 'models', this.getCurrentModelName()]);
2018-02-20 16:58:05 +01:00
/**
* 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-03-27 10:13:13 +02:00
getData = props => {
2018-02-20 19:20:31 +01:00
const source = getQueryParameters(props.location.search, 'source');
const limit = toInteger(getQueryParameters(props.location.search, 'limit')) || 10;
const page = toInteger(getQueryParameters(props.location.search, 'page')) || 1;
const sort = this.findPageSort(props);
const params = { limit, page, sort };
this.props.setParams(params);
this.props.getData(props.match.params.slug, source);
2018-03-27 10:13:13 +02:00
};
2018-02-20 15:51:20 +01:00
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 = () => {
2018-03-27 10:13:13 +02:00
const currentSchema =
get(this.props.schema, [this.getCurrentModelName()]) ||
get(this.props.schema, ['plugins', this.getSource(), this.getCurrentModelName()]);
const tableHeaders = map(currentSchema.list, value => ({
2018-02-20 16:58:05 +01:00
name: value,
label: currentSchema.fields[value].label,
type: currentSchema.fields[value].type,
}));
2018-03-27 10:13:13 +02:00
tableHeaders.splice(0, 0, {
name: this.getCurrentModel().primaryKey || 'id',
label: 'Id',
type: 'string',
});
2018-02-20 16:58:05 +01:00
return tableHeaders;
2018-03-27 10:13:13 +02:00
};
2018-02-20 16:58:05 +01:00
2018-02-20 15:51:20 +01:00
/**
* [findPageSort description]
* @param {Object} props [description]
* @return {String} the model's primaryKey
*/
2018-03-27 10:13:13 +02:00
findPageSort = props => {
2018-02-20 15:51:20 +01:00
const { match: { params: { slug } } } = props;
2018-02-20 16:58:05 +01:00
const source = this.getSource();
2018-03-27 10:13:13 +02:00
const modelPrimaryKey = get(props.models, ['models', slug.toLowerCase(), 'primaryKey']);
2018-02-20 15:51:20 +01:00
// Check if the model is in a plugin
2018-03-27 10:13:13 +02:00
const pluginModelPrimaryKey = get(props.models.plugins, [
source,
'models',
slug.toLowerCase(),
'primaryKey',
]);
2018-02-20 15:51:20 +01:00
2018-03-27 10:13:13 +02:00
return (
getQueryParameters(props.location.search, 'sort') ||
modelPrimaryKey ||
pluginModelPrimaryKey ||
'id'
);
};
2018-02-20 14:25:45 +01:00
2018-03-27 10:13:13 +02:00
handleChangeParams = e => {
2018-02-20 19:20:31 +01:00
const { history, listPage: { params } } = this.props;
2018-03-27 10:13:13 +02:00
const search =
e.target.name === 'params.limit'
2018-04-20 10:49:05 +02:00
? `page=${params.page}&limit=${e.target.value}&sort=${params.sort}&source=${this.getSource()}`
2018-03-27 10:27:50 +02:00
: `page=${e.target.value}&limit=${params.limit}&sort=${params.sort}&source=${this.getSource()}`;
2018-02-20 19:20:31 +01:00
this.props.history.push({
pathname: history.pathname,
search,
});
this.props.changeParams(e);
2018-03-27 10:13:13 +02:00
};
2018-02-20 19:20:31 +01:00
2018-03-27 10:13:13 +02:00
handleChangeSort = sort => {
2018-02-20 19:20:31 +01:00
const target = {
name: 'params.sort',
value: sort,
};
const { listPage: { params } } = this.props;
this.props.history.push({
pathname: this.props.location.pathname,
search: `?page=${params.page}&limit=${params.limit}&sort=${sort}&source=${this.getSource()}`,
});
this.props.changeParams({ target });
2018-03-27 10:13:13 +02:00
};
2018-02-20 19:20:31 +01:00
2018-03-27 10:13:13 +02:00
handleDelete = e => {
2018-02-20 19:20:31 +01:00
e.preventDefault();
e.stopPropagation();
2018-02-20 19:29:30 +01:00
this.props.deleteData(this.state.target, this.getCurrentModelName(), this.getSource());
2018-02-20 19:20:31 +01:00
this.setState({ showWarning: false });
2018-03-27 10:13:13 +02:00
};
2018-02-20 19:20:31 +01:00
2018-03-27 10:13:13 +02:00
toggleModalWarning = e => {
2018-02-20 19:20:31 +01:00
if (!isUndefined(e)) {
e.preventDefault();
e.stopPropagation();
this.setState({
target: e.target.id,
});
}
this.setState({ showWarning: !this.state.showWarning });
2018-03-27 10:13:13 +02:00
};
2018-02-20 19:20:31 +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-22 11:33:01 +01:00
const pluginHeaderActions = [
{
label: 'content-manager.containers.List.addAnEntry',
labelValues: {
entity: capitalize(this.props.match.params.slug) || 'Content Manager',
},
kind: 'primaryAddShape',
2018-03-27 10:13:13 +02:00
onClick: () =>
this.props.history.push({
pathname: `${this.props.location.pathname}/create`,
search: `?redirectUrl=/plugins/content-manager/${this.getCurrentModelName()}?page=${
params.page
}&limit=${params.limit}&sort=${params.sort}&source=${this.getSource()}`,
}),
2018-02-22 11:33:01 +01:00
},
];
2018-02-20 16:58:05 +01:00
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-22 11:33:01 +01:00
actions={pluginHeaderActions}
2018-02-20 14:25:45 +01:00
description={{
2018-03-27 10:13:13 +02:00
id:
listPage.count > 1
? 'content-manager.containers.List.pluginHeaderDescription'
: 'content-manager.containers.List.pluginHeaderDescription.singular',
2018-02-20 14:25:45 +01:00
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-05-15 17:01:47 +02:00
<div className={cn('row', styles.row)}>
<div className="col-md-12">
<FiltersWrapper>
<AddFilterCTA />
</FiltersWrapper>
</div>
</div>
2018-02-20 16:58:05 +01:00
<div className={cn('row', styles.row)}>
2018-02-20 19:20:31 +01:00
<div className="col-md-12">
2018-02-20 16:58:05 +01:00
<Table
records={listPage.records}
route={this.props.match}
routeParams={this.props.match.params}
headers={this.generateTableHeaders()}
2018-02-20 19:20:31 +01:00
onChangeSort={this.handleChangeSort}
2018-02-20 16:58:05 +01:00
sort={listPage.params.sort}
history={this.props.history}
primaryKey={this.getCurrentModel().primaryKey || 'id'}
2018-02-20 19:20:31 +01:00
handleDelete={this.toggleModalWarning}
2018-03-27 10:13:13 +02:00
redirectUrl={`?redirectUrl=/plugins/content-manager/${this.getCurrentModelName().toLowerCase()}?page=${
params.page
}&limit=${params.limit}&sort=${params.sort}&source=${this.getSource()}`}
2018-02-20 16:58:05 +01:00
/>
2018-02-20 19:20:31 +01:00
<PopUpWarning
isOpen={this.state.showWarning}
toggleModal={this.toggleModalWarning}
content={{
title: 'content-manager.popUpWarning.title',
message: 'content-manager.popUpWarning.bodyMessage.contentType.delete',
cancel: 'content-manager.popUpWarning.button.cancel',
confirm: 'content-manager.popUpWarning.button.confirm',
}}
popUpWarningType={'danger'}
onConfirm={this.handleDelete}
/>
<PageFooter
count={listPage.count}
onChangeParams={this.handleChangeParams}
params={listPage.params}
style={{ marginTop: '2.9rem', padding: '0 15px 0 15px' }}
/>
2018-02-20 16:58:05 +01:00
</div>
</div>
2018-02-20 11:53:18 +01:00
</div>
2018-02-20 11:34:15 +01:00
</div>
);
}
}
2018-02-20 14:25:45 +01:00
ListPage.propTypes = {
2018-02-20 19:20:31 +01:00
changeParams: PropTypes.func.isRequired,
deleteData: PropTypes.func.isRequired,
2018-02-20 14:25:45 +01:00
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 19:20:31 +01:00
deleteData,
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 });
2018-03-27 10:13:13 +02:00
export default compose(withReducer, withSaga, withConnect)(ListPage);