/** * * 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, sortBy } from 'lodash'; import PropTypes from 'prop-types'; import { onChange, onSubmit, onReset } from 'containers/App/actions'; import { makeSelectModifiedSchema, makeSelectSubmitSuccess } from 'containers/App/selectors'; import Input from 'components/InputsIndex'; import PluginHeader from 'components/PluginHeader'; import PopUpWarning from 'components/PopUpWarning'; import Block from 'components/Block'; import SettingsRow from 'components/SettingsRow'; import injectReducer from 'utils/injectReducer'; import injectSaga from 'utils/injectSaga'; 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 }; componentDidUpdate(prevProps) { if (prevProps.submitSuccess !== this.props.submitSuccess) { this.toggle(); } } componentWillUnmount() { this.props.onReset(); } getModels = (data = this.props.schema.models, destination = '/') => { const models = Object.keys(data).reduce((acc, curr) => { if (curr !== 'plugins') { if (!data[curr].fields && _.isObject(data[curr])) { return this.getModels(data[curr], `${destination}${curr}/`); } return acc.concat([{ name: curr, destination: `${destination}${curr}` }]); } return this.getModels(data[curr], `${destination}${curr}/`); }, []); return sortBy( models.filter(obj => obj.name !== 'permission' && obj.name !== 'role'), ['name'], ); } getPluginHeaderActions = () => ( [ { label: 'content-manager.popUpWarning.button.cancel', kind: 'secondary', onClick: this.handleReset, type: 'button', }, { kind: 'primary', label: 'content-manager.containers.Edit.submit', onClick: this.handleSubmit, 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}${destination}`); } handleConfirmReset = () => { this.props.onReset(); this.toggleWarningCancel(); } handleReset = (e) => { e.preventDefault(); this.setState({ showWarningCancel: true }); } handleSubmit = (e) => { e.preventDefault(); this.setState({ showWarning: true }); } toggle = () => this.setState(prevState => ({ showWarning: !prevState.showWarning })); toggleWarningCancel = () => this.setState(prevState => ({ showWarningCancel: !prevState.showWarningCancel })); renderForm = input => ( ); renderRow = model => ; render() { const { showWarning, showWarningCancel } = this.state; const { onChange, onReset, onSubmit } = this.props; return (
{forms.inputs.map(this.renderForm)}
{/* LIST */}
{this.getModels().map(this.renderRow)}
{/* LIST */}
); } } SettingsPage.defaultProps = {}; SettingsPage.propTypes = { history: PropTypes.object.isRequired, location: PropTypes.object.isRequired, onChange: PropTypes.func.isRequired, onReset: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired, schema: PropTypes.object.isRequired, submitSuccess: PropTypes.bool.isRequired, }; const mapDispatchToProps = (dispatch) => ( bindActionCreators( { onChange, onReset, onSubmit, }, dispatch, ) ); const mapStateToProps = createStructuredSelector({ schema: makeSelectModifiedSchema(), submitSuccess: makeSelectSubmitSuccess(), }); const withConnect = connect(mapStateToProps, mapDispatchToProps); const withReducer = injectReducer({ key: 'settingsPage', reducer }); const withSaga = injectSaga({ key: 'settingsPage', saga }); export default compose( withReducer, withSaga, withConnect, )(SettingsPage);