From eee89bf0d82822c5ee09b02734b7d029c5d07e89 Mon Sep 17 00:00:00 2001 From: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com> Date: Mon, 28 Feb 2022 21:42:50 +0530 Subject: [PATCH] feat: Show cc from last email on reply editor (#3983) * Adds last emails to reply editor * Fixes bug in reply box * Adds test cases * Prevents private notes having cc bcc data * Prevents private notes having cc bcc data * Init reply head with values * fix broken tests Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: Fayaz Ahmed <15716057+fayazara@users.noreply.github.com> Co-authored-by: Muhsin Keloth --- .../widgets/conversation/ReplyBox.vue | 20 +++++++++++-- .../widgets/conversation/ReplyEmailHead.vue | 4 +++ .../widgets/conversation/bubble/MailHead.vue | 2 +- .../store/modules/conversations/getters.js | 21 +++++++++++++ .../specs/conversations/getters.spec.js | 30 +++++++++++++++++++ 5 files changed, 74 insertions(+), 3 deletions(-) diff --git a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue index 5f17b5114..7441d9a73 100644 --- a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue +++ b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue @@ -190,6 +190,7 @@ export default { currentChat: 'getSelectedChat', messageSignature: 'getMessageSignature', currentUser: 'getCurrentUser', + lastEmail: 'getLastEmailInSelectedChat', globalConfig: 'globalConfig/get', accountId: 'getCurrentAccountId', }), @@ -388,6 +389,8 @@ export default { } else { this.replyType = REPLY_EDITOR_MODES.NOTE; } + + this.setCCEmailFromLastChat(); }, message(updatedMessage) { this.hasSlashCommand = @@ -409,6 +412,8 @@ export default { // working even if input/textarea is focussed. document.addEventListener('keydown', this.handleKeyEvents); document.addEventListener('paste', this.onPaste); + + this.setCCEmailFromLastChat(); }, destroyed() { document.removeEventListener('keydown', this.handleKeyEvents); @@ -650,11 +655,11 @@ export default { }); } - if (this.ccEmails) { + if (this.ccEmails && !this.isOnPrivateNote) { messagePayload.ccEmails = this.ccEmails; } - if (this.bccEmails) { + if (this.bccEmails && !this.isOnPrivateNote) { messagePayload.bccEmails = this.bccEmails; } @@ -667,6 +672,17 @@ export default { this.bccEmails = value.bccEmails; this.ccEmails = value.ccEmails; }, + setCCEmailFromLastChat() { + if (this.lastEmail) { + const { + content_attributes: { email: emailAttributes = {} }, + } = this.lastEmail; + const cc = emailAttributes.cc || []; + const bcc = emailAttributes.bcc || []; + this.ccEmails = cc.join(', '); + this.bccEmails = bcc.join(', '); + } + }, }, }; diff --git a/app/javascript/dashboard/components/widgets/conversation/ReplyEmailHead.vue b/app/javascript/dashboard/components/widgets/conversation/ReplyEmailHead.vue index e82fb2ecc..b2c7dd589 100644 --- a/app/javascript/dashboard/components/widgets/conversation/ReplyEmailHead.vue +++ b/app/javascript/dashboard/components/widgets/conversation/ReplyEmailHead.vue @@ -83,6 +83,10 @@ export default { } }, }, + mounted() { + this.ccEmailsVal = this.ccEmails; + this.bccEmailsVal = this.bccEmails; + }, validations: { ccEmailsVal: { hasValidEmails(value) { diff --git a/app/javascript/dashboard/components/widgets/conversation/bubble/MailHead.vue b/app/javascript/dashboard/components/widgets/conversation/bubble/MailHead.vue index 211ffeaa6..66365e85c 100644 --- a/app/javascript/dashboard/components/widgets/conversation/bubble/MailHead.vue +++ b/app/javascript/dashboard/components/widgets/conversation/bubble/MailHead.vue @@ -70,7 +70,7 @@ export default { return this.emailAttributes.subject || ''; }, showHead() { - return this.toMails || this.ccMails || this.bccMails; + return this.toMails || this.ccMails || this.bccMails || this.fromMail; }, }, }; diff --git a/app/javascript/dashboard/store/modules/conversations/getters.js b/app/javascript/dashboard/store/modules/conversations/getters.js index e4b063899..60ca0d590 100644 --- a/app/javascript/dashboard/store/modules/conversations/getters.js +++ b/app/javascript/dashboard/store/modules/conversations/getters.js @@ -1,4 +1,5 @@ import authAPI from '../../../api/auth'; +import { MESSAGE_TYPE } from 'shared/constants/messages'; import { applyPageFilters } from './helpers'; export const getSelectedChatConversation = ({ @@ -19,6 +20,26 @@ const getters = { ); return selectedChat || {}; }, + getLastEmailInSelectedChat: (stage, _getters) => { + const selectedChat = _getters.getSelectedChat; + const { messages = [] } = selectedChat; + const lastEmail = [...messages].reverse().find(message => { + const { + content_attributes: contentAttributes = {}, + message_type: messageType, + } = message; + const { email = {} } = contentAttributes; + const isIncomingOrOutgoing = + messageType === MESSAGE_TYPE.OUTGOING || + messageType === MESSAGE_TYPE.INCOMING; + if (email.from && isIncomingOrOutgoing) { + return true; + } + return false; + }); + + return lastEmail; + }, getMineChats: _state => activeFilters => { const currentUserID = authAPI.getCurrentUser().id; diff --git a/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js index bb718bb24..7dc0dade5 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/getters.spec.js @@ -131,4 +131,34 @@ describe('#getters', () => { expect(getters.getAppliedConversationFilters(state)).toEqual(filtersList); }); }); + + describe('#getLastEmailInSelectedChat', () => { + it('Returns cc in last email', () => { + const state = {}; + const getSelectedChat = { + messages: [ + { + message_type: 1, + content_attributes: { + email: { + from: 'why@how.my', + cc: ['nithin@me.co', 'we@who.why'], + }, + }, + }, + ], + }; + expect( + getters.getLastEmailInSelectedChat(state, { getSelectedChat }) + ).toEqual({ + message_type: 1, + content_attributes: { + email: { + from: 'why@how.my', + cc: ['nithin@me.co', 'we@who.why'], + }, + }, + }); + }); + }); });