From 1108446974e21fab553fe9ecfec342005807abfb Mon Sep 17 00:00:00 2001 From: Bruno Almeida Date: Thu, 14 May 2020 10:13:02 +0200 Subject: [PATCH] =?UTF-8?q?Feature:=20Move=20to=20the=20next=20conversatio?= =?UTF-8?q?n=20when=20I=20resolve=20a=20the=20current=20c=E2=80=A6=20(#757?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Feature: Move to the next conversation when I resolve a the current conversation * check if nextId is present before emitting the event * use es6 string literals * use a named variable for better reading * create a variable for better readability * better sintax to get clickable element * after last, go to first chat when resolve * use state and action to set next chat * remove not used emit * clear selected state when there is not next chat * Remove deprecated scope from FB Channel (#761) Remove deprecated scope from FB Channel * Feature: Customise the position of messenger (#767) Co-authored-by: Nithin David Thomas * Bug: Redirect user to set new password screen (#772) * auto linter * fix js linter * sort chats on getter / filter before getting next chat * Revert not related changes on ConversationCard.vue * add test for getNextChatConversation getter * remove not used module * add test for getAllConversations getter --- .../dashboard/components/ChatList.vue | 6 +- .../store/modules/conversations/actions.js | 8 +- .../store/modules/conversations/getters.js | 16 +++- .../specs/conversations/getters.spec.js | 83 +++++++++++++++++++ 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js diff --git a/app/javascript/dashboard/components/ChatList.vue b/app/javascript/dashboard/components/ChatList.vue index c0ddb1987..59012d516 100644 --- a/app/javascript/dashboard/components/ChatList.vue +++ b/app/javascript/dashboard/components/ChatList.vue @@ -157,11 +157,7 @@ export default { } else { copyList = this.allChatList.slice(); } - const sorted = copyList.sort( - (a, b) => - this.lastMessage(b).created_at - this.lastMessage(a).created_at - ); - return sorted; + return copyList; }, }, }; diff --git a/app/javascript/dashboard/store/modules/conversations/actions.js b/app/javascript/dashboard/store/modules/conversations/actions.js index 275be8112..b06312e0e 100644 --- a/app/javascript/dashboard/store/modules/conversations/actions.js +++ b/app/javascript/dashboard/store/modules/conversations/actions.js @@ -120,13 +120,19 @@ const actions = { } }, - toggleStatus: async ({ commit }, data) => { + toggleStatus: async ({ commit, dispatch, getters }, data) => { try { + const nextChat = getters.getNextChatConversation; const response = await ConversationApi.toggleStatus(data); commit( types.default.RESOLVE_CONVERSATION, response.data.payload.current_status ); + if (nextChat) { + dispatch('setActiveChat', nextChat); + } else { + dispatch('clearSelectedState'); + } } catch (error) { // Handle error } diff --git a/app/javascript/dashboard/store/modules/conversations/getters.js b/app/javascript/dashboard/store/modules/conversations/getters.js index c3431333a..0df2b4036 100644 --- a/app/javascript/dashboard/store/modules/conversations/getters.js +++ b/app/javascript/dashboard/store/modules/conversations/getters.js @@ -8,7 +8,9 @@ export const getSelectedChatConversation = ({ // getters const getters = { - getAllConversations: ({ allConversations }) => allConversations, + getAllConversations: ({ allConversations }) => allConversations.sort( + (a, b) => b.messages.last().created_at - a.messages.last().created_at + ), getSelectedChat: ({ selectedChat }) => selectedChat, getMineChats(_state) { const currentUserID = authAPI.getCurrentUser().id; @@ -50,6 +52,18 @@ const getters = { getChatStatusFilter: ({ chatStatusFilter }) => chatStatusFilter, getSelectedInbox: ({ currentInbox }) => currentInbox, getConvTabStats: ({ convTabStats }) => convTabStats, + getNextChatConversation: (_state) => { + const { selectedChat } = _state; + const conversations = getters.getAllStatusChats(_state); + if (conversations.length <= 1) { + return null; + }; + const currentIndex = conversations.findIndex( + conversation => conversation.id === selectedChat.id + ); + const nextIndex = (currentIndex + 1) % conversations.length; + return conversations[nextIndex]; + }, }; export default getters; diff --git a/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js new file mode 100644 index 000000000..a0437b337 --- /dev/null +++ b/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js @@ -0,0 +1,83 @@ +import commonHelpers from '../../../../helper/commons'; +import getters from '../../conversations/getters'; + +// loads .last() helper +commonHelpers(); + +describe('#getters', () => { + describe('#getAllConversations', () => { + it('order conversations based on last message date', () => { + const state = { + allConversations: [ + { + id: 1, + messages: [ + { + created_at: 1466424480, + }, + ], + }, + { + id: 2, + messages: [ + { + created_at: 2466424490, + }, + ], + }, + ], + }; + expect(getters.getAllConversations(state)).toEqual([ + { + id: 2, + messages: [ + { + created_at: 2466424490, + }, + ], + }, + { + id: 1, + messages: [ + { + created_at: 1466424480, + }, + ], + }, + ]); + }); + }); + describe('#getNextChatConversation', () => { + it('return the next chat', () => { + const state = { + allConversations: [ + { + id: 1, + }, + { + id: 2, + }, + ], + selectedChat: { + id: 1, + }, + }; + expect(getters.getNextChatConversation(state)).toEqual({ + id: 2, + }); + }); + it('return null when there is only one chat', () => { + const state = { + allConversations: [ + { + id: 1, + }, + ], + selectedChat: { + id: 1, + }, + }; + expect(getters.getNextChatConversation(state)).toBeNull(); + }); + }); +});