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

135 lines
3.8 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) {
2014-11-10 14:55:56 +01:00
if (this.isClientError(error)) {
var errors = JSON.parse(error.responseText)
2014-11-10 15:25:27 +01:00
errors.forEach(function(e) { this.addError(e.msg); }.bind(this))
} else if (error.status === 0) {
this.addError("server unreachable");
2014-11-10 14:55:56 +01:00
} else {
2014-11-10 15:25:27 +01:00
this.addError(error);
2014-11-10 14:55:56 +01:00
}
2014-10-31 12:25:18 +01:00
this.forceUpdate();
2014-10-30 18:25:38 +01:00
},
updateFeature: function (feature) {
2014-10-31 12:25:18 +01:00
this.stopFeaturePoller();
this.state.featureStore
.updateFeature(feature)
2014-10-31 12:25:18 +01:00
.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-11-10 15:25:27 +01:00
addError: function(msg) {
if (this.state.errors[this.state.errors.length - 1] !== msg) {
this.state.errors.push(msg);
}
},
2014-11-10 14:55:56 +01:00
isClientError: function(error) {
try {
return error.status >= 400 &&
error.status < 500 &&
JSON.parse(error.responseText);
} catch (e) {
if (e instanceof SyntaxError) {
// fall through;
} else {
throw e;
}
}
return false;
},
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()}
<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 14:00:45 +01:00
module.exports = FeatureTogglesComponent;