2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
|
|
|
const Reflux = require('reflux');
|
|
|
|
const FeatureActions = require('./FeatureToggleActions');
|
|
|
|
const filter = require('lodash/collection/filter');
|
|
|
|
const sortBy = require('lodash/collection/sortBy');
|
|
|
|
const findIndex = require('lodash/array/findIndex');
|
2015-03-03 19:27:17 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
let _featureToggles = [];
|
2015-03-03 19:27:17 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const FeatureStore = Reflux.createStore({
|
2015-03-17 18:16:06 +01:00
|
|
|
|
2015-03-03 19:27:17 +01:00
|
|
|
// Initial setup
|
2016-07-02 11:54:50 +02:00
|
|
|
init () {
|
2016-04-24 22:41:37 +02:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
onCreate (feature) {
|
2016-04-24 22:41:37 +02:00
|
|
|
this.setToggles([feature].concat(_featureToggles));
|
|
|
|
},
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
setToggles (toggles) {
|
2016-04-24 22:41:37 +02:00
|
|
|
_featureToggles = sortBy(toggles, 'name');
|
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
onUpdate (feature) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const idx = findIndex(_featureToggles, 'name', feature.name);
|
2016-04-24 22:41:37 +02:00
|
|
|
_featureToggles[idx] = feature;
|
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
onArchive (feature) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const featureToggles = filter(_featureToggles, item => item.name !== feature.name);
|
2016-04-24 22:41:37 +02:00
|
|
|
this.setToggles(featureToggles);
|
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
onRevive (item) {
|
2016-04-24 22:41:37 +02:00
|
|
|
this.setToggles(_featureToggles.concat([item]));
|
|
|
|
this.trigger();
|
|
|
|
},
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
getFeatureToggles () {
|
2016-04-24 22:41:37 +02:00
|
|
|
return _featureToggles;
|
|
|
|
},
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
initStore (toggles) {
|
2016-04-24 22:41:37 +02:00
|
|
|
_featureToggles = toggles;
|
2016-06-18 21:55:46 +02:00
|
|
|
},
|
2015-03-03 19:27:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = FeatureStore;
|