158 lines
4.4 KiB
JavaScript
Raw Normal View History

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';
import { get } from 'lodash';
import PropTypes from 'prop-types';
2018-06-28 18:39:36 +02:00
import { onChange, onSubmit, onReset } from 'containers/App/actions';
import { makeSelectModifiedSchema } 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';
import injectReducer from 'utils/injectReducer';
import injectSaga from 'utils/injectSaga';
import reducer from './reducer';
import saga from './saga';
import makeSelectSettingsPage from './selectors';
import styles from './styles.scss';
import forms from './forms.json';
class SettingsPage extends React.PureComponent {
2018-06-28 18:39:36 +02:00
state = { showWarning: false };
2018-06-28 17:54:24 +02:00
getPluginHeaderActions = () => (
[
{
label: 'content-manager.popUpWarning.button.cancel',
kind: 'secondary',
2018-06-28 18:39:36 +02:00
onClick: this.props.onReset,
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();
}
2018-06-28 18:39:36 +02:00
handleSubmit = (e) => {
e.preventDefault();
this.setState({ showWarning: true });
}
toggle = () => this.setState(prevState => ({ showWarning: !prevState.showWarning }));
2018-06-28 17:54:24 +02:00
render() {
2018-06-28 18:39:36 +02:00
const { showWarning } = this.state;
const { onChange, onSubmit } = this.props;
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();
this.toggle();
}}
/>
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">
<div className="col-md-10">
<div className="row">
{forms.inputs.map(input => {
return (
<Input
key={input.name}
2018-06-28 18:39:36 +02:00
onChange={onChange}
2018-06-28 17:54:24 +02:00
value={this.getValue(input)}
{...input}
/>
);
})}
</div>
</div>
</div>
</form>
</Block>
</div>
</div>
);
}
}
SettingsPage.defaultProps = {};
SettingsPage.propTypes = {
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,
};
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-06-28 17:54:24 +02:00
settingsPage: makeSelectSettingsPage(),
});
const withConnect = connect(mapStateToProps, mapDispatchToProps);
const withReducer = injectReducer({ key: 'settingsPage', reducer });
const withSaga = injectSaga({ key: 'settingsPage', saga });
export default compose(
withReducer,
withSaga,
withConnect,
)(SettingsPage);