From 7db7a311f16a58e56fd798f63e480c59b91f01b8 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Tue, 24 Apr 2018 08:01:55 -0300 Subject: [PATCH] MM-10300 Fix crash when leaving channel (#1606) --- app/actions/views/channel.js | 20 ++++++++++++++++---- app/constants/view.js | 1 + app/reducers/views/team.js | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/actions/views/channel.js b/app/actions/views/channel.js index b6adc19c3..20238c9ee 100644 --- a/app/actions/views/channel.js +++ b/app/actions/views/channel.js @@ -400,11 +400,23 @@ export function refreshChannelWithRetry(channelId) { export function leaveChannel(channel, reset = false) { return async (dispatch, getState) => { - const {currentTeamId} = getState().entities.teams; - await serviceLeaveChannel(channel.id)(dispatch, getState); - if (channel.isCurrent || reset) { - await selectInitialChannel(currentTeamId)(dispatch, getState); + const state = getState(); + const {currentChannelId} = state.entities.channels; + const {currentTeamId} = state.entities.teams; + + dispatch({ + type: ViewTypes.REMOVE_LAST_CHANNEL_FOR_TEAM, + data: { + teamId: currentTeamId, + channelId: channel.id, + }, + }); + + if (channel.id === currentChannelId || reset) { + await dispatch(selectInitialChannel(currentTeamId)); } + + await serviceLeaveChannel(channel.id)(dispatch, getState); }; } diff --git a/app/constants/view.js b/app/constants/view.js index 471e823b1..5f53b7a36 100644 --- a/app/constants/view.js +++ b/app/constants/view.js @@ -44,6 +44,7 @@ const ViewTypes = keyMirror({ SET_CHANNEL_DISPLAY_NAME: null, SET_LAST_CHANNEL_FOR_TEAM: null, + REMOVE_LAST_CHANNEL_FOR_TEAM: null, GITLAB: null, SAML: null, diff --git a/app/reducers/views/team.js b/app/reducers/views/team.js index 47f0ff97a..1ddfdf415 100644 --- a/app/reducers/views/team.js +++ b/app/reducers/views/team.js @@ -44,6 +44,29 @@ function lastChannelForTeam(state = {}, action) { [action.teamId]: channelIds, }; } + case ViewTypes.REMOVE_LAST_CHANNEL_FOR_TEAM: { + const {data} = action; + const team = state[data.teamId]; + + if (!data.channelId) { + return state; + } + + if (team) { + const channelIds = [...team]; + const index = channelIds.indexOf(data.channelId); + if (index !== -1) { + channelIds.splice(index, 1); + } + + return { + ...state, + [data.teamId]: channelIds, + }; + } + + return state; + } default: return state; }