1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

#13 Post enable change to server and update state

This commit is contained in:
andsandv 2014-10-22 14:05:36 +02:00
parent 85beb3e65b
commit 93bf2b7e84

View File

@ -22,15 +22,39 @@ var FeatureList = React.createClass({
}, },
componentDidMount: function () { componentDidMount: function () {
reqwest("/features").then(this.setFeatures); reqwest('/features').then(this.setFeatures);
},
setFeatures: function (data) {
this.setState({features: data.features});
},
updateFeature: function (feature) {
var newFeatures = this.state.features;
newFeatures.forEach(function(f){
if(f.name === feature.name) {
f = feature;
}
});
reqwest({
url: 'features/' + feature.name,
method: 'post',
type: 'json',
data: feature
}).then(function() {
this.setState({features: newFeatures});
}.bind(this), function() {
alert("update failed");
}.bind(this));
}, },
render: function () { render: function () {
var featureNodes = this.state.features.map(function (feature) { var featureNodes = this.state.features.map(function (feature) {
return ( return (
<Feature feature={feature} /> <Feature feature={feature} updateFeature={this.updateFeature} />
); );
}); }.bind(this));
return ( return (
<div className="mod shadow"> <div className="mod shadow">
@ -41,19 +65,23 @@ var FeatureList = React.createClass({
</div> </div>
</div> </div>
); );
},
setFeatures: function (data) {
this.setState({features: data.features});
} }
}); });
var Feature = React.createClass({ var Feature = React.createClass({
// TODO: validate props? // TODO: validate props?
/*
handleStatusChange: function (event) { handleStatusChange: function (feature, event) {
console.log(feature);
console.log(event); console.log(event);
}, },
*/
handleEnableChange: function(event) {
var feature = this.props.feature;
feature.enabled = event.target.checked;
this.props.updateFeature(feature);
},
render: function () { render: function () {
return ( return (
@ -80,9 +108,9 @@ var Feature = React.createClass({
{this.props.feature.status} {this.props.feature.status}
<input <input
type="checkbox" type="checkbox"
checked={this.props.feature.status === 'on'} checked={this.props.feature.enabled}
className="mll" className="mll"
onChange={this.handleStatusChange} onChange={this.handleEnableChange}
/> />
</label> </label>
</p> </p>