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';
|
2018-02-26 12:16:15 +01:00
|
|
|
|
2018-02-28 11:38:50 +01:00
|
|
|
// You can find these components in either
|
|
|
|
// ./node_modules/strapi-helper-plugin/lib/src
|
|
|
|
// or strapi/packages/strapi-helper-plugin/lib/src
|
|
|
|
import ContainerFluid from 'components/ContainerFluid';
|
|
|
|
import HeaderNav from 'components/HeaderNav';
|
|
|
|
import PluginHeader from 'components/PluginHeader';
|
|
|
|
|
2018-02-28 14:57:45 +01:00
|
|
|
// Plugin's components
|
|
|
|
import EditForm from 'components/EditForm';
|
2018-02-28 11:38:50 +01:00
|
|
|
|
2018-02-26 12:16:15 +01:00
|
|
|
// You can find these utils in either
|
|
|
|
// ./node_modules/strapi-helper-plugin/lib/src
|
|
|
|
// or strapi/packages/strapi-helper-plugin/lib/src
|
|
|
|
import injectReducer from 'utils/injectReducer';
|
|
|
|
import injectSaga from 'utils/injectSaga';
|
|
|
|
|
2018-02-28 12:46:12 +01:00
|
|
|
import {
|
|
|
|
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
|
|
|
} from './actions';
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
// Get new settings on navigation change
|
|
|
|
if (nextProps.match.params.env !== this.props.match.params.env) {
|
|
|
|
this.getSettings(nextProps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 17:11:44 +01:00
|
|
|
getSelectedProviderIndex = () => findIndex(this.props.settings.providers, ['provider', get(this.props.modifiedData, 'provider')]);
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
getSettings = (props) => {
|
|
|
|
const { match: { params: { env} } } = props;
|
|
|
|
this.props.getSettings(env);
|
|
|
|
}
|
|
|
|
|
2018-02-28 11:38:50 +01:00
|
|
|
generateLinks = () => {
|
|
|
|
const headerNavLinks = this.context.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;
|
|
|
|
}
|
|
|
|
|
2018-02-28 17:11:44 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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' }}
|
|
|
|
title={{ id: 'upload.ConfigPage.title'}}
|
|
|
|
/>
|
|
|
|
<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-27 18:43:33 +01:00
|
|
|
ConfigPage.contextTypes = {
|
|
|
|
appEnvironments: PropTypes.array,
|
|
|
|
};
|
|
|
|
|
2018-02-28 14:57:45 +01:00
|
|
|
ConfigPage.defaultProps = {
|
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-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,
|
|
|
|
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-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
|
|
|
},
|
2018-02-26 12:16:15 +01:00
|
|
|
dispatch,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-28 11:38:50 +01:00
|
|
|
const mapStateToProps = selectConfigPage();
|
2018-02-26 12:16:15 +01:00
|
|
|
|
|
|
|
const withConnect = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
|
|
|
|
const withReducer = injectReducer({ key: 'configPage', reducer });
|
|
|
|
const withSaga = injectSaga({ key: 'configPage', saga });
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withReducer,
|
|
|
|
withSaga,
|
|
|
|
withConnect,
|
|
|
|
)(ConfigPage);
|