From 4b636bfefc0bfd9dc1183c09f3d6f6ac97a2b80a Mon Sep 17 00:00:00 2001 From: enahum Date: Thu, 8 Dec 2016 13:36:28 -0300 Subject: [PATCH] Remove unnecessary moreChannels from store (#104) * Remove unnecessary moreChannels from store * Remove profile fetching on DM creation --- src/actions/channels.js | 32 ++++-------------------------- src/reducers/entities/channels.js | 33 ++----------------------------- test/actions/channels.test.js | 22 +++++++++++++++------ test/actions/users.test.js | 1 - 4 files changed, 22 insertions(+), 66 deletions(-) diff --git a/src/actions/channels.js b/src/actions/channels.js index 4d481021a..280ba137a 100644 --- a/src/actions/channels.js +++ b/src/actions/channels.js @@ -65,20 +65,9 @@ export function createChannel(channel, userId) { export function createDirectChannel(userId, otherUserId) { return async (dispatch, getState) => { try { - dispatch(batchActions([ - { - type: ChannelTypes.CREATE_CHANNEL_REQUEST - }, - { - type: ChannelTypes.CHANNEL_MEMBERS_REQUEST - }, - { - type: UsersTypes.PROFILES_REQUEST - } - ]), getState); + dispatch({type: ChannelTypes.CREATE_CHANNEL_REQUEST}, getState); const created = await Client.createDirectChannel(otherUserId); - const profile = await Client.getUser(otherUserId); const member = { channel_id: created.id, user_id: userId, @@ -95,26 +84,16 @@ export function createDirectChannel(userId, otherUserId) { type: ChannelTypes.RECEIVED_CHANNEL, data: created }, - { - type: ChannelTypes.CREATE_CHANNEL_SUCCESS - }, { type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER, data: member }, - { - type: ChannelTypes.CHANNEL_MEMBERS_SUCCESS - }, - { - type: UsersTypes.RECEIVED_PROFILES, - data: {[profile.id]: profile} - }, - { - type: UsersTypes.PROFILES_SUCCESS - }, { type: UsersTypes.RECEIVED_PREFERENCE, data: {category: Constants.CATEGORY_DIRECT_CHANNEL_SHOW, name: otherUserId, value: 'true'} + }, + { + type: ChannelTypes.CREATE_CHANNEL_SUCCESS } ]), getState); } catch (error) { @@ -127,9 +106,6 @@ export function createDirectChannel(userId, otherUserId) { { type: ChannelTypes.CHANNEL_MEMBERS_FAILURE, error - }, - { - type: UsersTypes.PROFILES_FAILURE } ]), getState); } diff --git a/src/reducers/entities/channels.js b/src/reducers/entities/channels.js index dfb6b29fd..a39d84b77 100644 --- a/src/reducers/entities/channels.js +++ b/src/reducers/entities/channels.js @@ -23,17 +23,13 @@ function channels(state = {}, action) { [action.data.id]: action.data }; - case ChannelTypes.RECEIVED_CHANNELS: { + case ChannelTypes.RECEIVED_CHANNELS: + case ChannelTypes.RECEIVED_MORE_CHANNELS: { for (const channel of action.data) { nextState[channel.id] = channel; } return nextState; } - case ChannelTypes.LEAVE_CHANNEL: - case ChannelTypes.RECEIVED_CHANNEL_DELETED: { - Reflect.deleteProperty(nextState, action.channel_id); - return nextState; - } case UsersTypes.LOGOUT_SUCCESS: return {}; @@ -89,28 +85,6 @@ function myMembers(state = {}, action) { } } -function moreChannels(state = {}, action) { - const nextState = {...state}; - - switch (action.type) { - case ChannelTypes.RECEIVED_MORE_CHANNELS: { - for (const channel of action.data) { - nextState[channel.id] = channel; - } - return nextState; - } - case ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER: { - const channelMember = action.data; - Reflect.deleteProperty(nextState, channelMember.channel_id); - return nextState; - } - case UsersTypes.LOGOUT_SUCCESS: - return {}; - default: - return state; - } -} - function stats(state = {}, action) { switch (action.type) { case ChannelTypes.RECEIVED_CHANNEL_STATS: { @@ -138,9 +112,6 @@ export default combineReducers({ //object where every key is the channel id and has and object with the channel members detail myMembers, - // object where every key is the channel id and has a object with the channel detail where the user is not a current member - moreChannels, - // object where every key is the team id and has an object with the team stats stats }); diff --git a/test/actions/channels.test.js b/test/actions/channels.test.js index 502b96852..571dfe852 100644 --- a/test/actions/channels.test.js +++ b/test/actions/channels.test.js @@ -4,6 +4,7 @@ import assert from 'assert'; import * as Actions from 'actions/channels'; +import {getProfiles} from 'actions/users'; import Client from 'client'; import configureStore from 'store/configureStore'; import {RequestStatus} from 'constants'; @@ -54,7 +55,12 @@ describe('Actions.Channels', () => { it('createDirectChannel', (done) => { TestHelper.initBasic(Client).then(async () => { const store = configureStore(); - const user = await TestHelper.basicClient.createUser(TestHelper.fakeUser()); + const user = await TestHelper.basicClient.createUserWithInvite( + TestHelper.fakeUser(), + null, + null, + TestHelper.basicTeam.invite_id + ); store.subscribe(() => { const channels = store.getState().entities.channels.channels; @@ -63,9 +69,12 @@ describe('Actions.Channels', () => { const preferences = store.getState().entities.users.myPreferences; const createRequest = store.getState().requests.channels.createChannel; - const membersRequest = store.getState().requests.channels.myMembers; - if (createRequest.status === RequestStatus.SUCCESS && membersRequest.status === RequestStatus.SUCCESS) { + if (createRequest.status === RequestStatus.SUCCESS || createRequest.status === RequestStatus.FAILURE) { + if (createRequest.error) { + done(new Error(JSON.stringify(createRequest.error))); + } + const channelsCount = Object.keys(channels).length; const membersCount = Object.keys(members).length; assert.ok(channels); @@ -80,11 +89,10 @@ describe('Actions.Channels', () => { assert.equal(channelsCount, 1); assert.equal(membersCount, 1); done(); - } else if (createRequest.status === RequestStatus.FAILURE && membersRequest.status === RequestStatus.FAILURE) { - done(new Error(JSON.stringify(createRequest.error))); } }); + await getProfiles(0)(store.dispatch, store.getState); Actions.createDirectChannel(TestHelper.basicUser.id, user.id)(store.dispatch, store.getState); }); }); @@ -382,13 +390,15 @@ describe('Actions.Channels', () => { TestHelper.fakeChannel(TestHelper.basicTeam.id)); store.subscribe(() => { - const channels = store.getState().entities.channels.moreChannels; + const channels = store.getState().entities.channels.channels; + const members = store.getState().entities.channels.myMembers; const moreRequest = store.getState().requests.channels.getMoreChannels; if (moreRequest.status === RequestStatus.SUCCESS) { const channel = channels[secondChannel.id]; assert.ok(channel); + assert.ifError(members[channel.id]); done(); } else if (moreRequest.status === RequestStatus.FAILURE) { done(new Error(JSON.stringify(moreRequest.error))); diff --git a/test/actions/users.test.js b/test/actions/users.test.js index a7b4641f5..e2da3e8aa 100644 --- a/test/actions/users.test.js +++ b/test/actions/users.test.js @@ -92,7 +92,6 @@ describe('Actions.Users', () => { assert.strictEqual(channels.currentId, '', 'current channel id is not empty'); assert.deepStrictEqual(channels.channels, {}, 'channels is not empty'); assert.deepStrictEqual(channels.myMembers, {}, 'channel members is not empty'); - assert.deepStrictEqual(channels.moreChannels, {}, 'more channels is not empty'); assert.deepStrictEqual(channels.stats, {}, 'channel stats is not empty'); assert.strictEqual(posts.selectedPostId, '', 'selected post id is not empty'); assert.strictEqual(posts.currentFocusedPostId, '', 'current focused post id is not empty');