From 6c4c706313eb765eb00c639f381646be74f27b69 Mon Sep 17 00:00:00 2001
From: Joram Wilander <jwawilander@gmail.com>
Date: Tue, 25 Apr 2017 11:46:02 -0400
Subject: Start moving webapp to Redux (#6140)

* Start moving webapp to Redux

* Fix localforage import

* Updates per feedback

* Feedback udpates and a few fixes

* Minor updates

* Fix statuses, config not loading properly, getMe sanitizing too much

* Fix preferences

* Fix user autocomplete

* Fix sessions and audits

* Fix error handling for all redux actions

* Use new directory structure for components and containers

* Refresh immediately on logout instead of after timeout

* Add fetch polyfill
---
 webapp/stores/preference_store.jsx | 42 ++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 9 deletions(-)

(limited to 'webapp/stores/preference_store.jsx')

diff --git a/webapp/stores/preference_store.jsx b/webapp/stores/preference_store.jsx
index 7a286f7a2..f3476d9ea 100644
--- a/webapp/stores/preference_store.jsx
+++ b/webapp/stores/preference_store.jsx
@@ -8,6 +8,10 @@ import EventEmitter from 'events';
 
 const CHANGE_EVENT = 'change';
 
+import store from 'stores/redux_store.jsx';
+import * as Selectors from 'mattermost-redux/selectors/entities/preferences';
+import {PreferenceTypes} from 'mattermost-redux/action_types';
+
 class PreferenceStore extends EventEmitter {
     constructor() {
         super();
@@ -16,6 +20,23 @@ class PreferenceStore extends EventEmitter {
         this.dispatchToken = AppDispatcher.register(this.handleEventPayload);
 
         this.preferences = new Map();
+        this.entities = Selectors.getMyPreferences(store.getState());
+        Object.keys(this.entities).forEach((key) => {
+            this.preferences.set(key, this.entities[key].value);
+        });
+
+        store.subscribe(() => {
+            const newEntities = Selectors.getMyPreferences(store.getState());
+            if (this.entities !== newEntities) {
+                this.preferences = new Map();
+                Object.keys(newEntities).forEach((key) => {
+                    this.preferences.set(key, newEntities[key].value);
+                });
+                this.emitChange();
+            }
+
+            this.entities = newEntities;
+        });
 
         this.setMaxListeners(600);
     }
@@ -79,21 +100,24 @@ class PreferenceStore extends EventEmitter {
     }
 
     setPreference(category, name, value) {
-        this.preferences.set(this.getKey(category, name), value);
+        store.dispatch({
+            type: PreferenceTypes.RECEIVED_PREFERENCES,
+            data: [{category, name, value}]
+        });
     }
 
     setPreferencesFromServer(newPreferences) {
-        for (const preference of newPreferences) {
-            this.setPreference(preference.category, preference.name, preference.value);
-        }
+        store.dispatch({
+            type: PreferenceTypes.RECEIVED_PREFERENCES,
+            data: newPreferences
+        });
     }
 
     deletePreference(preference) {
-        this.preferences.delete(this.getKey(preference.category, preference.name));
-    }
-
-    clear() {
-        this.preferences.clear();
+        store.dispatch({
+            type: PreferenceTypes.DELETED_PREFERENCES,
+            data: [preference]
+        });
     }
 
     emitChange(category) {
-- 
cgit v1.2.3-1-g7c22

