diff --git a/public/js/UnleashApp.jsx b/public/js/UnleashApp.jsx index 3690e0cb61..e15b0d1256 100644 --- a/public/js/UnleashApp.jsx +++ b/public/js/UnleashApp.jsx @@ -3,6 +3,7 @@ var TabView = require('./components/TabView'); var Menu = require('./components/Menu'); var UserStore = require('./stores/UserStore'); var ErrorMessages = require('./components/ErrorMessages'); +var initalizer = require('./stores/initalizer'); var LogEntriesComponent = React.createFactory(require('./components/log/LogEntriesComponent')); var FeatureTogglesComponent = React.createFactory(require('./components/feature/FeatureTogglesComponent')); var StrategiesComponent = React.createFactory(require('./components/strategy/StrategiesComponent')); @@ -35,15 +36,9 @@ var tabPanes = [ var UnleashApp = React.createClass({ - getInitialState: function() { - return { - featureToggles: [], - archivedToggles: [], - strategies: [] - - }; + componentWillMount: function() { + initalizer(30); }, - render: function () { return (
diff --git a/public/js/stores/ArchivedToggleStore.js b/public/js/stores/ArchivedToggleStore.js index 15b8dea8f7..607f24ca31 100644 --- a/public/js/stores/ArchivedToggleStore.js +++ b/public/js/stores/ArchivedToggleStore.js @@ -1,8 +1,5 @@ var Reflux = require('reflux'); var FeatureActions = require('./FeatureToggleActions'); -var ErrorActions = require('./ErrorActions'); -var Timer = require('../utils/Timer'); -var Server = require('./FeatureToggleServerFacade'); var filter = require('lodash/collection/filter'); var sortBy = require('lodash/collection/sortBy'); @@ -13,26 +10,15 @@ var FeatureStore = Reflux.createStore({ // Initial setup init: function() { + this.listenTo(FeatureActions.initArchive.completed, this.onInit); this.listenTo(FeatureActions.archive.completed, this.onArchive); this.listenTo(FeatureActions.revive.completed, this.onRevive); - this.timer = new Timer(this.loadDataFromServer, 30*1000); - this.timer.start(); }, - loadDataFromServer: function() { - //TODO: this should not be part of the store! - Server.getArchivedFeatures(function(err, archivedToggles) { - - if(err) { - ErrorActions.error(err); - return; - } else { - _archivedToggles = archivedToggles; - this.trigger(_archivedToggles); - } - - }.bind(this)); + onInit: function(toggles) { + _archivedToggles = toggles; + this.trigger(); }, onArchive: function(feature) { diff --git a/public/js/stores/FeatureToggleActions.js b/public/js/stores/FeatureToggleActions.js index c06c5ee499..f41f071057 100644 --- a/public/js/stores/FeatureToggleActions.js +++ b/public/js/stores/FeatureToggleActions.js @@ -2,12 +2,34 @@ var Reflux = require("reflux"); var Server = require('./FeatureToggleServerFacade'); var FeatureToggleActions = Reflux.createActions({ + 'init': { asyncResult: true }, + 'initArchive':{ asyncResult: true }, 'create': { asyncResult: true }, 'update': { asyncResult: true }, 'archive': { asyncResult: true }, 'revive': { asyncResult: true } }); +FeatureToggleActions.init.listen(function(){ + Server.getFeatures(function(error, features) { + if(error) { + this.failed(error); + } else { + this.completed(features); + } + }.bind(this)); +}); + +FeatureToggleActions.initArchive.listen(function(){ + Server.getArchivedFeatures(function(error, archivedToggles) { + if(error) { + this.failed(error); + } else { + this.completed(archivedToggles); + } + }.bind(this)); +}); + FeatureToggleActions.create.listen(function(feature){ Server.createFeature(feature, function(error) { if(error) { diff --git a/public/js/stores/FeatureToggleStore.js b/public/js/stores/FeatureToggleStore.js index 672b94017d..ab34dc6e6a 100644 --- a/public/js/stores/FeatureToggleStore.js +++ b/public/js/stores/FeatureToggleStore.js @@ -1,8 +1,5 @@ var Reflux = require('reflux'); var FeatureActions = require('./FeatureToggleActions'); -var ErrorActions = require('./ErrorActions'); -var Server = require('./FeatureToggleServerFacade'); -var Timer = require('../utils/Timer'); var filter = require('lodash/collection/filter'); var sortBy = require('lodash/collection/sortBy'); var findIndex = require('lodash/array/findIndex'); @@ -15,26 +12,11 @@ var FeatureStore = Reflux.createStore({ // Initial setup init: function() { + this.listenTo(FeatureActions.init.completed, this.setToggles); this.listenTo(FeatureActions.create.completed, this.onCreate); this.listenTo(FeatureActions.update.completed, this.onUpdate); this.listenTo(FeatureActions.archive.completed, this.onArchive); this.listenTo(FeatureActions.revive.completed, this.onRevive); - - //TODO: this should not be part of the store! - this.timer = new Timer(this.loadDataFromServer, 30*1000); - this.timer.start(); - }, - - loadDataFromServer: function() { - //TODO: this should not be part of the store! - Server.getFeatures(function(err, featureToggles) { - if(err) { - ErrorActions.error(err); - return; - } else { - this.setToggles(featureToggles); - } - }.bind(this)); }, onCreate: function(feature) { diff --git a/public/js/stores/StrategyActions.js b/public/js/stores/StrategyActions.js index 8dcc5f391a..aa135cf1f1 100644 --- a/public/js/stores/StrategyActions.js +++ b/public/js/stores/StrategyActions.js @@ -2,10 +2,21 @@ var Reflux = require("reflux"); var StrategyAPI = require('./StrategyAPI'); var StrategyActions = Reflux.createActions({ + 'init': { asyncResult: true }, 'create': { asyncResult: true }, 'remove': { asyncResult: true }, }); +StrategyActions.init.listen(function(){ + StrategyAPI.getStrategies(function(err, strategies) { + if(err) { + this.failed(err); + } else { + this.completed(strategies); + } + }.bind(this)); +}); + StrategyActions.create.listen(function(feature){ StrategyAPI.createStrategy(feature, function(err) { if(err) { diff --git a/public/js/stores/StrategyStore.js b/public/js/stores/StrategyStore.js index f25c853fc0..da571022b2 100644 --- a/public/js/stores/StrategyStore.js +++ b/public/js/stores/StrategyStore.js @@ -1,7 +1,5 @@ var Reflux = require('reflux'); -var ErrorActions = require('./ErrorActions'); var StrategyActions = require('./StrategyActions'); -var StrategyAPI = require('./StrategyAPI'); var filter = require('lodash/collection/filter'); var _strategies = []; @@ -11,22 +9,9 @@ var StrategyStore = Reflux.createStore({ // Initial setup init: function() { + this.listenTo(StrategyActions.init.completed, this.setStrategies); this.listenTo(StrategyActions.create.completed, this.onCreate); this.listenTo(StrategyActions.remove.completed, this.onRemove); - this.loadDataFromServer(); - }, - - loadDataFromServer: function() { - //TODO: this should not be part of the store! - StrategyAPI.getStrategies(function(err, strategies) { - - if(err) { - ErrorActions.error(err); - return; - } else { - this.setStrategies(strategies); - } - }.bind(this)); }, onCreate: function(strategy) { diff --git a/public/js/stores/initalizer.js b/public/js/stores/initalizer.js new file mode 100644 index 0000000000..e949b19d63 --- /dev/null +++ b/public/js/stores/initalizer.js @@ -0,0 +1,17 @@ +var FeatureToggleActions = require('./FeatureToggleActions'); +var StrategyActions = require('./StrategyActions'); +var Timer = require('../utils/Timer'); + +var _timer; + +function load() { + FeatureToggleActions.init.triggerPromise(); + StrategyActions.init.triggerPromise(); + FeatureToggleActions.initArchive.triggerPromise(); +} + +module.exports = function(pollInterval) { + var intervall = pollInterval || 30; + _timer = new Timer(load, intervall*1000); + _timer.start(); +};