MM-20272 Prevent channel reducers from deleting the active channel. (#990)

* Prevent channel reducers from deleting the active channel.

* Tests and simplifation.
parent 1945d829
......@@ -508,7 +508,9 @@ export function fetchMyChannelsAndMembers(teamId: string): ActionFunc {
return {error};
}
const {currentUserId} = getState().entities.users;
const state = getState();
const {currentUserId} = state.entities.users;
const {currentChannelId} = state.entities.channels;
dispatch(batchActions([
{
......@@ -516,6 +518,7 @@ export function fetchMyChannelsAndMembers(teamId: string): ActionFunc {
teamId,
data: channels,
sync: true,
currentChannelId,
},
{
type: ChannelTypes.CHANNELS_SUCCESS,
......@@ -525,6 +528,7 @@ export function fetchMyChannelsAndMembers(teamId: string): ActionFunc {
data: channelMembers,
remove: getChannelsIdForTeam(getState(), teamId),
currentUserId,
currentChannelId,
},
]), getState);
const roles = new Set<string>();
......@@ -554,13 +558,16 @@ export function getMyChannelMembers(teamId: string): ActionFunc {
return {error};
}
const {currentUserId} = getState().entities.users;
const state = getState();
const {currentUserId} = state.entities.users;
const {currentChannelId} = state.entities.channels;
dispatch({
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBERS,
data: channelMembers,
remove: getChannelsIdForTeam(getState(), teamId),
currentUserId,
currentChannelId,
});
const roles = new Set<string>();
......
......@@ -313,7 +313,6 @@ describe('channels', () => {
expect(nextState).toEqual(state);
});
});
describe('MANUALLY_UNREAD', () => {
test('should mark channel as manually unread', () => {
const state = deepFreeze({
......@@ -366,4 +365,53 @@ describe('channels', () => {
expect(nextState.channel231).toBe(undefined);
});
});
describe('RECEIVED_CHANNELS', () => {
test('should not remove current channel', () => {
const state = deepFreeze({
channelsInTeam: {},
currentChannelId: '',
groupsAssociatedToChannel: {},
myMembers: {},
stats: {},
totalCount: 0,
membersInChannel: {},
channels: {
channel1: {
id: 'channel1',
team_id: 'team',
},
channel2: {
id: 'channel2',
team_id: 'team',
},
channel3: {
id: 'channel3',
team_id: 'team',
},
},
});
const nextState = channelsReducer(state, {
type: ChannelTypes.RECEIVED_CHANNELS,
sync: true,
currentChannelId: 'channel3',
teamId: 'team',
data: [{
id: 'channel1',
team_id: 'team',
}],
});
expect(nextState).not.toBe(state);
expect(nextState.channels.channel1).toEqual({
id: 'channel1',
team_id: 'team',
});
expect(nextState.channels.channel2).toBe(undefined);
expect(nextState.channels.channel3).toEqual({
id: 'channel3',
team_id: 'team',
});
});
});
});
......@@ -15,8 +15,10 @@ function channelListToSet(state: any, action: GenericAction) {
// Remove existing channels that are no longer
if (action.sync && teamChannelIds && teamChannelIds.size) {
teamChannelIds.forEach((id: string) => {
if (!action.data.find((c: any) => c.id === id)) {
teamChannelIds.delete(id);
if (id !== action.currentChannelId) {
if (!action.data.find((c: any) => c.id === id)) {
teamChannelIds.delete(id);
}
}
});
nextState[action.teamId] = teamChannelIds;
......@@ -73,8 +75,10 @@ function channels(state: IDMappedObjects<Channel> = {}, action: GenericAction) {
currentChannels.forEach((channel) => {
if (channel.team_id === action.teamId) {
const id: string = channel.id;
if (!action.data.find((c: any) => c.id === id)) {
Reflect.deleteProperty(nextState, id);
if (id !== action.currentChannelId) {
if (!action.data.find((c: any) => c.id === id)) {
Reflect.deleteProperty(nextState, id);
}
}
}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment