/** * * ConfigPage * */ import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { bindActionCreators, compose } from 'redux'; import { findIndex, get, isEmpty } from 'lodash'; import { ContainerFluid, HeaderNav, PluginHeader } from 'strapi-helper-plugin'; import pluginId from '../../pluginId'; // Plugin's components import EditForm from '../../components/EditForm'; import { getSettings, onCancel, onChange, setErrors, submit } from './actions'; import reducer from './reducer'; import saga from './saga'; import selectConfigPage from './selectors'; class ConfigPage extends React.Component { componentDidMount() { this.getSettings(this.props); } componentWillReceiveProps(nextProps) { // Get new settings on navigation change if (nextProps.match.params.env !== this.props.match.params.env) { this.getSettings(nextProps); } // Redirect the user to the upload list after modifying is provider if (nextProps.submitSuccess !== this.props.submitSuccess) { this.props.history.push('/plugins/upload'); } } getSelectedProviderIndex = () => findIndex(this.props.settings.providers, [ 'provider', get(this.props.modifiedData, 'provider'), ]); /** * Get Settings depending on the props * @param {Object} props * @return {Func} calls the saga that gets the current settings */ getSettings = props => { const { match: { params: { env }, }, } = props; this.props.getSettings(env); }; generateLinks = () => { const headerNavLinks = this.props.appEnvironments .reduce((acc, current) => { const link = Object.assign(current, { to: `/plugins/upload/configurations/${current.name}`, }); acc.push(link); return acc; }, []) .sort(link => link.name === 'production'); return headerNavLinks; }; handleSubmit = e => { e.preventDefault(); const formErrors = Object.keys( get( this.props.settings, ['providers', this.getSelectedProviderIndex(), 'auth'], {}, ), ).reduce((acc, current) => { if (isEmpty(get(this.props.modifiedData, current, ''))) { acc.push({ name: current, errors: [{ id: 'components.Input.error.validation.required' }], }); } return acc; }, []); if (!isEmpty(formErrors)) { return this.props.setErrors(formErrors); } return this.props.submit(); }; pluginHeaderActions = [ { kind: 'secondary', label: 'app.components.Button.cancel', onClick: this.props.onCancel, type: 'button', }, { kind: 'primary', label: 'app.components.Button.save', onClick: this.handleSubmit, type: 'submit', }, ]; render() { return (