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
2020-02-20 08:30:27 +01:00

57 lines
1.6 KiB
JavaScript

'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');
let _featureToggles = [];
const FeatureStore = Reflux.createStore({
// Initial setup
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);
},
onCreate(feature) {
this.setToggles([feature].concat(_featureToggles));
},
setToggles(toggles) {
_featureToggles = sortBy(toggles, 'name');
this.trigger();
},
onUpdate(feature) {
const idx = findIndex(_featureToggles, 'name', feature.name);
_featureToggles[idx] = feature;
this.trigger();
},
onArchive(feature) {
const featureToggles = filter(_featureToggles, item => item.name !== feature.name);
this.setToggles(featureToggles);
this.trigger();
},
onRevive(item) {
this.setToggles(_featureToggles.concat([item]));
this.trigger();
},
getFeatureToggles() {
return _featureToggles;
},
initStore(toggles) {
_featureToggles = toggles;
},
});
module.exports = FeatureStore;