/** @jsx React.DOM */
/* jshint quotmark:false */
// FeatureListPage
// Meny
// FeatureList
// Feature
// - name
// - status
// - description
// - button-edit
// - button-delete
// - toggle-status
//
// NewFeaturePage
// Meny
// NewFeatureForm
var FeatureForm = React.createClass({
getInitialState: function () {
return {name: '', description: '', strategy: 'Default'};
},
handleNameChange: function(e) { this.setState({name: e.target.value.trim()}); },
handleDescriptionChange: function(e) { this.setState({description: e.target.value.trim()}); },
handleStrategyChange: function(e) { this.setState({strategy: e.target.value.trim()}); },
handleSubmit: function(e) {
e.preventDefault();
this.props.onFeatureSubmit(this.state);
return;
},
render: function () {
return (
);
}
});
var Feature = React.createClass({
// TODO: validate props?
handleEnableChange: function(event) {
var feature = this.props.feature;
this.props.updateFeature({
name: feature.name,
field: 'enabled',
value: event.target.checked
});
},
render: function () {
return (
{this.props.feature.name}
{this.props.feature.description}
);
}
});
var FeatureList = React.createClass({
getInitialState: function() {
return {
features: []
};
},
componentDidMount: function () {
this.loadFeaturesFromServer();
setInterval(this.loadFeaturesFromServer, this.props.pollInterval);
},
loadFeaturesFromServer: function () {
reqwest('/features').then(this.setFeatures);
},
setFeatures: function (data) {
this.setState({features: data.features});
},
updateFeature: function (changeRequest) {
var newFeatures = this.state.features;
newFeatures.forEach(function(f){
if(f.name === changeRequest.name) {
f[changeRequest.field] = changeRequest.value;
}
});
reqwest({
url: 'features/' + changeRequest.name,
method: 'patch',
type: 'json',
contentType: 'application/json',
data: JSON.stringify(changeRequest)
}).then(function() {
this.setState({features: newFeatures});
}.bind(this), function() {
window.alert('update failed');
}.bind(this));
},
createFeature: function (feature, callback) {
reqwest({
url: 'features',
method: 'post',
type: 'json',
contentType: 'application/json',
data: JSON.stringify(feature)
}).then(function() {
// how do we communicate success?
}.bind(this), function() {
window.alert('create failed');
}.bind(this));
},
render: function () {
var featureNodes = this.state.features.map(function (feature) {
return (
);
}.bind(this));
return (
);
}
});
React.renderComponent(
,
document.getElementById('content')
);