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/FeatureTogglesComponent.jsx

116 lines
3.2 KiB
React
Raw Normal View History

2014-10-30 18:25:38 +01:00
var React = require('react');
var Timer = require('../../utils/Timer');
var ErrorMessages = require('../ErrorMessages');
2014-10-30 18:25:38 +01:00
var FeatureList = require('./FeatureList');
2014-11-03 13:54:06 +01:00
var FeatureForm = require('./FeatureForm');
var FeatureStore = require('../../stores/FeatureStore');
2014-10-30 18:25:38 +01:00
2014-11-03 14:00:45 +01:00
var FeatureTogglesComponent = React.createClass({
2014-10-30 18:25:38 +01:00
getInitialState: function() {
return {
2014-11-03 13:54:06 +01:00
features: [],
2014-10-30 18:25:38 +01:00
errors: [],
2014-11-03 13:54:06 +01:00
createView: false,
2014-10-31 12:25:18 +01:00
featurePoller: new Timer(this.loadFeaturesFromServer, this.props.pollInterval),
featureStore: new FeatureStore()
2014-10-30 18:25:38 +01:00
};
},
componentDidMount: function () {
this.loadFeaturesFromServer();
2014-10-31 12:25:18 +01:00
this.startFeaturePoller();
2014-10-30 18:25:38 +01:00
},
componentWillUnmount: function () {
2014-10-31 12:25:18 +01:00
this.stopFeaturePoller();
2014-10-30 18:25:38 +01:00
},
loadFeaturesFromServer: function () {
2014-10-31 12:25:18 +01:00
this.state.featureStore.getFeatures().then(this.setFeatures).catch(this.handleError);
2014-10-30 18:25:38 +01:00
},
setFeatures: function (data) {
2014-11-03 13:54:06 +01:00
this.setState({features: data.features});
2014-10-30 18:25:38 +01:00
},
handleError: function (error) {
this.state.errors.push(error);
2014-10-31 12:25:18 +01:00
this.forceUpdate();
2014-10-30 18:25:38 +01:00
},
updateFeature: function (changeRequest) {
2014-11-03 13:54:06 +01:00
var newFeatures = this.state.features;
2014-10-30 18:25:38 +01:00
newFeatures.forEach(function(f){
if(f.name === changeRequest.name) {
f[changeRequest.field] = changeRequest.value;
}
});
this.setState({features: newFeatures});
2014-10-31 12:25:18 +01:00
this.stopFeaturePoller();
this.state.featureStore.updateFeature(changeRequest)
.then(this.startFeaturePoller)
.catch(this.handleError);
},
startFeaturePoller: function () {
this.state.featurePoller.start();
},
stopFeaturePoller: function () {
if (this.state.featurePoller != null) {
this.state.featurePoller.stop();
}
2014-10-30 18:25:38 +01:00
},
createFeature: function (feature) {
2014-10-31 12:25:18 +01:00
this.state.featureStore.createFeature(feature)
2014-11-03 13:54:06 +01:00
.then(this.cancelNewFeature)
2014-10-31 12:25:18 +01:00
.catch(this.handleError);
2014-10-30 18:25:38 +01:00
},
newFeature: function() {
2014-11-03 13:54:06 +01:00
this.setState({createView: true});
2014-10-30 18:25:38 +01:00
},
cancelNewFeature: function (feature) {
2014-11-03 13:54:06 +01:00
this.setState({createView: false});
2014-10-30 18:25:38 +01:00
},
2014-10-31 10:30:23 +01:00
clearErrors: function() {
this.setState({errors: []});
},
2014-10-30 18:25:38 +01:00
render: function() {
return (
<div>
<ErrorMessages
errors={this.state.errors}
onClearErrors={this.clearErrors} />
2014-11-03 13:54:06 +01:00
{this.state.createView ? this.renderCreateView() : this.renderCreateButton()}
<hr />
<FeatureList
2014-11-03 13:54:06 +01:00
features={this.state.features}
onFeatureChanged={this.updateFeature}
onFeatureSubmit={this.createFeature}
onFeatureCancel={this.cancelNewFeature}
onNewFeature={this.newFeature} />
2014-10-30 18:25:38 +01:00
</div>
);
2014-11-03 13:54:06 +01:00
},
renderCreateView: function() {
return <FeatureForm onCancel={this.cancelNewFeature} onSubmit={this.createFeature} />
},
renderCreateButton: function() {
return <button className="mal" onClick={this.newFeature}>Create feature toggle</button>
2014-10-30 18:25:38 +01:00
}
});
2014-11-03 13:54:06 +01:00
2014-11-03 14:00:45 +01:00
module.exports = FeatureTogglesComponent;