From 7d03c24b44a2f4eba86adf86954280fa73e726e4 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Fri, 2 Oct 2015 09:50:34 -0400
Subject: Changed direct channels in the sidebar to be displayed based on user
 preferences

---
 web/react/stores/preference_store.jsx | 135 ++++++++++++++++++++++++++++++++++
 1 file changed, 135 insertions(+)
 create mode 100644 web/react/stores/preference_store.jsx

(limited to 'web/react/stores/preference_store.jsx')

diff --git a/web/react/stores/preference_store.jsx b/web/react/stores/preference_store.jsx
new file mode 100644
index 000000000..c13c61e1d
--- /dev/null
+++ b/web/react/stores/preference_store.jsx
@@ -0,0 +1,135 @@
+// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+const ActionTypes = require('../utils/constants.jsx').ActionTypes;
+const AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
+const BrowserStore = require('./browser_store.jsx');
+const EventEmitter = require('events').EventEmitter;
+const UserStore = require('../stores/user_store.jsx');
+
+const CHANGE_EVENT = 'change';
+
+class PreferenceStoreClass extends EventEmitter {
+    constructor() {
+        super();
+
+        this.getAllPreferences = this.getAllPreferences.bind(this);
+        this.getPreference = this.getPreference.bind(this);
+        this.getPreferenceWithAltId = this.getPreferenceWithAltId.bind(this);
+        this.getPreferences = this.getPreferences.bind(this);
+        this.getPreferencesWhere = this.getPreferencesWhere.bind(this);
+        this.setAllPreferences = this.setAllPreferences.bind(this);
+        this.setPreference = this.setPreference.bind(this);
+        this.setPreferenceWithAltId = this.setPreferenceWithAltId.bind(this);
+
+        this.emitChange = this.emitChange.bind(this);
+        this.addChangeListener = this.addChangeListener.bind(this);
+        this.removeChangeListener = this.removeChangeListener.bind(this);
+
+        this.handleEventPayload = this.handleEventPayload.bind(this);
+        this.dispatchToken = AppDispatcher.register(this.handleEventPayload);
+    }
+
+    getKey(category, name, altId = '') {
+        return `${category}-${name}-${altId}`;
+    }
+
+    getKeyForModel(preference) {
+        return `${preference.category}-${preference.name}-${preference.alt_id}`;
+    }
+
+    getAllPreferences() {
+        console.log('getting preferences'); // eslint-disable-line no-console
+        return new Map(BrowserStore.getItem('preferences', []));
+    }
+
+    getPreference(category, name, defaultValue = '') {
+        return this.getAllPreferences().get(this.getKey(category, name)) || defaultValue;
+    }
+
+    getPreferenceWithAltId(category, name, altId, defaultValue = '') {
+        return this.getAllPreferences().get(this.getKey(category, name, altId)) || defaultValue;
+    }
+
+    getPreferences(category, name) {
+        return this.getPreferencesWhere((preference) => (preference.category === category && preference.name === name));
+    }
+
+    getPreferencesWhere(pred) {
+        const all = this.getAllPreferences();
+        const preferences = [];
+
+        for (const [, preference] of all) {
+            if (pred(preference)) {
+                preferences.push(preference);
+            }
+        }
+
+        return preferences;
+    }
+
+    setAllPreferences(preferences) {
+        // note that we store the preferences as an array of key-value pairs so that we can deserialize
+        // it as a proper Map instead of an object
+        BrowserStore.setItem('preferences', [...preferences]);
+    }
+
+    setPreference(category, name, value) {
+        this.setPreferenceWithAltId(category, name, '', value);
+    }
+
+    setPreferenceWithAltId(category, name, altId, value) {
+        const preferences = this.getAllPreferences();
+
+        const key = this.getKey(category, name);
+        let preference = preferences.get(key);
+
+        if (!preference) {
+            preference = {
+                user_id: UserStore.getCurrentId(),
+                category,
+                name,
+                alt_id: altId
+            };
+        }
+        preference.value = value;
+
+        preferences.set(key, preference);
+
+        this.setAllPreferences(preferences);
+    }
+
+    emitChange(preferences) {
+        this.emit(CHANGE_EVENT, preferences);
+    }
+
+    addChangeListener(callback) {
+        this.on(CHANGE_EVENT, callback);
+    }
+
+    removeChangeListener(callback) {
+        this.removeListener(CHANGE_EVENT, callback);
+    }
+
+    handleEventPayload(payload) {
+        const action = payload.action;
+
+        switch (action.type) {
+        case ActionTypes.RECIEVED_PREFERENCES:
+            const preferences = this.getAllPreferences();
+
+            for (const preference of action.preferences) {
+                preferences.set(this.getKeyForModel(preference), preference);
+            }
+
+            this.setAllPreferences(preferences);
+            this.emitChange(preferences);
+        }
+    }
+}
+
+const PreferenceStore = new PreferenceStoreClass();
+export default PreferenceStore;
+
+// TODO remove me
+global.PreferenceStore = PreferenceStore;
-- 
cgit v1.2.3-1-g7c22


From ed31538893ad2790de46ace7eeac5c1aa015a7f1 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Fri, 2 Oct 2015 14:25:55 -0400
Subject: Changed direct messages channels so users can show/hide them

---
 web/react/stores/preference_store.jsx | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

(limited to 'web/react/stores/preference_store.jsx')

diff --git a/web/react/stores/preference_store.jsx b/web/react/stores/preference_store.jsx
index c13c61e1d..d731e7d27 100644
--- a/web/react/stores/preference_store.jsx
+++ b/web/react/stores/preference_store.jsx
@@ -75,13 +75,13 @@ class PreferenceStoreClass extends EventEmitter {
     }
 
     setPreference(category, name, value) {
-        this.setPreferenceWithAltId(category, name, '', value);
+        return this.setPreferenceWithAltId(category, name, '', value);
     }
 
     setPreferenceWithAltId(category, name, altId, value) {
         const preferences = this.getAllPreferences();
 
-        const key = this.getKey(category, name);
+        const key = this.getKey(category, name, altId);
         let preference = preferences.get(key);
 
         if (!preference) {
@@ -97,6 +97,8 @@ class PreferenceStoreClass extends EventEmitter {
         preferences.set(key, preference);
 
         this.setAllPreferences(preferences);
+
+        return preference;
     }
 
     emitChange(preferences) {
@@ -130,6 +132,3 @@ class PreferenceStoreClass extends EventEmitter {
 
 const PreferenceStore = new PreferenceStoreClass();
 export default PreferenceStore;
-
-// TODO remove me
-global.PreferenceStore = PreferenceStore;
-- 
cgit v1.2.3-1-g7c22


From 097d236f437b0c5af167cd383c6ee4c3ee45f495 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Wed, 7 Oct 2015 11:34:29 -0400
Subject: Fixed edge cases with leaving a direct channel while viewing that
 channel

---
 web/react/stores/preference_store.jsx | 1 -
 1 file changed, 1 deletion(-)

(limited to 'web/react/stores/preference_store.jsx')

diff --git a/web/react/stores/preference_store.jsx b/web/react/stores/preference_store.jsx
index d731e7d27..8101452ed 100644
--- a/web/react/stores/preference_store.jsx
+++ b/web/react/stores/preference_store.jsx
@@ -39,7 +39,6 @@ class PreferenceStoreClass extends EventEmitter {
     }
 
     getAllPreferences() {
-        console.log('getting preferences'); // eslint-disable-line no-console
         return new Map(BrowserStore.getItem('preferences', []));
     }
 
-- 
cgit v1.2.3-1-g7c22


From 2a39e8dbfab8506b09d0d030f87cac4c079b975a Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 13 Oct 2015 11:52:17 -0400
Subject: Removed Preference.AltId

---
 web/react/stores/preference_store.jsx | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)

(limited to 'web/react/stores/preference_store.jsx')

diff --git a/web/react/stores/preference_store.jsx b/web/react/stores/preference_store.jsx
index 8101452ed..4d7fb9ad6 100644
--- a/web/react/stores/preference_store.jsx
+++ b/web/react/stores/preference_store.jsx
@@ -15,12 +15,10 @@ class PreferenceStoreClass extends EventEmitter {
 
         this.getAllPreferences = this.getAllPreferences.bind(this);
         this.getPreference = this.getPreference.bind(this);
-        this.getPreferenceWithAltId = this.getPreferenceWithAltId.bind(this);
         this.getPreferences = this.getPreferences.bind(this);
         this.getPreferencesWhere = this.getPreferencesWhere.bind(this);
         this.setAllPreferences = this.setAllPreferences.bind(this);
         this.setPreference = this.setPreference.bind(this);
-        this.setPreferenceWithAltId = this.setPreferenceWithAltId.bind(this);
 
         this.emitChange = this.emitChange.bind(this);
         this.addChangeListener = this.addChangeListener.bind(this);
@@ -30,12 +28,12 @@ class PreferenceStoreClass extends EventEmitter {
         this.dispatchToken = AppDispatcher.register(this.handleEventPayload);
     }
 
-    getKey(category, name, altId = '') {
-        return `${category}-${name}-${altId}`;
+    getKey(category, name) {
+        return `${category}-${name}`;
     }
 
     getKeyForModel(preference) {
-        return `${preference.category}-${preference.name}-${preference.alt_id}`;
+        return `${preference.category}-${preference.name}`;
     }
 
     getAllPreferences() {
@@ -46,12 +44,8 @@ class PreferenceStoreClass extends EventEmitter {
         return this.getAllPreferences().get(this.getKey(category, name)) || defaultValue;
     }
 
-    getPreferenceWithAltId(category, name, altId, defaultValue = '') {
-        return this.getAllPreferences().get(this.getKey(category, name, altId)) || defaultValue;
-    }
-
-    getPreferences(category, name) {
-        return this.getPreferencesWhere((preference) => (preference.category === category && preference.name === name));
+    getPreferences(category) {
+        return this.getPreferencesWhere((preference) => (preference.category === category));
     }
 
     getPreferencesWhere(pred) {
@@ -74,21 +68,16 @@ class PreferenceStoreClass extends EventEmitter {
     }
 
     setPreference(category, name, value) {
-        return this.setPreferenceWithAltId(category, name, '', value);
-    }
-
-    setPreferenceWithAltId(category, name, altId, value) {
         const preferences = this.getAllPreferences();
 
-        const key = this.getKey(category, name, altId);
+        const key = this.getKey(category, name);
         let preference = preferences.get(key);
 
         if (!preference) {
             preference = {
                 user_id: UserStore.getCurrentId(),
                 category,
-                name,
-                alt_id: altId
+                name
             };
         }
         preference.value = value;
-- 
cgit v1.2.3-1-g7c22


From 97b2f6ffe7fa09a2188163740865322582b00b59 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 13 Oct 2015 15:18:01 -0400
Subject: Made further changes based on feedback

---
 web/react/stores/preference_store.jsx | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

(limited to 'web/react/stores/preference_store.jsx')

diff --git a/web/react/stores/preference_store.jsx b/web/react/stores/preference_store.jsx
index 4d7fb9ad6..d71efa10f 100644
--- a/web/react/stores/preference_store.jsx
+++ b/web/react/stores/preference_store.jsx
@@ -9,6 +9,14 @@ const UserStore = require('../stores/user_store.jsx');
 
 const CHANGE_EVENT = 'change';
 
+function getPreferenceKey(category, name) {
+    return `${category}-${name}`;
+}
+
+function getPreferenceKeyForModel(preference) {
+    return `${preference.category}-${preference.name}`;
+}
+
 class PreferenceStoreClass extends EventEmitter {
     constructor() {
         super();
@@ -28,20 +36,12 @@ class PreferenceStoreClass extends EventEmitter {
         this.dispatchToken = AppDispatcher.register(this.handleEventPayload);
     }
 
-    getKey(category, name) {
-        return `${category}-${name}`;
-    }
-
-    getKeyForModel(preference) {
-        return `${preference.category}-${preference.name}`;
-    }
-
     getAllPreferences() {
         return new Map(BrowserStore.getItem('preferences', []));
     }
 
     getPreference(category, name, defaultValue = '') {
-        return this.getAllPreferences().get(this.getKey(category, name)) || defaultValue;
+        return this.getAllPreferences().get(getPreferenceKey(category, name)) || defaultValue;
     }
 
     getPreferences(category) {
@@ -70,7 +70,7 @@ class PreferenceStoreClass extends EventEmitter {
     setPreference(category, name, value) {
         const preferences = this.getAllPreferences();
 
-        const key = this.getKey(category, name);
+        const key = getPreferenceKey(category, name);
         let preference = preferences.get(key);
 
         if (!preference) {
@@ -109,7 +109,7 @@ class PreferenceStoreClass extends EventEmitter {
             const preferences = this.getAllPreferences();
 
             for (const preference of action.preferences) {
-                preferences.set(this.getKeyForModel(preference), preference);
+                preferences.set(getPreferenceKeyForModel(preference), preference);
             }
 
             this.setAllPreferences(preferences);
-- 
cgit v1.2.3-1-g7c22

