1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/public/js/components/feature/FeatureForm.jsx

182 lines
6.1 KiB
React
Raw Normal View History

2014-10-30 18:25:38 +01:00
var React = require('react');
var TextInput = require('../form/TextInput');
2014-10-30 18:25:38 +01:00
2014-11-03 13:54:06 +01:00
var FeatureForm = React.createClass({
2014-11-12 13:47:21 +01:00
getInitialState: function() {
2015-03-17 22:01:46 +01:00
return {
strategyOptions: this.props.strategies,
requiredParams: [],
currentStrategy: this.props.feature ? this.props.feature.strategy : "default"
};
2014-11-12 13:47:21 +01:00
},
componentWillMount: function() {
if(this.props.feature) {
this.setSelectedStrategy(this.props.feature.strategy);
}
},
onStrategyChange: function(e) {
this.setSelectedStrategy(e.target.value);
this.setState({currentStrategy: e.target.value});
},
getParameterValue: function(name) {
if(this.props.feature && this.props.feature.parameters) {
return this.props.feature.parameters[name];
} else {
return "";
}
},
setSelectedStrategy: function(name) {
var selectedStrategy = this.state.strategyOptions.filter(function(strategy) {
return strategy.name === name;
})[0];
if(selectedStrategy) {
2015-03-17 22:01:46 +01:00
this.setStrategyParams(selectedStrategy);
} else {
var updatedStrategyName = name + " (deleted)";
this.setState({
currentStrategy: updatedStrategyName,
strategyOptions: this.state.strategyOptions.concat([{name: updatedStrategyName}])
});
}
},
setStrategyParams: function(strategy) {
var requiredParams = [];
var key;
for(key in strategy.parametersTemplate) {
requiredParams.push({name: key, value: this.getParameterValue(key)});
}
this.setState({requiredParams: requiredParams});
2014-11-12 13:47:21 +01:00
},
2014-10-30 18:25:38 +01:00
render: function() {
var feature = this.props.feature || {
2015-03-17 22:01:46 +01:00
name: '',
strategy: 'default',
enabled: false
};
var 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>
2014-11-29 12:16:53 +01:00
{this.props.feature ? "" : <legend>Create new toggle</legend>}
<TextInput
id={idPrefix + "-name"}
name="name"
label="Name"
value={feature.name}
disabled={feature.name.length}
ref="name"
placeholder="Toggle name" />
<TextInput
id={idPrefix + "-description"}
name="description"
label="Description"
value={feature.description}
ref="description"
placeholder="Enter description" />
<div className="formelement">
<label htmlFor={idPrefix + "-strategy"}>Strategy</label>
<div className="input select">
2015-03-23 18:47:23 +01:00
<select id={idPrefix + "-strategy"}
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>
2015-03-23 18:47:23 +01:00
<input id={idPrefix + "-active"}
ref="enabled"
type="checkbox"
defaultChecked={feature.enabled} />
<label htmlFor={idPrefix + "-active"}>
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
);
},
2014-11-25 13:54:25 +01:00
renderStrategyOptions: function() {
return this.state.strategyOptions.map(function(strategy) {
2014-11-25 13:54:25 +01:00
return (
2015-03-17 22:01:46 +01:00
<option key={strategy.name} value={strategy.name}>
{strategy.name}
</option>
2014-11-25 13:54:25 +01:00
);
}.bind(this));
},
renderStrategyProperties: function() {
return this.state.requiredParams.map(function(param) {
return <TextInput
id={"param-" + param.name}
key={"param-" + param.name}
name={param.name}
label={param.name}
ref={param.name}
2015-03-23 18:47:23 +01:00
value={param.value} />;
2014-11-25 13:54:25 +01:00
});
},
2014-10-30 18:25:38 +01:00
saveFeature: function(e) {
e.preventDefault();
2014-11-03 13:54:06 +01:00
var feature = {
name: this.refs.name.getValue(),
description: this.refs.description.getValue(),
strategy: this.state.currentStrategy,
enabled: this.refs.enabled.getDOMNode().checked,
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
},
cancelFeature: function(e) {
e.preventDefault();
2014-11-03 13:54:06 +01:00
this.props.onCancel();
},
getParameters: function() {
var parameters = {};
this.state.requiredParams.forEach(function(param) {
parameters[param.name] = this.refs[param.name].getValue();
}.bind(this));
return parameters;
2014-10-30 18:25:38 +01:00
}
});
2015-03-17 22:01:46 +01:00
module.exports = FeatureForm;