2015-03-03 19:27:17 +01:00
|
|
|
var Reflux = require('reflux');
|
|
|
|
var FeatureActions = require('./FeatureToggleActions');
|
|
|
|
var Server = require('./FeatureToggleServerFacade');
|
2015-03-05 17:55:21 +01:00
|
|
|
var Timer = require('../utils/Timer');
|
2015-03-17 18:16:06 +01:00
|
|
|
var filter = require('lodash/collection/filter');
|
|
|
|
var sortBy = require('lodash/collection/sortBy');
|
|
|
|
var findIndex = require('lodash/array/findIndex');
|
2015-03-03 19:27:17 +01:00
|
|
|
|
2015-03-17 18:16:06 +01:00
|
|
|
//TODO: have archived toggles in seperate store.
|
2015-03-03 19:27:17 +01:00
|
|
|
var _featureToggles = [];
|
|
|
|
var _archivedToggles = [];
|
|
|
|
|
|
|
|
// Creates a DataStore
|
|
|
|
var FeatureStore = Reflux.createStore({
|
2015-03-17 18:16:06 +01:00
|
|
|
//The store should be split in two: toggleStore && archivedToggleStore!
|
|
|
|
|
2015-03-03 19:27:17 +01:00
|
|
|
// Initial setup
|
|
|
|
init: function() {
|
2015-03-16 21:27:56 +01:00
|
|
|
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);
|
2015-03-03 19:27:17 +01:00
|
|
|
|
2015-03-17 18:16:06 +01:00
|
|
|
//TODO: this should not be part of the store!
|
2015-03-05 17:55:21 +01:00
|
|
|
this.timer = new Timer(this.loadDataFromServer, 30*1000);
|
|
|
|
this.timer.start();
|
|
|
|
},
|
|
|
|
|
|
|
|
loadDataFromServer: function() {
|
2015-03-17 18:16:06 +01:00
|
|
|
//TODO: this should not be part of the store!
|
2015-03-03 19:27:17 +01:00
|
|
|
Server.getFeatures(function(err, featureToggles) {
|
|
|
|
this.setToggles(featureToggles);
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
Server.getArchivedFeatures(function(err, archivedToggles) {
|
|
|
|
_archivedToggles = archivedToggles;
|
|
|
|
this.trigger();
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
onCreate: function(feature) {
|
2015-03-16 21:27:56 +01:00
|
|
|
this.setToggles([feature].concat(_featureToggles));
|
2015-03-03 19:27:17 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
setToggles: function(toggles) {
|
2015-03-17 18:16:06 +01:00
|
|
|
_featureToggles = sortBy(toggles, 'name');
|
2015-03-03 19:27:17 +01:00
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
2015-03-16 21:27:56 +01:00
|
|
|
onUpdate: function(feature) {
|
2015-03-17 18:16:06 +01:00
|
|
|
var idx = findIndex(_featureToggles, 'name', feature.name);
|
2015-03-03 19:27:17 +01:00
|
|
|
_featureToggles[idx] = feature;
|
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
2015-03-16 21:27:56 +01:00
|
|
|
onArchive: function(feature) {
|
2015-03-17 18:16:06 +01:00
|
|
|
var featureToggles = filter(_featureToggles, function(item) {
|
|
|
|
return item.name !== feature.name;
|
2015-03-03 19:27:17 +01:00
|
|
|
});
|
|
|
|
_archivedToggles.unshift(feature);
|
2015-03-17 18:16:06 +01:00
|
|
|
this.setToggles(featureToggles);
|
2015-03-03 19:27:17 +01:00
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
2015-03-16 21:27:56 +01:00
|
|
|
onRevive: function(item) {
|
2015-03-03 19:27:17 +01:00
|
|
|
var newStore = _archivedToggles.filter(function(f) {
|
|
|
|
return f.name !== item.name;
|
|
|
|
});
|
|
|
|
_archivedToggles = newStore;
|
2015-03-17 18:16:06 +01:00
|
|
|
this.setToggles(_featureToggles.concat([item]));
|
2015-03-03 19:27:17 +01:00
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
|
|
|
getFeatureToggles: function() {
|
|
|
|
return _featureToggles;
|
|
|
|
},
|
|
|
|
|
|
|
|
getArchivedToggles: function() {
|
|
|
|
return _archivedToggles;
|
|
|
|
},
|
|
|
|
|
|
|
|
initStore: function(toggles, archivedToggles) {
|
|
|
|
_featureToggles = toggles;
|
|
|
|
_archivedToggles = archivedToggles;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = FeatureStore;
|