/** * * EditPage * */ import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { createStructuredSelector } from 'reselect'; import { bindActionCreators, compose } from 'redux'; import { FormattedMessage } from 'react-intl'; import { findIndex, get, isEmpty, isEqual, size } from 'lodash'; import cn from 'classnames'; // Design import BackHeader from 'components/BackHeader'; import Input from 'components/Input'; import InputSearch from 'components/InputSearch'; import PluginHeader from 'components/PluginHeader'; import Plugins from 'components/Plugins'; import injectSaga from 'utils/injectSaga'; import injectReducer from 'utils/injectReducer'; // Actions import { addUser, getPermissions, getRole, onCancel, onChangeInput, onClickDelete, setActionType, setErrors, setForm, submit, } from './actions'; // Selectors import makeSelectEditPage from './selectors'; import reducer from './reducer'; import saga from './saga'; import styles from './styles.scss'; export class EditPage extends React.Component { // eslint-disable-line react/prefer-stateless-function getChildContext = () => ( { onChange: this.props.onChangeInput, } ); componentDidMount() { this.props.setActionType(this.props.match.params.actionType); if (this.props.match.params.actionType === 'create') { // Set reducer modifiedData this.props.setForm(); // Get the available permissions this.props.getPermissions(); } else { this.props.getRole(this.props.match.params.id); } } componentWillReceiveProps(nextProps) { // Redirect user to HomePage if submit ok if (nextProps.editPage.didSubmit !== this.props.editPage.didSubmit) { this.props.history.push('/plugins/users-permissions/roles'); } } componentWillUnmount() { // Empty formErrors this.props.setErrors([]); } handleSubmit = () => { // Check if the name field is filled if (isEmpty(get(this.props.editPage, ['modifiedData', 'name']))) { return this.props.setErrors([{ name: 'name', errors: [{ id: 'users-permissions.EditPage.form.roles.name.error' }] }]); } this.props.submit(); } pluginHeaderActions = [ { label: 'users-permissions.EditPage.cancel', kind: 'secondary', onClick: this.props.onCancel, type: 'button', }, { kind: 'primary', label: 'users-permissions.EditPage.submit', onClick: this.handleSubmit, type: 'submit', }, ]; render() { const pluginHeaderTitle = this.props.match.params.actionType === 'create' ? 'users-permissions.EditPage.header.title.create' : 'users-permissions.EditPage.header.title'; const pluginHeaderDescription = this.props.match.params.actionType === 'create' ? 'users-permissions.EditPage.header.description.create' : 'users-permissions.EditPage.header.description'; const pluginHeaderActions = !isEqual(this.props.editPage.modifiedData, this.props.editPage.initialData) ? this.pluginHeaderActions : []; return (