From 96600c5598a53f908166152e38639f7d8ab7fefa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Tue, 17 Mar 2015 18:31:55 +0100 Subject: [PATCH] Archived toggles in separate store --- .../feature/ArchiveFeatureComponent-test.js | 4 +- .../feature/ArchiveFeatureComponent.jsx | 2 +- public/js/stores/ArchivedToggleStore.js | 55 +++++++++++++++++++ public/js/stores/FeatureToggleStore.js | 19 +------ 4 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 public/js/stores/ArchivedToggleStore.js diff --git a/public/js/__tests__/components/feature/ArchiveFeatureComponent-test.js b/public/js/__tests__/components/feature/ArchiveFeatureComponent-test.js index a461da3c43..871071f51f 100644 --- a/public/js/__tests__/components/feature/ArchiveFeatureComponent-test.js +++ b/public/js/__tests__/components/feature/ArchiveFeatureComponent-test.js @@ -6,7 +6,7 @@ var React = require("react/addons"); var TestUtils = React.addons.TestUtils; var FeatureArchive = require("../../../components/feature/ArchiveFeatureComponent"); var Server = require("../../../stores/FeatureToggleServerFacade"); -var FeatureToggleStore = require("../../../stores/FeatureToggleStore"); +var FeatureToggleStore = require("../../../stores/ArchivedToggleStore"); describe("FeatureForm", function () { var Component; @@ -20,7 +20,7 @@ describe("FeatureForm", function () { cb(archivedToggles); }); - FeatureToggleStore.initStore([], archivedToggles); + FeatureToggleStore.initStore(archivedToggles); Component = TestUtils.renderIntoDocument(); }); diff --git a/public/js/components/feature/ArchiveFeatureComponent.jsx b/public/js/components/feature/ArchiveFeatureComponent.jsx index 0443a930ea..1f2e669129 100644 --- a/public/js/components/feature/ArchiveFeatureComponent.jsx +++ b/public/js/components/feature/ArchiveFeatureComponent.jsx @@ -1,6 +1,6 @@ var React = require("react"); var FeatureActions = require('../../stores/FeatureToggleActions'); -var FeatureToggleStore = require('../../stores/FeatureToggleStore'); +var FeatureToggleStore = require('../../stores/ArchivedToggleStore'); var ArchiveFeatureComponent = React.createClass({ getInitialState: function() { diff --git a/public/js/stores/ArchivedToggleStore.js b/public/js/stores/ArchivedToggleStore.js new file mode 100644 index 0000000000..8c7265f591 --- /dev/null +++ b/public/js/stores/ArchivedToggleStore.js @@ -0,0 +1,55 @@ +var Reflux = require('reflux'); +var FeatureActions = require('./FeatureToggleActions'); +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({ + //The store should be split in two: toggleStore && archivedToggleStore! + + // 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) { + _archivedToggles = archivedToggles; + this.trigger(); + }.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; + } +}); + +module.exports = FeatureStore; diff --git a/public/js/stores/FeatureToggleStore.js b/public/js/stores/FeatureToggleStore.js index d42c1baf97..bd09302346 100644 --- a/public/js/stores/FeatureToggleStore.js +++ b/public/js/stores/FeatureToggleStore.js @@ -6,9 +6,7 @@ var filter = require('lodash/collection/filter'); var sortBy = require('lodash/collection/sortBy'); var findIndex = require('lodash/array/findIndex'); -//TODO: have archived toggles in seperate store. var _featureToggles = []; -var _archivedToggles = []; // Creates a DataStore var FeatureStore = Reflux.createStore({ @@ -31,11 +29,6 @@ var FeatureStore = Reflux.createStore({ Server.getFeatures(function(err, featureToggles) { this.setToggles(featureToggles); }.bind(this)); - - Server.getArchivedFeatures(function(err, archivedToggles) { - _archivedToggles = archivedToggles; - this.trigger(); - }.bind(this)); }, onCreate: function(feature) { @@ -57,16 +50,11 @@ var FeatureStore = Reflux.createStore({ var featureToggles = filter(_featureToggles, function(item) { return item.name !== feature.name; }); - _archivedToggles.unshift(feature); this.setToggles(featureToggles); this.trigger(); }, onRevive: function(item) { - var newStore = _archivedToggles.filter(function(f) { - return f.name !== item.name; - }); - _archivedToggles = newStore; this.setToggles(_featureToggles.concat([item])); this.trigger(); }, @@ -75,13 +63,8 @@ var FeatureStore = Reflux.createStore({ return _featureToggles; }, - getArchivedToggles: function() { - return _archivedToggles; - }, - - initStore: function(toggles, archivedToggles) { + initStore: function(toggles) { _featureToggles = toggles; - _archivedToggles = archivedToggles; } });