2016-12-17 14:37:11 +01:00
|
|
|
import { connect } from 'react-redux';
|
2016-12-18 10:27:04 +01:00
|
|
|
import { hashHistory } from 'react-router';
|
2016-12-17 14:37:11 +01:00
|
|
|
import { createMapper, createActions } from '../input-helpers';
|
|
|
|
import { updateStrategy } from '../../store/strategy/actions';
|
|
|
|
|
|
|
|
import AddStrategy from './add-strategy';
|
|
|
|
|
|
|
|
const ID = 'edit-strategy';
|
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
function getId(props) {
|
2016-12-17 14:37:11 +01:00
|
|
|
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,
|
2017-08-28 19:15:47 +02:00
|
|
|
prepare: props => {
|
2016-12-17 14:37:11 +01:00
|
|
|
props.editmode = true;
|
|
|
|
return props;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const prepare = (methods, dispatch) => {
|
2017-08-28 19:15:47 +02:00
|
|
|
methods.onSubmit = input => e => {
|
|
|
|
e.preventDefault();
|
|
|
|
// clean
|
|
|
|
const parameters = (input.parameters || [])
|
|
|
|
.filter(name => !!name)
|
2017-08-28 21:40:44 +02:00
|
|
|
.map(({ name, type = 'string', description = '', required = false }) => ({
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
description,
|
|
|
|
required,
|
|
|
|
}));
|
2016-12-17 14:37:11 +01:00
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
updateStrategy({
|
|
|
|
name: input.name,
|
|
|
|
description: input.description,
|
|
|
|
parameters,
|
|
|
|
})(dispatch)
|
|
|
|
.then(() => methods.clear())
|
|
|
|
.then(() => hashHistory.push(`/strategies/view/${input.name}`));
|
|
|
|
};
|
2016-12-17 14:37:11 +01:00
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
methods.onCancel = e => {
|
2016-12-17 14:37:11 +01:00
|
|
|
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);
|