1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-23 20:07:40 +02:00
unleash.unleash/frontend/src/component/feature/form-add-container.jsx

58 lines
1.7 KiB
React
Raw Normal View History

2016-11-10 14:26:24 +01:00
import { connect } from 'react-redux';
import { hashHistory } from 'react-router';
import { createFeatureToggles, validateName } from '../../store/feature-actions';
import { createMapper, createActions } from '../input-helpers';
import FormComponent from './form';
const ID = 'add-feature-toggle';
const mapStateToProps = createMapper({
id: ID,
getDefault () {
let name;
try {
[, name] = document.location.hash.match(/name=([a-z0-9-_]+)/i);
} catch (e) {}
return { name };
},
});
2016-11-10 14:26:24 +01:00
const prepare = (methods, dispatch) => {
methods.onSubmit = (input) => (
(e) => {
e.preventDefault();
createFeatureToggles(input)(dispatch)
.then(() => methods.clear())
2016-12-13 20:45:56 +01:00
.then(() => hashHistory.push(`/features/edit/${input.name}`));
2016-11-10 14:26:24 +01:00
}
);
methods.onCancel = (evt) => {
evt.preventDefault();
methods.clear();
2016-11-10 14:26:24 +01:00
hashHistory.push('/features');
};
methods.addStrategy = (v) => {
methods.pushToList('strategies', v);
};
methods.updateStrategy = (index, n) => {
methods.updateInList('strategies', index, n);
2016-11-10 14:26:24 +01:00
};
methods.removeStrategy = (index) => {
methods.removeFromList('strategies', index);
2016-11-10 14:26:24 +01:00
};
methods.validateName = (v) => {
const featureToggleName = v.target.value;
validateName(featureToggleName)
.then(() => methods.setValue('nameError', undefined))
.catch((err) => methods.setValue('nameError', err.message));
};
return methods;
};
const actions = createActions({ id: ID, prepare });
export default connect(mapStateToProps, actions)(FormComponent);