1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/packages/unleash-frontend/public/js/components/feature/FeatureForm.jsx

175 lines
5.8 KiB
React
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
const React = require('react');
const TextInput = require('../form/TextInput');
2014-10-30 18:25:38 +01:00
2016-06-18 21:53:18 +02:00
const FeatureForm = React.createClass({
2016-07-02 11:54:50 +02:00
getInitialState () {
2015-03-17 22:01:46 +01:00
return {
strategyOptions: this.props.strategies,
requiredParams: [],
2016-06-18 21:55:46 +02:00
currentStrategy: this.props.feature ? this.props.feature.strategy : 'default',
2015-03-17 22:01:46 +01:00
};
2014-11-12 13:47:21 +01:00
},
2016-07-02 11:54:50 +02:00
componentWillMount () {
2016-06-18 21:55:46 +02:00
if (this.props.feature) {
this.setSelectedStrategy(this.props.feature.strategy);
}
},
2016-07-02 11:54:50 +02:00
onStrategyChange (e) {
this.setSelectedStrategy(e.target.value);
2016-06-18 21:55:46 +02:00
this.setState({ currentStrategy: e.target.value });
},
getParameterValue (feature) {
2016-06-18 21:55:46 +02:00
if (this.props.feature && this.props.feature.parameters) {
return this.props.feature.parameters[feature];
}
2016-06-18 22:23:19 +02:00
return '';
},
setSelectedStrategy (strategyName) {
const selectedStrategy = this.props.strategies.filter(strategy => strategy.name === strategyName)[0];
2016-06-18 21:55:46 +02:00
if (selectedStrategy) {
2015-03-17 22:01:46 +01:00
this.setStrategyParams(selectedStrategy);
} else {
this.setState({
currentStrategy: 'default',
2016-06-18 21:55:46 +02:00
requiredParams: [],
});
}
},
2016-07-02 11:54:50 +02:00
setStrategyParams (strategy) {
2016-06-18 21:53:18 +02:00
const requiredParams = [];
let key;
2016-06-18 21:55:46 +02:00
for (key in strategy.parametersTemplate) {
2016-06-18 22:23:19 +02:00
if (Object.hasOwnProperty.call(strategy.parametersTemplate, key)) {
requiredParams.push({ name: key, value: this.getParameterValue(key) });
}
}
2016-06-18 21:55:46 +02:00
this.setState({ requiredParams });
2014-11-12 13:47:21 +01:00
},
2016-07-02 11:54:50 +02:00
render () {
2016-06-18 21:53:18 +02:00
const feature = this.props.feature || {
2015-03-17 22:01:46 +01:00
name: '',
strategy: 'default',
2016-06-18 21:55:46 +02:00
enabled: false,
};
2016-06-18 21:53:18 +02:00
const idPrefix = this.props.feature ? this.props.feature.name : 'new';
2014-10-30 18:25:38 +01:00
return (
<div className="bg-lilac-xlt r-pam">
<form ref="form" className="r-size1of2">
<fieldset>
2016-06-18 21:55:46 +02:00
{this.props.feature ? '' : <legend>Create new toggle</legend>}
<TextInput
2016-06-18 21:53:18 +02:00
id={`${idPrefix}-name`}
name="name"
label="Name"
value={feature.name}
disabled={feature.name.length}
ref="name"
placeholder="Toggle name" />
<TextInput
2016-06-18 21:53:18 +02:00
id={`${idPrefix}-description`}
name="description"
label="Description"
value={feature.description}
ref="description"
placeholder="Enter description" />
<div className="formelement">
2016-06-18 21:53:18 +02:00
<label htmlFor={`${idPrefix}-strategy`}>Strategy</label>
<div className="input select">
2016-06-18 21:53:18 +02:00
<select id={`${idPrefix}-strategy`}
2015-03-23 18:47:23 +01:00
ref="strategy"
value={this.state.currentStrategy}
onChange={this.onStrategyChange}>
{this.renderStrategyOptions()}
</select>
</div>
</div>
<div className="formelement">
<div className="input">
<ul className="inputs-list">
<li>
2016-06-18 21:53:18 +02:00
<input id={`${idPrefix}-active`}
2015-03-23 18:47:23 +01:00
ref="enabled"
type="checkbox"
defaultChecked={feature.enabled} />
2016-06-18 21:53:18 +02:00
<label htmlFor={`${idPrefix}-active`}>
2015-03-23 18:47:23 +01:00
Active
</label>
</li>
</ul>
</div>
</div>
{this.renderStrategyProperties()}
</fieldset>
<div className="actions">
<button className="primary mrs" onClick={this.saveFeature}>Save</button>
<button className="" onClick={this.cancelFeature}>Cancel</button>
</div>
</form>
2014-10-31 10:30:23 +01:00
</div>
2014-10-30 18:25:38 +01:00
);
},
2016-07-02 11:54:50 +02:00
renderStrategyOptions () {
2016-06-18 21:53:18 +02:00
return this.props.strategies.map(strategy => <option key={strategy.name} value={strategy.name}>
{strategy.name}
</option>);
},
2016-07-02 11:54:50 +02:00
renderStrategyProperties () {
2016-06-18 21:53:18 +02:00
return this.state.requiredParams.map(param => <TextInput
id={`param-${param.name}`}
key={`param-${param.name}`}
name={param.name}
label={param.name}
ref={param.name}
value={param.value} />);
2014-11-25 13:54:25 +01:00
},
2016-07-02 11:54:50 +02:00
saveFeature (e) {
2014-10-30 18:25:38 +01:00
e.preventDefault();
2016-06-18 21:53:18 +02:00
const feature = {
name: this.refs.name.getValue(),
description: this.refs.description.getValue(),
strategy: this.state.currentStrategy,
enabled: this.refs.enabled.getDOMNode().checked,
2016-06-18 21:55:46 +02:00
parameters: this.getParameters(),
2014-11-25 13:54:25 +01:00
};
2014-10-30 18:25:38 +01:00
2014-11-03 13:54:06 +01:00
this.props.onSubmit(feature);
2014-10-30 18:25:38 +01:00
},
2016-07-02 11:54:50 +02:00
cancelFeature (e) {
2014-10-30 18:25:38 +01:00
e.preventDefault();
2014-11-03 13:54:06 +01:00
this.props.onCancel();
},
2016-07-02 11:54:50 +02:00
getParameters () {
2016-06-18 21:53:18 +02:00
const parameters = {};
this.state.requiredParams.forEach(param => {
parameters[param.name] = this.refs[param.name].getValue();
2016-06-18 21:53:18 +02:00
});
return parameters;
2016-06-18 21:55:46 +02:00
},
2014-10-30 18:25:38 +01:00
});
2015-03-17 22:01:46 +01:00
module.exports = FeatureForm;