204 lines
4.9 KiB
JavaScript
Raw Normal View History

/**
*
* 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';
// You can find these components in either
// ./node_modules/strapi-helper-plugin/lib/src
// or strapi/packages/strapi-helper-plugin/lib/src
2019-04-16 16:55:53 +02:00
import { ContainerFluid, HeaderNav, PluginHeader } from 'strapi-helper-plugin';
2019-02-22 11:11:25 +01:00
import pluginId from '../../pluginId';
// Plugin's components
2019-02-22 11:11:25 +01:00
import EditForm from '../../components/EditForm';
2019-04-16 16:55:53 +02:00
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);
}
2018-06-13 21:32:40 +02:00
componentDidUpdate(prevProps) {
// Get new settings on navigation change
2018-06-13 21:32:40 +02:00
if (prevProps.match.params.env !== this.props.match.params.env) {
this.getSettings(this.props);
}
// Redirect the user to the email list after modifying is provider
2018-06-13 21:32:40 +02:00
if (prevProps.submitSuccess !== this.props.submitSuccess) {
2019-04-16 16:55:53 +02:00
this.props.history.push(
`/plugins/email/configurations/${this.props.match.params.env}`
2019-04-16 16:55:53 +02:00
);
}
}
2019-04-16 16:55:53 +02:00
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
*/
2019-04-16 16:55:53 +02:00
getSettings = props => {
const {
match: {
params: { env },
},
} = props;
this.props.getSettings(env);
2019-04-16 16:55:53 +02:00
};
generateLinks = () => {
2019-04-16 16:55:53 +02:00
const headerNavLinks = this.props.appEnvironments
.reduce((acc, current) => {
const link = Object.assign(current, {
to: `/plugins/email/configurations/${current.name}`,
});
acc.push(link);
return acc;
}, [])
.sort(link => link.name === 'production');
return headerNavLinks;
2019-04-16 16:55:53 +02:00
};
2019-04-16 16:55:53 +02:00
handleSubmit = e => {
e.preventDefault();
2019-04-16 16:55:53 +02:00
const formErrors = Object.keys(
get(
this.props.settings,
['providers', this.getSelectedProviderIndex(), 'auth'],
{}
)
2019-04-16 16:55:53 +02:00
).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();
2019-04-16 16:55:53 +02:00
};
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 (
<div>
<form onSubmit={this.handleSubmit}>
<ContainerFluid>
<PluginHeader
actions={this.pluginHeaderActions}
description={{ id: 'email.ConfigPage.description' }}
2019-04-16 16:55:53 +02:00
title={{ id: 'email.ConfigPage.title' }}
/>
<HeaderNav links={this.generateLinks()} />
<EditForm
didCheckErrors={this.props.didCheckErrors}
formErrors={this.props.formErrors}
modifiedData={this.props.modifiedData}
onChange={this.props.onChange}
selectedProviderIndex={this.getSelectedProviderIndex()}
settings={this.props.settings}
/>
</ContainerFluid>
</form>
</div>
);
}
}
ConfigPage.defaultProps = {
appEnvironments: [],
formErrors: [],
settings: {
providers: [],
},
};
ConfigPage.propTypes = {
appEnvironments: PropTypes.array,
didCheckErrors: PropTypes.bool.isRequired,
formErrors: PropTypes.array,
getSettings: PropTypes.func.isRequired,
history: PropTypes.object.isRequired,
match: PropTypes.object.isRequired,
modifiedData: PropTypes.object.isRequired,
onCancel: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
setErrors: PropTypes.func.isRequired,
settings: PropTypes.object,
submit: PropTypes.func.isRequired,
submitSuccess: PropTypes.bool.isRequired,
};
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
getSettings,
onCancel,
onChange,
setErrors,
submit,
},
dispatch
);
}
const mapStateToProps = selectConfigPage();
2019-04-16 16:55:53 +02:00
const withConnect = connect(
mapStateToProps,
mapDispatchToProps
2019-04-16 16:55:53 +02:00
);
2019-04-16 16:55:53 +02:00
const withReducer = strapi.injectReducer({
key: 'configPage',
reducer,
pluginId,
});
2019-02-11 18:56:17 +01:00
const withSaga = strapi.injectSaga({ key: 'configPage', saga, pluginId });
export default compose(
withReducer,
withSaga,
withConnect
)(ConfigPage);