1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

Extract into a FeatureStore

This commit is contained in:
Jari Bakken 2014-10-31 12:25:18 +01:00
parent 21a882c237
commit 73b0d22616
3 changed files with 64 additions and 30 deletions

View File

@ -6,8 +6,8 @@ var ErrorMessages = React.createClass({
return <div/>; return <div/>;
} }
var errorNodes = this.props.errors.map(function(e) { var errorNodes = this.props.errors.map(function(e, i) {
return (<li key={e} className="largetext">{e}</li>); return (<li key={e + i} className="largetext">{e}</li>);
}); });
return ( return (

View File

@ -1,9 +1,9 @@
var React = require('react'); var React = require('react');
var reqwest = require('reqwest');
var Timer = require('../utils/Timer'); var Timer = require('../utils/Timer');
var Menu = require('./Menu'); var Menu = require('./Menu');
var ErrorMessages = require('./ErrorMessages'); var ErrorMessages = require('./ErrorMessages');
var FeatureList = require('./FeatureList'); var FeatureList = require('./FeatureList');
var FeatureStore = require('../stores/FeatureStore');
var Unleash = React.createClass({ var Unleash = React.createClass({
getInitialState: function() { getInitialState: function() {
@ -11,24 +11,22 @@ var Unleash = React.createClass({
savedFeatures: [], savedFeatures: [],
unsavedFeatures: [], unsavedFeatures: [],
errors: [], errors: [],
timer: null featurePoller: new Timer(this.loadFeaturesFromServer, this.props.pollInterval),
featureStore: new FeatureStore()
}; };
}, },
componentDidMount: function () { componentDidMount: function () {
this.loadFeaturesFromServer(); this.loadFeaturesFromServer();
this.state.timer = new Timer(this.loadFeaturesFromServer, this.props.pollInterval); this.startFeaturePoller();
this.state.timer.start();
}, },
componentWillUnmount: function () { componentWillUnmount: function () {
if (this.state.timer != null) { this.stopFeaturePoller();
this.state.timer.stop();
}
}, },
loadFeaturesFromServer: function () { loadFeaturesFromServer: function () {
reqwest('features').then(this.setFeatures, this.handleError); this.state.featureStore.getFeatures().then(this.setFeatures).catch(this.handleError);
}, },
setFeatures: function (data) { setFeatures: function (data) {
@ -37,6 +35,7 @@ var Unleash = React.createClass({
handleError: function (error) { handleError: function (error) {
this.state.errors.push(error); this.state.errors.push(error);
this.forceUpdate();
}, },
updateFeature: function (changeRequest) { updateFeature: function (changeRequest) {
@ -48,18 +47,20 @@ var Unleash = React.createClass({
}); });
this.setState({features: newFeatures}); this.setState({features: newFeatures});
this.state.timer.stop(); this.stopFeaturePoller();
this.state.featureStore.updateFeature(changeRequest)
.then(this.startFeaturePoller)
.catch(this.handleError);
},
reqwest({ startFeaturePoller: function () {
url: 'features/' + changeRequest.name, this.state.featurePoller.start();
method: 'patch', },
type: 'json',
contentType: 'application/json', stopFeaturePoller: function () {
data: JSON.stringify(changeRequest) if (this.state.featurePoller != null) {
}).then(function() { this.state.featurePoller.stop();
// all good }
this.state.timer.start();
}.bind(this), this.handleError);
}, },
createFeature: function (feature) { createFeature: function (feature) {
@ -76,15 +77,9 @@ var Unleash = React.createClass({
this.setState({unsavedFeatures: unsaved}); this.setState({unsavedFeatures: unsaved});
reqwest({ this.state.featureStore.createFeature(feature)
url: 'features', .then(function(r) { console.log(r.statusText); }.bind(this))
method: 'post', .catch(this.handleError);
type: 'json',
contentType: 'application/json',
data: JSON.stringify(feature)
}).then(function(r) {
console.log(r.statusText);
}.bind(this), this.handleError);
}, },
newFeature: function() { newFeature: function() {

View File

@ -0,0 +1,39 @@
var reqwest = require('reqwest');
var FeatureStore = function () {
};
FeatureStore.TYPE = 'json';
FeatureStore.CONTENT_TYPE = 'application/json';
FeatureStore.prototype = {
updateFeature: function (changeRequest) {
return reqwest({
url: 'features/' + changeRequest.name,
method: 'patch',
type: FeatureStore.TYPE,
contentType: FeatureStore.CONTENT_TYPE,
data: JSON.stringify(changeRequest)
});
},
createFeature: function (feature) {
return reqwest({
url: 'features',
method: 'post',
type: FeatureStore.TYPE,
contentType: FeatureStore.CONTENT_TYPE,
data: JSON.stringify(feature)
});
},
getFeatures: function () {
return reqwest({
url: 'features',
method: 'get',
type: FeatureStore.TYPE
});
}
};
module.exports = FeatureStore;