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

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-12-17 14:37:11 +01:00
import { connect } from 'react-redux';
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';
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,
prepare: props => {
2016-12-17 14:37:11 +01:00
props.editmode = true;
return props;
},
});
const prepare = (methods, dispatch) => {
methods.onSubmit = input => e => {
e.preventDefault();
// clean
const parameters = (input.parameters || [])
.filter(name => !!name)
.map(
({
2016-12-17 14:37:11 +01:00
name,
type = 'string',
description = '',
required = false,
}) => ({
name,
type,
description,
required,
})
);
2016-12-17 14:37:11 +01: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
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);