2016-11-10 14:26:24 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { hashHistory } from 'react-router';
|
|
|
|
|
2018-02-16 09:19:30 +01:00
|
|
|
import { requestUpdateFeatureToggle } from '../../../store/feature-actions';
|
|
|
|
import { createMapper, createActions } from '../../input-helpers';
|
|
|
|
import UpdateFeatureToggleComponent from './form-update-feature-component';
|
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) => {
|
2018-04-17 11:23:15 +02:00
|
|
|
methods.onSubmit = (input, features) => e => {
|
2017-08-28 19:15:47 +02:00
|
|
|
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
|
|
|
}
|
2018-02-15 17:28:09 +01:00
|
|
|
delete input.description;
|
2018-04-17 11:23:15 +02:00
|
|
|
// take the status of the feature toggles from the central store in case `toggleFeature` function was called
|
|
|
|
const feat = features.find(f => f.name === input.name);
|
|
|
|
input.enabled = feat.enabled;
|
2018-02-15 17:09:00 +01:00
|
|
|
|
2017-08-28 19:15:47 +02:00
|
|
|
// TODO: should add error handling
|
|
|
|
requestUpdateFeatureToggle(input)(dispatch)
|
|
|
|
.then(() => methods.clear())
|
2018-02-17 17:23:56 +01:00
|
|
|
.then(() => hashHistory.push(`/features`));
|
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();
|
2018-03-11 11:54:45 +01:00
|
|
|
hashHistory.push(`/features`);
|
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,
|
|
|
|
});
|
|
|
|
|
2018-02-16 09:19:30 +01:00
|
|
|
export default connect(mapStateToProps, actions)(UpdateFeatureToggleComponent);
|