1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-23 20:07:40 +02:00
unleash.unleash/frontend/src/component/strategies/edit-container.js
ivaosthu 59bcabe331 fix(router): Upgrade to react-router v. 4.x.
This is rather big change to react-router and
required a lot of rewrites.

Mostly followed this guide:
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/migrating.md
2018-08-07 12:50:28 +02:00

62 lines
1.6 KiB
JavaScript

import { connect } from 'react-redux';
import { createMapper, createActions } from '../input-helpers';
import { updateStrategy } from '../../store/strategy/actions';
import AddStrategy from './add-strategy';
const ID = 'edit-strategy';
function getId(props) {
return [ID, props.strategy.name];
}
// TODO: need to scope to the active strategy
// best is to emulate the "input-storage"?
const mapStateToProps = createMapper({
id: getId,
getDefault: (state, ownProps) => ownProps.strategy,
prepare: props => {
props.editmode = true;
return props;
},
});
const prepare = (methods, dispatch) => {
methods.onSubmit = input => e => {
e.preventDefault();
// clean
const parameters = (input.parameters || [])
.filter(name => !!name)
.map(({ name, type = 'string', description = '', required = false }) => ({
name,
type,
description,
required,
}));
updateStrategy({
name: input.name,
description: input.description,
parameters,
})(dispatch)
.then(() => methods.clear())
.then(() => this.props.history.push(`/strategies/view/${input.name}`));
};
methods.onCancel = e => {
e.preventDefault();
methods.clear();
// somewhat quickfix / hacky to go back..
window.history.back();
};
return methods;
};
const actions = createActions({
id: getId,
prepare,
});
export default connect(mapStateToProps, actions)(AddStrategy);