From 1c0ee4d2f65d1d4434a3a16070abe7d61a268ce6 Mon Sep 17 00:00:00 2001
From: JoramWilander <jwawilander@gmail.com>
Date: Mon, 27 Jul 2015 11:30:03 -0400
Subject: added getChannel api service and use that over getChannels where
 appropriate on client

---
 web/react/stores/channel_store.jsx | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)

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

diff --git a/web/react/stores/channel_store.jsx b/web/react/stores/channel_store.jsx
index a97f13391..46e856a97 100644
--- a/web/react/stores/channel_store.jsx
+++ b/web/react/stores/channel_store.jsx
@@ -146,12 +146,39 @@ var ChannelStore = assign({}, EventEmitter.prototype, {
 
     return extra;
   },
+  _storeChannel: function(channel) {
+    var channels = this._getChannels();
+    var found;
+
+    for (var i = 0; i < channels.length; i++) {
+      if (channels[i].id == channel.id) {
+        channels[i] = channel;
+        found = true;
+        break;
+      }
+    }
+
+    if (!found) {
+        channels.push(channel);
+        channels.sort(function(a,b) {
+          if (a.display_name < b.display_name) return -1;
+          if (a.display_name > b.display_name) return 1;
+          return 0;
+        });
+    }
+    this._storeChannels(channels);
+  },
   _storeChannels: function(channels) {
     BrowserStore.setItem("channels", channels);
   },
   _getChannels: function() {
     return BrowserStore.getItem("channels", []);
   },
+  _storeChannelMember: function(channelMember) {
+    var members = this._getChannelMembers();
+    members[channelMember.channel_id] = channelMember;
+    this._storeChannelMembers(members);
+  },
   _storeChannelMembers: function(channelMembers) {
     BrowserStore.setItem("channel_members", channelMembers);
   },
@@ -202,6 +229,14 @@ ChannelStore.dispatchToken = AppDispatcher.register(function(payload) {
       ChannelStore.emitChange();
       break;
 
+    case ActionTypes.RECIEVED_CHANNEL:
+      ChannelStore._storeChannel(action.channel);
+      ChannelStore._storeChannelMember(action.member);
+      var currentId = ChannelStore.getCurrentId();
+      if (currentId) ChannelStore.resetCounts(currentId);
+      ChannelStore.emitChange();
+      break;
+
     case ActionTypes.RECIEVED_MORE_CHANNELS:
       ChannelStore._storeMoreChannels(action.channels);
       ChannelStore.emitMoreChange();
@@ -218,4 +253,4 @@ ChannelStore.dispatchToken = AppDispatcher.register(function(payload) {
   }
 });
 
-module.exports = ChannelStore;
\ No newline at end of file
+module.exports = ChannelStore;
-- 
cgit v1.2.3-1-g7c22

