205 lines
5.1 KiB
JavaScript
Raw Normal View History

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';
import { ContainerFluid, HeaderNav, GlobalContext } from 'strapi-helper-plugin';
import { Header } from '@buffetjs/custom';
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';
import Wrapper from './Wrapper';
2019-04-16 18:23:26 +02:00
import { getSettings, onCancel, onChange, setErrors, submit } from './actions';
2018-02-26 12:16:15 +01:00
import reducer from './reducer';
import saga from './saga';
import selectConfigPage from './selectors';
class ConfigPage extends React.Component {
static contextType = GlobalContext;
componentDidMount() {
this.getSettings(this.props);
}
2019-08-13 11:31:10 +02:00
UNSAFE_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');
}
}
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
/**
* 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;
this.props.getSettings(env);
2019-04-16 18:23:26 +02: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');
return headerNavLinks;
2019-04-16 18:23:26 +02: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
pluginHeaderActions = [
{
color: 'cancel',
label: this.context.formatMessage({ id: 'app.components.Button.cancel' }),
onClick: this.props.onCancel,
type: 'button',
key: 'button-cancel',
},
{
color: 'success',
className: 'button-submit',
label: this.context.formatMessage({ id: 'app.components.Button.save' }),
2018-02-28 17:11:44 +01:00
onClick: this.handleSubmit,
type: 'submit',
key: 'button-submit',
},
];
2018-02-26 12:16:15 +01:00
render() {
const { formatMessage } = this.context;
2018-02-26 12:16:15 +01:00
return (
<Wrapper>
2018-02-28 17:11:44 +01:00
<form onSubmit={this.handleSubmit}>
<ContainerFluid>
<Header
actions={this.pluginHeaderActions}
content={formatMessage({ id: 'upload.ConfigPage.description' })}
title={{
label: formatMessage({ 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}
/>
</ContainerFluid>
</form>
</Wrapper>
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: [],
},
};
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,
getSettings: PropTypes.func.isRequired,
history: PropTypes.object.isRequired,
match: PropTypes.object.isRequired,
2018-02-28 14:57:45 +01:00
modifiedData: PropTypes.object.isRequired,
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,
submitSuccess: PropTypes.bool.isRequired,
};
2018-02-26 12:16:15 +01:00
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
getSettings,
onCancel,
2018-02-28 14:57:45 +01:00
onChange,
2018-02-28 17:11:44 +01:00
setErrors,
submit,
},
2019-08-13 11:31:10 +02:00
dispatch
2018-02-26 12:16:15 +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);