2015-03-17 18:31:55 +01:00
|
|
|
var Reflux = require('reflux');
|
|
|
|
var FeatureActions = require('./FeatureToggleActions');
|
2015-03-17 20:34:19 +01:00
|
|
|
var ErrorActions = require('./ErrorActions');
|
2015-03-17 18:31:55 +01:00
|
|
|
var Timer = require('../utils/Timer');
|
|
|
|
var Server = require('./FeatureToggleServerFacade');
|
|
|
|
var filter = require('lodash/collection/filter');
|
|
|
|
var sortBy = require('lodash/collection/sortBy');
|
|
|
|
|
|
|
|
var _archivedToggles = [];
|
|
|
|
|
|
|
|
// Creates a DataStore
|
|
|
|
var FeatureStore = Reflux.createStore({
|
2015-03-17 20:34:19 +01:00
|
|
|
|
|
|
|
// Initial setup
|
|
|
|
init: function() {
|
|
|
|
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));
|
|
|
|
},
|
|
|
|
|
|
|
|
onArchive: function(feature) {
|
|
|
|
var toggles = _archivedToggles.concat([feature]);
|
|
|
|
_archivedToggles = sortBy(toggles, 'name');
|
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
|
|
|
onRevive: function(item) {
|
|
|
|
var newStore = filter(_archivedToggles, function(f) {
|
|
|
|
return f.name !== item.name;
|
|
|
|
});
|
|
|
|
|
|
|
|
_archivedToggles = sortBy(newStore, 'name');
|
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
|
|
|
getArchivedToggles: function() {
|
|
|
|
return _archivedToggles;
|
|
|
|
},
|
|
|
|
|
|
|
|
initStore: function(archivedToggles) {
|
|
|
|
_archivedToggles = archivedToggles;
|
|
|
|
}
|
2015-03-17 18:31:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = FeatureStore;
|