1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/packages/unleash-frontend/public/js/stores/FeatureToggleStore.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

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');
2016-06-18 21:53:18 +02:00
let _featureToggles = [];
2016-06-18 21:53:18 +02:00
const FeatureStore = Reflux.createStore({
2015-03-17 18:16:06 +01:00
// Initial setup
2016-07-02 11:54:50 +02:00
init () {
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) {
this.setToggles([feature].concat(_featureToggles));
},
2016-07-02 11:54:50 +02:00
setToggles (toggles) {
_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);
_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);
this.setToggles(featureToggles);
this.trigger();
},
2016-07-02 11:54:50 +02:00
onRevive (item) {
this.setToggles(_featureToggles.concat([item]));
this.trigger();
},
2016-07-02 11:54:50 +02:00
getFeatureToggles () {
return _featureToggles;
},
2016-07-02 11:54:50 +02:00
initStore (toggles) {
_featureToggles = toggles;
2016-06-18 21:55:46 +02:00
},
});
module.exports = FeatureStore;