2016-11-10 14:26:24 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { hashHistory } from 'react-router';
|
|
|
|
|
|
|
|
import { requestUpdateFeatureToggle } from '../../store/feature-actions';
|
|
|
|
import { createMapper, createActions } from '../input-helpers';
|
2016-12-05 12:57:21 +01:00
|
|
|
import FormComponent from './form';
|
2016-11-10 14:26:24 +01:00
|
|
|
|
|
|
|
const ID = 'edit-feature-toggle';
|
2017-08-28 19:15:47 +02:00
|
|
|
function getId(props) {
|
2016-11-16 00:15:57 +01:00
|
|
|
return [ID, props.featureToggle.name];
|
2016-11-10 14:26:24 +01:00
|
|
|
}
|
|
|
|
// TODO: need to scope to the active featureToggle
|
|
|
|
// best is to emulate the "input-storage"?
|
|
|
|
const mapStateToProps = createMapper({
|
|
|
|
id: getId,
|
2016-12-31 09:55:07 +01:00
|
|
|
getDefault: (state, ownProps) => {
|
|
|
|
ownProps.featureToggle.strategies.forEach((strategy, index) => {
|
|
|
|
strategy.id = Math.round(Math.random() * 1000000 * (1 + index));
|
|
|
|
});
|
|
|
|
return ownProps.featureToggle;
|
|
|
|
},
|
2017-08-28 19:15:47 +02:00
|
|
|
prepare: props => {
|
2016-11-10 14:26:24 +01:00
|
|
|
props.editmode = true;
|
|
|
|
return props;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-06-29 08:36:10 +02:00
|
|
|
const prepare = (methods, dispatch) => {
|
2017-08-28 19:15:47 +02:00
|
|
|
methods.onSubmit = input => e => {
|
|
|
|
e.preventDefault();
|
2017-01-09 11:08:04 +01:00
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
if (Array.isArray(input.strategies)) {
|
|
|
|
input.strategies.forEach(s => {
|
|
|
|
delete s.id;
|
|
|
|
});
|
2016-11-10 14:26:24 +01:00
|
|
|
}
|
2017-08-28 19:15:47 +02:00
|
|
|
// TODO: should add error handling
|
|
|
|
requestUpdateFeatureToggle(input)(dispatch)
|
|
|
|
.then(() => methods.clear())
|
2018-02-11 17:56:00 +01:00
|
|
|
.then(() => hashHistory.push(`/features/strategies/${input.name}`));
|
2017-08-28 19:15:47 +02:00
|
|
|
};
|
2016-11-10 14:26:24 +01:00
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
methods.onCancel = evt => {
|
2016-11-10 14:26:24 +01:00
|
|
|
evt.preventDefault();
|
2016-11-16 21:58:30 +01:00
|
|
|
methods.clear();
|
2016-12-19 20:53:49 +01:00
|
|
|
window.history.back();
|
2016-11-10 14:26:24 +01:00
|
|
|
};
|
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
methods.addStrategy = v => {
|
2016-12-31 09:55:07 +01:00
|
|
|
v.id = Math.round(Math.random() * 10000000);
|
2016-11-10 14:26:24 +01:00
|
|
|
methods.pushToList('strategies', v);
|
|
|
|
};
|
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
methods.removeStrategy = index => {
|
2016-11-16 21:20:41 +01:00
|
|
|
methods.removeFromList('strategies', index);
|
2016-11-10 14:26:24 +01:00
|
|
|
};
|
|
|
|
|
2016-12-30 23:24:46 +01:00
|
|
|
methods.moveStrategy = (index, toIndex) => {
|
|
|
|
methods.moveItem('strategies', index, toIndex);
|
|
|
|
};
|
|
|
|
|
2016-11-16 21:20:41 +01:00
|
|
|
methods.updateStrategy = (index, n) => {
|
|
|
|
methods.updateInList('strategies', index, n);
|
2016-11-10 14:26:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
methods.validateName = () => {};
|
|
|
|
|
|
|
|
return methods;
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions = createActions({
|
|
|
|
id: getId,
|
|
|
|
prepare,
|
|
|
|
});
|
|
|
|
|
2016-12-05 12:57:21 +01:00
|
|
|
export default connect(mapStateToProps, actions)(FormComponent);
|