1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

Inital rewrite of feature-store to reflux

This commit is contained in:
Ivar Conradi Østhus 2015-03-02 21:48:42 +01:00
parent 30c764bb31
commit cbf884dd76
4 changed files with 87 additions and 19 deletions

View File

@ -51,6 +51,7 @@
"nconf": "0.6.9",
"pg": "3.6.1",
"react": "^0.12.0",
"reflux": "^0.2.5",
"reqwest": "^1.1.4",
"webpack": "1.4.15",
"webpack-dev-middleware": "^1.0.11"

View File

@ -4,33 +4,20 @@ var ErrorMessages = require('../ErrorMessages');
var FeatureList = require('./FeatureList');
var FeatureForm = require('./FeatureForm');
var FeatureStore = require('../../stores/FeatureStore');
var FeatureStore2 = require('../../stores/FeatureStore2');
var FeatureActions = require('../../stores/FeatureActions');
var Reflux = require('reflux');
var FeatureTogglesComponent = React.createClass({
getInitialState: function() {
return {
features: [],
errors: [],
createView: false,
featurePoller: new Timer(this.loadFeaturesFromServer, this.props.pollInterval)
};
},
componentDidMount: function () {
this.loadFeaturesFromServer();
this.startFeaturePoller();
},
componentWillUnmount: function () {
this.stopFeaturePoller();
},
loadFeaturesFromServer: function () {
FeatureStore.getFeatures().then(this.setFeatures).catch(this.handleError);
},
setFeatures: function (data) {
this.setState({features: data.features});
},
mixins: [Reflux.connect(FeatureStore2,"features")],
handleError: function (error) {
if (this.isClientError(error)) {
@ -78,13 +65,20 @@ var FeatureTogglesComponent = React.createClass({
},
createFeature: function (feature) {
this.stopFeaturePoller();
//this.stopFeaturePoller();
FeatureActions.addToggle.triggerPromise(feature)
.then(this.cancelNewFeature)
.catch(this.handleError);
/*
FeatureStore
.createFeature(feature)
.then(this.cancelNewFeature)
.then(this.startFeaturePoller)
.catch(this.handleError);
*/
},
newFeature: function() {
@ -150,4 +144,4 @@ var FeatureTogglesComponent = React.createClass({
}
});
module.exports = FeatureTogglesComponent;
module.exports = FeatureTogglesComponent;

View File

@ -0,0 +1,4 @@
var Reflux = require("reflux");
module.exports = {
addToggle: Reflux.createAction({ asyncResult: true })
};

View File

@ -0,0 +1,69 @@
var reqwest = require('reqwest');
var Reflux = require('reflux');
var FeatureActions = require('./FeatureActions');
var TYPE = 'json';
var CONTENT_TYPE = 'application/json';
function getFeatures() {
return reqwest({
url: 'features',
method: 'get',
type: CONTENT_TYPE
});
}
var _toggles = [];
// Creates a DataStore
var FeatureStore = Reflux.createStore({
// Initial setup
init: function() {
this.listenTo(FeatureActions.addToggle, this.onAddToggle);
getFeatures()
.then(function(data) {
this.setToggles(JSON.parse(data.response).features);
}.bind(this))
.catch(this.handleError);
},
onAddToggle: function(feature) {
reqwest({
url: 'features',
method: 'post',
type: TYPE,
contentType: CONTENT_TYPE,
data: JSON.stringify(feature),
error: function (error) {
FeatureActions.addToggle.failed(error);
},
success: function () {
this.setToggles(_toggles.concat([feature]));
FeatureActions.addToggle.completed();
}.bind(this)
});
},
setToggles: function(toggles) {
_toggles = toggles;
this.trigger(_toggles);
},
handleError: function(er) {
console.log("error: "+ er);
},
//getter for notes
getToggles: function() {
return _toggles;
},
getInitialState: function() {
return _toggles;
}
});
module.exports = FeatureStore;