From b79b2b2a53935cd883312c1158918291a926bacd Mon Sep 17 00:00:00 2001
From: enahum <nahumhbl@gmail.com>
Date: Thu, 22 Dec 2016 15:20:36 -0300
Subject: Fix team switch (#4851)

* Fix team switch

* Fix channel switching by using BrowserStore

* Fixes plt-5076 and plt-5068

* Updating styles for Teams sidebar (#4875)
---
 webapp/actions/global_actions.jsx | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

(limited to 'webapp/actions/global_actions.jsx')

diff --git a/webapp/actions/global_actions.jsx b/webapp/actions/global_actions.jsx
index e1009e9c2..d3fc2cd29 100644
--- a/webapp/actions/global_actions.jsx
+++ b/webapp/actions/global_actions.jsx
@@ -53,6 +53,8 @@ export function emitChannelClickEvent(channel) {
             trackPage();
         });
 
+        BrowserStore.setGlobalItem(chan.team_id, chan.id);
+
         AppDispatcher.handleViewAction({
             type: ActionTypes.CLICK_CHANNEL,
             name: chan.name,
@@ -542,7 +544,11 @@ export function emitBrowserFocus(focus) {
 export function redirectUserToDefaultTeam() {
     const teams = TeamStore.getAll();
     const teamMembers = TeamStore.getMyTeamMembers();
-    let teamId = PreferenceStore.get('last', 'team');
+    let teamId = BrowserStore.getGlobalItem('team');
+
+    function redirect(teamName, channelName) {
+        browserHistory.push(`/${teamName}/channels/${channelName}`);
+    }
 
     if (!teams[teamId] && teamMembers.length > 0) {
         let myTeams = [];
@@ -560,12 +566,24 @@ export function redirectUserToDefaultTeam() {
     }
 
     if (teams[teamId]) {
-        const channelId = PreferenceStore.get(teamId, 'channel');
-        let channel = ChannelStore.getChannelById(channelId);
-        if (!channel) {
-            channel = 'town-square';
+        const channelId = BrowserStore.getGlobalItem(teamId);
+        const channel = ChannelStore.getChannelById(channelId);
+        if (channel) {
+            redirect(teams[teamId].name, channel);
+        } else if (channelId) {
+            Client.setTeamId(teamId);
+            Client.getChannel(
+                channelId,
+                (data) => {
+                    redirect(teams[teamId].name, data.channel.name);
+                },
+                () => {
+                    redirect(teams[teamId].name, 'town-square');
+                }
+            );
+        } else {
+            redirect(teams[teamId].name, 'town-square');
         }
-        browserHistory.push(`/${teams[teamId].name}/channels/${channel}`);
     } else {
         browserHistory.push('/select_team');
     }
-- 
cgit v1.2.3-1-g7c22

