2018-02-26 12:16:15 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* ConfigPage
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2018-02-27 18:43:33 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2018-02-26 12:16:15 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
2018-02-28 17:11:44 +01:00
|
|
|
import { findIndex, get, isEmpty } from 'lodash';
|
2019-04-16 18:23:26 +02:00
|
|
|
import { ContainerFluid, HeaderNav, PluginHeader } from 'strapi-helper-plugin';
|
2018-02-28 11:38:50 +01:00
|
|
|
|
2019-02-22 11:11:25 +01:00
|
|
|
import pluginId from '../../pluginId';
|
|
|
|
|
2018-02-28 14:57:45 +01:00
|
|
|
// Plugin's components
|
2019-02-22 11:11:25 +01:00
|
|
|
import EditForm from '../../components/EditForm';
|
2018-02-28 11:38:50 +01:00
|
|
|
|
2019-04-16 18:23:26 +02:00
|
|
|
import { getSettings, onCancel, onChange, setErrors, submit } from './actions';
|
2018-02-28 12:46:12 +01:00
|
|
|
|
2018-02-26 12:16:15 +01:00
|
|
|
import reducer from './reducer';
|
|
|
|
import saga from './saga';
|
|
|
|
import selectConfigPage from './selectors';
|
|
|
|
|
2018-02-28 11:38:50 +01:00
|
|
|
class ConfigPage extends React.Component {
|
2018-02-28 12:46:12 +01:00
|
|
|
componentDidMount() {
|
|
|
|
this.getSettings(this.props);
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:31:10 +02:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2018-02-28 12:46:12 +01:00
|
|
|
// Get new settings on navigation change
|
|
|
|
if (nextProps.match.params.env !== this.props.match.params.env) {
|
|
|
|
this.getSettings(nextProps);
|
|
|
|
}
|
2018-03-05 12:28:29 +01:00
|
|
|
|
|
|
|
// Redirect the user to the upload list after modifying is provider
|
|
|
|
if (nextProps.submitSuccess !== this.props.submitSuccess) {
|
|
|
|
this.props.history.push('/plugins/upload');
|
|
|
|
}
|
2018-02-28 12:46:12 +01:00
|
|
|
}
|
|
|
|
|
2019-04-16 18:23:26 +02:00
|
|
|
getSelectedProviderIndex = () =>
|
|
|
|
findIndex(this.props.settings.providers, [
|
|
|
|
'provider',
|
|
|
|
get(this.props.modifiedData, 'provider'),
|
|
|
|
]);
|
2018-02-28 17:11:44 +01:00
|
|
|
|
2018-02-28 12:46:12 +01:00
|
|
|
/**
|
|
|
|
* Get Settings depending on the props
|
|
|
|
* @param {Object} props
|
|
|
|
* @return {Func} calls the saga that gets the current settings
|
|
|
|
*/
|
2019-04-16 18:23:26 +02:00
|
|
|
getSettings = props => {
|
|
|
|
const {
|
|
|
|
match: {
|
|
|
|
params: { env },
|
|
|
|
},
|
|
|
|
} = props;
|
2018-02-28 12:46:12 +01:00
|
|
|
this.props.getSettings(env);
|
2019-04-16 18:23:26 +02:00
|
|
|
};
|
2018-02-28 12:46:12 +01:00
|
|
|
|
2018-02-28 11:38:50 +01:00
|
|
|
generateLinks = () => {
|
2019-04-16 18:23:26 +02:00
|
|
|
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');
|
2018-02-28 11:38:50 +01:00
|
|
|
|
|
|
|
return headerNavLinks;
|
2019-04-16 18:23:26 +02:00
|
|
|
};
|
2018-02-28 11:38:50 +01:00
|
|
|
|
2019-04-16 18:23:26 +02:00
|
|
|
handleSubmit = e => {
|
2018-02-28 17:11:44 +01:00
|
|
|
e.preventDefault();
|
2019-04-16 18:23:26 +02:00
|
|
|
const formErrors = Object.keys(
|
|
|
|
get(
|
|
|
|
this.props.settings,
|
|
|
|
['providers', this.getSelectedProviderIndex(), 'auth'],
|
2019-08-13 11:31:10 +02:00
|
|
|
{}
|
|
|
|
)
|
2019-04-16 18:23:26 +02:00
|
|
|
).reduce((acc, current) => {
|
2018-02-28 17:11:44 +01:00
|
|
|
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 18:23:26 +02:00
|
|
|
};
|
2018-02-28 17:11:44 +01:00
|
|
|
|
2018-02-28 11:38:50 +01:00
|
|
|
pluginHeaderActions = [
|
|
|
|
{
|
|
|
|
kind: 'secondary',
|
|
|
|
label: 'app.components.Button.cancel',
|
2018-02-28 12:46:12 +01:00
|
|
|
onClick: this.props.onCancel,
|
2018-02-28 11:38:50 +01:00
|
|
|
type: 'button',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
kind: 'primary',
|
|
|
|
label: 'app.components.Button.save',
|
2018-02-28 17:11:44 +01:00
|
|
|
onClick: this.handleSubmit,
|
|
|
|
type: 'submit',
|
2018-02-28 11:38:50 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2018-02-26 12:16:15 +01:00
|
|
|
render() {
|
|
|
|
return (
|
2018-02-28 11:38:50 +01:00
|
|
|
<div>
|
2018-02-28 17:11:44 +01:00
|
|
|
<form onSubmit={this.handleSubmit}>
|
2018-02-28 11:38:50 +01:00
|
|
|
<ContainerFluid>
|
|
|
|
<PluginHeader
|
|
|
|
actions={this.pluginHeaderActions}
|
|
|
|
description={{ id: 'upload.ConfigPage.description' }}
|
2019-04-16 18:23:26 +02:00
|
|
|
title={{ id: 'upload.ConfigPage.title' }}
|
2018-02-28 11:38:50 +01:00
|
|
|
/>
|
|
|
|
<HeaderNav links={this.generateLinks()} />
|
2018-02-28 14:57:45 +01:00
|
|
|
<EditForm
|
2018-02-28 17:11:44 +01:00
|
|
|
didCheckErrors={this.props.didCheckErrors}
|
|
|
|
formErrors={this.props.formErrors}
|
2018-02-28 14:57:45 +01:00
|
|
|
modifiedData={this.props.modifiedData}
|
|
|
|
onChange={this.props.onChange}
|
2018-02-28 17:11:44 +01:00
|
|
|
selectedProviderIndex={this.getSelectedProviderIndex()}
|
2018-02-28 14:57:45 +01:00
|
|
|
settings={this.props.settings}
|
|
|
|
/>
|
2018-02-28 11:38:50 +01:00
|
|
|
</ContainerFluid>
|
|
|
|
</form>
|
|
|
|
</div>
|
2018-02-26 12:16:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 14:57:45 +01:00
|
|
|
ConfigPage.defaultProps = {
|
2018-03-08 10:32:01 +01:00
|
|
|
appEnvironments: [],
|
2018-02-28 17:11:44 +01:00
|
|
|
formErrors: [],
|
2018-02-28 14:57:45 +01:00
|
|
|
settings: {
|
|
|
|
providers: [],
|
|
|
|
},
|
|
|
|
};
|
2018-02-28 12:46:12 +01:00
|
|
|
|
|
|
|
ConfigPage.propTypes = {
|
2018-03-08 10:32:01 +01:00
|
|
|
appEnvironments: PropTypes.array,
|
2018-02-28 17:11:44 +01:00
|
|
|
didCheckErrors: PropTypes.bool.isRequired,
|
|
|
|
formErrors: PropTypes.array,
|
2018-02-28 12:46:12 +01:00
|
|
|
getSettings: PropTypes.func.isRequired,
|
2018-03-05 12:28:29 +01:00
|
|
|
history: PropTypes.object.isRequired,
|
2018-02-28 12:46:12 +01:00
|
|
|
match: PropTypes.object.isRequired,
|
2018-02-28 14:57:45 +01:00
|
|
|
modifiedData: PropTypes.object.isRequired,
|
2018-02-28 12:46:12 +01:00
|
|
|
onCancel: PropTypes.func.isRequired,
|
2018-02-28 14:57:45 +01:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2018-02-28 17:11:44 +01:00
|
|
|
setErrors: PropTypes.func.isRequired,
|
2018-02-28 14:57:45 +01:00
|
|
|
settings: PropTypes.object,
|
2018-02-28 17:11:44 +01:00
|
|
|
submit: PropTypes.func.isRequired,
|
2018-03-05 12:28:29 +01:00
|
|
|
submitSuccess: PropTypes.bool.isRequired,
|
2018-02-28 12:46:12 +01:00
|
|
|
};
|
2018-02-26 12:16:15 +01:00
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(
|
2018-02-28 12:46:12 +01:00
|
|
|
{
|
|
|
|
getSettings,
|
|
|
|
onCancel,
|
2018-02-28 14:57:45 +01:00
|
|
|
onChange,
|
2018-02-28 17:11:44 +01:00
|
|
|
setErrors,
|
|
|
|
submit,
|
2018-02-28 12:46:12 +01:00
|
|
|
},
|
2019-08-13 11:31:10 +02:00
|
|
|
dispatch
|
2018-02-26 12:16:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-28 11:38:50 +01:00
|
|
|
const mapStateToProps = selectConfigPage();
|
2018-02-26 12:16:15 +01:00
|
|
|
|
2019-04-16 18:23:26 +02:00
|
|
|
const withConnect = connect(
|
|
|
|
mapStateToProps,
|
2019-08-13 11:31:10 +02:00
|
|
|
mapDispatchToProps
|
2019-04-16 18:23:26 +02:00
|
|
|
);
|
2018-02-26 12:16:15 +01:00
|
|
|
|
2019-04-16 18:23:26 +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 });
|
2018-02-26 12:16:15 +01:00
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withReducer,
|
|
|
|
withSaga,
|
2019-08-13 11:31:10 +02:00
|
|
|
withConnect
|
2018-02-26 12:16:15 +01:00
|
|
|
)(ConfigPage);
|