MM-10300 Fix crash when leaving channel (#1606)

This commit is contained in:
Elias Nahum 2018-04-24 08:01:55 -03:00 committed by GitHub
parent 5385a1e5fa
commit 7db7a311f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 4 deletions

View file

@ -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);
};
}

View file

@ -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,

View file

@ -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;
}