221 lines
6.6 KiB
JavaScript
Raw Normal View History

2018-06-28 17:54:24 +02:00
/**
*
2018-06-28 17:54:24 +02:00
* SettingsPage
*/
import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators, compose } from 'redux';
import { createStructuredSelector } from 'reselect';
import cn from 'classnames';
2018-06-29 12:29:31 +02:00
import { get, sortBy } from 'lodash';
2018-06-28 17:54:24 +02:00
import PropTypes from 'prop-types';
2019-02-11 18:56:17 +01:00
import pluginId from 'pluginId';
2018-06-28 18:39:36 +02:00
import { onChange, onSubmit, onReset } from 'containers/App/actions';
2018-07-05 17:57:30 +02:00
import { makeSelectModifiedSchema, makeSelectSubmitSuccess } from 'containers/App/selectors';
2018-06-28 17:54:24 +02:00
import Input from 'components/InputsIndex';
2018-06-28 18:39:36 +02:00
import PluginHeader from 'components/PluginHeader';
import PopUpWarning from 'components/PopUpWarning';
2018-06-28 17:54:24 +02:00
import Block from 'components/Block';
2018-06-29 12:29:31 +02:00
import SettingsRow from 'components/SettingsRow';
2018-06-28 17:54:24 +02:00
import reducer from './reducer';
import saga from './saga';
import styles from './styles.scss';
import forms from './forms.json';
class SettingsPage extends React.PureComponent {
state = { showWarning: false, showWarningCancel: false };
2018-06-28 18:39:36 +02:00
2018-07-05 17:57:30 +02:00
componentDidUpdate(prevProps) {
if (prevProps.submitSuccess !== this.props.submitSuccess) {
this.toggle();
}
}
componentWillUnmount() {
this.props.onReset();
}
getModels = (data = this.props.schema.models, destination = '/') => {
2018-06-29 12:29:31 +02:00
const models = Object.keys(data).reduce((acc, curr) => {
if (curr !== 'plugins') {
2018-06-29 12:29:31 +02:00
if (!data[curr].fields && _.isObject(data[curr])) {
return acc.concat(this.getModels(data[curr], `${destination}${curr}/`));
2018-06-29 12:29:31 +02:00
}
return acc.concat([{ name: curr, destination: `${destination}${curr}` }]);
}
return acc.concat(this.getModels(data[curr], `${destination}${curr}/`));
2018-06-29 12:29:31 +02:00
}, []);
return sortBy(
models.filter(obj => !!this.props.schema.layout[obj.name]),
2018-06-29 12:29:31 +02:00
['name'],
);
}
2018-06-28 17:54:24 +02:00
getPluginHeaderActions = () => (
[
{
2018-11-20 15:25:32 +01:00
id: 'cancelChanges',
2018-06-28 17:54:24 +02:00
label: 'content-manager.popUpWarning.button.cancel',
kind: 'secondary',
onClick: this.handleReset,
2018-06-28 17:54:24 +02:00
type: 'button',
},
{
kind: 'primary',
label: 'content-manager.containers.Edit.submit',
2018-06-28 18:39:36 +02:00
onClick: this.handleSubmit,
2018-06-28 17:54:24 +02:00
type: 'submit',
},
]
);
getValue = (input) => {
const { schema: { generalSettings } } = this.props;
const value = get(generalSettings, input.name.split('.')[1], input.type === 'toggle' ? false : 10);
return input.type === 'toggle' ? value : value.toString();
}
handleClick = (destination) => {
const { location: { pathname } } = this.props;
this.props.history.push(`${pathname}/list-settings${destination}`);
}
handleConfirmReset = () => {
this.props.onReset();
this.toggleWarningCancel();
}
handleReset = (e) => {
e.preventDefault();
this.setState({ showWarningCancel: true });
}
2018-06-28 18:39:36 +02:00
handleSubmit = (e) => {
e.preventDefault();
this.setState({ showWarning: true });
}
toggle = () => this.setState(prevState => ({ showWarning: !prevState.showWarning }));
toggleWarningCancel = () => this.setState(prevState => ({ showWarningCancel: !prevState.showWarningCancel }));
renderForm = input => (
<Input
key={input.name}
onChange={this.props.onChange}
value={this.getValue(input)}
{...input}
/>
);
renderRow = model => <SettingsRow key={model.name} {...model} onClick={this.handleClick} />;
2018-06-28 17:54:24 +02:00
render() {
const { showWarning, showWarningCancel } = this.state;
const { onSubmit } = this.props;
2018-06-28 18:39:36 +02:00
2018-06-28 17:54:24 +02:00
return (
<div className={cn('container-fluid', styles.containerFluid)}>
<PluginHeader
actions={this.getPluginHeaderActions()}
title="Content Manager"
description={{ id: 'content-manager.containers.SettingsPage.pluginHeaderDescription' }}
/>
2018-06-28 18:39:36 +02:00
<PopUpWarning
isOpen={showWarning}
toggleModal={this.toggle}
content={{
title: 'content-manager.popUpWarning.title',
message: 'content-manager.popUpWarning.warning.updateAllSettings',
cancel: 'content-manager.popUpWarning.button.cancel',
confirm: 'content-manager.popUpWarning.button.confirm',
}}
popUpWarningType="danger"
onConfirm={onSubmit}
2018-06-28 18:39:36 +02:00
/>
<PopUpWarning
isOpen={showWarningCancel}
toggleModal={this.toggleWarningCancel}
content={{
title: 'content-manager.popUpWarning.title',
message: 'content-manager.popUpWarning.warning.cancelAllSettings',
cancel: 'content-manager.popUpWarning.button.cancel',
confirm: 'content-manager.popUpWarning.button.confirm',
}}
popUpWarningType="danger"
onConfirm={this.handleConfirmReset}
/>
2018-06-28 17:54:24 +02:00
<div className={cn('row', styles.container)}>
<Block
description="content-manager.containers.SettingsPage.Block.generalSettings.description"
title="content-manager.containers.SettingsPage.Block.generalSettings.title"
>
2018-06-28 18:39:36 +02:00
<form onSubmit={this.handleSubmit} className={styles.ctmForm}>
2018-06-28 17:54:24 +02:00
<div className="row">
2018-08-20 14:18:37 +02:00
<div className="col-md-12">
2018-06-28 17:54:24 +02:00
<div className="row">
{forms.inputs.map(this.renderForm)}
2018-06-28 17:54:24 +02:00
</div>
</div>
</div>
</form>
</Block>
{/* LIST */}
2018-06-29 12:29:31 +02:00
<Block
title="content-manager.containers.SettingsPage.Block.contentType.title"
description="content-manager.containers.SettingsPage.Block.contentType.description"
>
<div className={styles.contentTypesWrapper}>
{this.getModels().map(this.renderRow)}
2018-06-29 12:29:31 +02:00
</div>
</Block>
{/* LIST */}
2018-06-28 17:54:24 +02:00
</div>
</div>
);
}
}
SettingsPage.defaultProps = {};
SettingsPage.propTypes = {
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
2018-06-28 17:54:24 +02:00
onChange: PropTypes.func.isRequired,
2018-06-28 18:39:36 +02:00
onReset: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
2018-06-28 17:54:24 +02:00
schema: PropTypes.object.isRequired,
2018-07-05 17:57:30 +02:00
submitSuccess: PropTypes.bool.isRequired,
2018-06-28 17:54:24 +02:00
};
const mapDispatchToProps = (dispatch) => (
bindActionCreators(
{
onChange,
2018-06-28 18:39:36 +02:00
onReset,
onSubmit,
2018-06-28 17:54:24 +02:00
},
dispatch,
)
);
const mapStateToProps = createStructuredSelector({
2018-06-28 18:39:36 +02:00
schema: makeSelectModifiedSchema(),
2018-07-05 17:57:30 +02:00
submitSuccess: makeSelectSubmitSuccess(),
2018-06-28 17:54:24 +02:00
});
const withConnect = connect(mapStateToProps, mapDispatchToProps);
2019-02-11 18:56:17 +01:00
const withReducer = strapi.injectReducer({ key: 'settingsPage', reducer, pluginId });
const withSaga = strapi.injectSaga({ key: 'settingsPage', saga, pluginId });
2018-06-28 17:54:24 +02:00
export default compose(
withReducer,
withSaga,
withConnect,
)(SettingsPage);