1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/public/js/stores/FeatureToggleStore.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

var Reflux = require('reflux');
var FeatureActions = require('./FeatureToggleActions');
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');
var _featureToggles = [];
var FeatureStore = Reflux.createStore({
2015-03-17 18:16:06 +01:00
// Initial setup
init: function() {
2015-03-17 22:52:10 +01:00
this.listenTo(FeatureActions.init.completed, this.setToggles);
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);
},
onCreate: function(feature) {
2015-03-16 21:27:56 +01:00
this.setToggles([feature].concat(_featureToggles));
},
setToggles: function(toggles) {
2015-03-17 18:16:06 +01:00
_featureToggles = sortBy(toggles, 'name');
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);
_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-17 18:16:06 +01:00
this.setToggles(featureToggles);
this.trigger();
},
2015-03-16 21:27:56 +01:00
onRevive: function(item) {
2015-03-17 18:16:06 +01:00
this.setToggles(_featureToggles.concat([item]));
this.trigger();
},
getFeatureToggles: function() {
return _featureToggles;
},
2015-03-17 18:31:55 +01:00
initStore: function(toggles) {
_featureToggles = toggles;
}
});
module.exports = FeatureStore;