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

139 lines
3.7 KiB
React
Raw Normal View History

2014-10-21 18:57:05 +02:00
/** @jsx React.DOM */
/* jshint quotmark:false */
// Unleash
// - Menu
// - FeatureList
// - UnsavedFeature
// - SavedFeature
//
2014-10-21 18:57:05 +02:00
var Menu = React.createClass({
render: function() { return <div/>; }
});
2014-10-23 14:30:02 +02:00
2014-10-23 16:52:41 +02:00
var UnsavedFeature = React.createClass({
// TODO: form
render: function() { return <div/>; }
2014-10-23 14:30:02 +02:00
});
var SavedFeature = React.createClass({
2014-10-24 10:31:36 +02:00
onChange: function(event) {
this.props.onChange({
name: this.props.feature.name,
field: 'enabled',
value: event.target.checked
});
},
render: function() {
return (
2014-10-24 10:31:36 +02:00
<div>
<span title='{this.props.feature.description}'>{this.props.feature.name}</span>
<span>{this.props.feature.strategy}</span>
<input type='checkbox' checked={this.props.feature.enabled} onChange={this.onChange} />
</div>
);
}
});
2014-10-21 18:57:05 +02:00
var FeatureList = React.createClass({
render: function() {
var featureNodes = [];
2014-10-23 16:00:13 +02:00
this.props.unsavedFeatures.forEach(function(feature) {
2014-10-24 10:05:15 +02:00
featureNodes.push(<UnsavedFeature feature={feature} onSubmit={this.props.onFeatureSubmit} />);
2014-10-24 10:31:36 +02:00
}.bind(this));
2014-10-23 16:52:41 +02:00
this.props.savedFeatures.forEach(function(feature) {
2014-10-24 10:05:15 +02:00
featureNodes.push(
2014-10-24 10:31:36 +02:00
<SavedFeature
key={feature.name}
feature={feature}
onChange={this.props.onFeatureChanged} />
2014-10-24 10:05:15 +02:00
);
2014-10-24 10:31:36 +02:00
}.bind(this));
2014-10-23 16:52:41 +02:00
2014-10-24 10:40:41 +02:00
return (
<div className='panel panel-primary'>
<div className='panel-heading'>
<h3 className='panel-title'>Features</h3>
</div>
<div className='panel-body'>
{featureNodes}
</div>
</div>
);
}
2014-10-23 16:52:41 +02:00
2014-10-23 16:00:13 +02:00
});
var Unleash = React.createClass({
2014-10-21 18:57:05 +02:00
getInitialState: function() {
return { savedFeatures: [], unsavedFeatures: [] };
2014-10-21 18:57:05 +02:00
},
componentDidMount: function () {
2014-10-23 15:30:38 +02:00
this.loadFeaturesFromServer();
2014-10-24 10:31:36 +02:00
// setInterval(this.loadFeaturesFromServer, this.props.pollInterval);
2014-10-23 15:30:38 +02:00
},
loadFeaturesFromServer: function () {
reqwest('features').then(this.setFeatures, this.handleError);
},
setFeatures: function (data) {
this.setState({savedFeatures: data.features});
2014-10-21 18:57:05 +02:00
},
handleError: function (error) {
2014-10-24 10:31:36 +02:00
// TODO: ErrorComponent could use <div class="alert alert-warning" role="alert">...</div>
window.alert(error);
2014-10-23 15:16:09 +02:00
},
2014-10-24 10:05:15 +02:00
updateFeature: function (changeRequest) {
2014-10-24 10:31:36 +02:00
var newFeatures = this.state.savedFeatures;
newFeatures.forEach(function(f){
if(f.name === changeRequest.name) {
f[changeRequest.field] = changeRequest.value;
}
});
this.setState({features: newFeatures});
reqwest({
url: 'features/' + changeRequest.name,
method: 'patch',
type: 'json',
contentType: 'application/json',
data: JSON.stringify(changeRequest)
}).then(function() {
}, this.handleError.bind(this));
2014-10-24 10:05:15 +02:00
},
createFeature: function (feature) {
console.log(feature);
},
render: function() {
2014-10-21 18:57:05 +02:00
return (
<div>
2014-10-24 10:31:36 +02:00
<Menu />
2014-10-24 10:40:41 +02:00
<FeatureList
unsavedFeatures={this.state.unsavedFeatures}
savedFeatures={this.state.savedFeatures}
onFeatureChanged={this.updateFeature}
onFeatureSubmit={this.createFeature} />
2014-10-21 18:57:05 +02:00
</div>
);
}
});
2014-10-21 18:57:05 +02:00
React.renderComponent(
<Unleash pollInterval={5000} />,
2014-10-21 18:57:05 +02:00
document.getElementById('content')
);