2015-03-03 19:27:17 +01:00
|
|
|
var React = require('react');
|
|
|
|
var ErrorMessages = require('../ErrorMessages');
|
|
|
|
var FeatureList = require('./FeatureList');
|
|
|
|
var FeatureForm = require('./FeatureForm');
|
|
|
|
var FeatureActions = require('../../stores/FeatureToggleActions');
|
|
|
|
var FeatureToggleStore = require('../../stores/FeatureToggleStore');
|
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 {
|
2015-03-03 19:27:17 +01:00
|
|
|
features: FeatureToggleStore.getFeatureToggles(),
|
2014-10-30 18:25:38 +01:00
|
|
|
errors: [],
|
2015-03-03 19:27:17 +01:00
|
|
|
createView: false
|
2014-10-30 18:25:38 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-03-03 19:27:17 +01:00
|
|
|
onFeatureToggleChange: function() {
|
|
|
|
this.setState({
|
|
|
|
features: FeatureToggleStore.getFeatureToggles()
|
|
|
|
});
|
|
|
|
},
|
|
|
|
componentDidMount: function() {
|
|
|
|
this.unsubscribe = FeatureToggleStore.listen(this.onFeatureToggleChange);
|
|
|
|
},
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
this.unsubscribe();
|
|
|
|
},
|
2014-10-30 18:25:38 +01:00
|
|
|
|
|
|
|
handleError: function (error) {
|
2015-03-03 19:27:17 +01:00
|
|
|
console.log(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-30 18:25:38 +01:00
|
|
|
},
|
|
|
|
|
2014-11-14 12:56:23 +01:00
|
|
|
updateFeature: function (feature) {
|
2015-03-03 19:27:17 +01:00
|
|
|
FeatureActions.update.triggerPromise(feature)
|
2014-10-31 12:25:18 +01:00
|
|
|
.catch(this.handleError);
|
|
|
|
},
|
|
|
|
|
2014-12-15 22:40:07 +01:00
|
|
|
archiveFeature: function (feature) {
|
2015-03-03 19:27:17 +01:00
|
|
|
FeatureActions.archive.triggerPromise(feature)
|
2014-12-15 22:40:07 +01:00
|
|
|
.catch(this.handleError);
|
|
|
|
},
|
|
|
|
|
2014-10-30 18:25:38 +01:00
|
|
|
createFeature: function (feature) {
|
2015-03-03 19:27:17 +01:00
|
|
|
FeatureActions.create.triggerPromise(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) {
|
2015-03-03 19:27:17 +01:00
|
|
|
var errors = this.state.errors;
|
|
|
|
if (errors[errors.length - 1] !== msg) {
|
|
|
|
errors.push(msg);
|
|
|
|
this.setState(errors);
|
2014-11-10 15:25:27 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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>
|
2014-11-01 14:52:37 +01:00
|
|
|
<ErrorMessages
|
2014-11-03 13:19:32 +01:00
|
|
|
errors={this.state.errors}
|
|
|
|
onClearErrors={this.clearErrors} />
|
2014-11-03 13:54:06 +01:00
|
|
|
|
|
|
|
{this.state.createView ? this.renderCreateView() : this.renderCreateButton()}
|
|
|
|
|
2014-11-01 14:52:37 +01:00
|
|
|
<FeatureList
|
2014-11-03 13:54:06 +01:00
|
|
|
features={this.state.features}
|
2014-11-03 13:19:32 +01:00
|
|
|
onFeatureChanged={this.updateFeature}
|
2014-12-15 22:40:07 +01:00
|
|
|
onFeatureArchive={this.archiveFeature}
|
2014-11-03 13:19:32 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-02 21:48:42 +01:00
|
|
|
module.exports = FeatureTogglesComponent;
|