From 6e3375fb84f62ca6993a8a41199779cc8935eac5 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 4 Oct 2023 19:33:28 +0530 Subject: [PATCH] refactor: move attachment methods to a mixin --- .../widgets/conversation/ReplyBox.vue | 24 ++----------- .../mixins/attachmentHandlerMixin.js | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 app/javascript/dashboard/mixins/attachmentHandlerMixin.js diff --git a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue index f2f78f093..fd9432f75 100644 --- a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue +++ b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue @@ -155,6 +155,7 @@ import fileUploadMixin from 'dashboard/mixins/fileUploadMixin'; import replyDraftMixin from 'dashboard/mixins/replyDraftMixin'; import audioRecordingMixin from 'dashboard/mixins/audioRecordingMixin'; import emailEditorMixin from 'dashboard/mixins/emailEditorMixin'; +import attachmentHandlerMixin from 'dashboard/mixins/attachmentHandlerMixin'; import uiSettingsMixin from 'dashboard/mixins/uiSettings'; // constants @@ -220,6 +221,7 @@ export default { replyDraftMixin, audioRecordingMixin, emailEditorMixin, + attachmentHandlerMixin, ], props: { selectedTweet: { @@ -240,7 +242,6 @@ export default { message: '', isFocused: false, showEmojiPicker: false, - attachedFiles: [], isUploading: false, replyType: REPLY_EDITOR_MODES.REPLY, mentionSearchKey: '', @@ -401,9 +402,6 @@ export default { : '(↵)'; return `${sendMessageText} ${keyLabel}`; }, - hasAttachments() { - return this.attachedFiles.length; - }, hasRecordedAudio() { return ( this.$refs.audioRecorderInput && @@ -857,24 +855,6 @@ export default { isPrivate, }); }, - attachFile({ blob, file }) { - const reader = new FileReader(); - reader.readAsDataURL(file.file); - reader.onloadend = () => { - this.attachedFiles.push({ - currentChatId: this.currentChat.id, - resource: blob || file, - isPrivate: this.isPrivate, - thumb: reader.result, - blobSignedId: blob ? blob.signed_id : undefined, - }); - }; - }, - removeAttachment(itemIndex) { - this.attachedFiles = this.attachedFiles.filter( - (item, index) => itemIndex !== index - ); - }, getMessagePayloadForWhatsapp(message) { const multipleMessagePayload = []; diff --git a/app/javascript/dashboard/mixins/attachmentHandlerMixin.js b/app/javascript/dashboard/mixins/attachmentHandlerMixin.js new file mode 100644 index 000000000..a1d182fc4 --- /dev/null +++ b/app/javascript/dashboard/mixins/attachmentHandlerMixin.js @@ -0,0 +1,35 @@ +export default { + data() { + return { + attachedFiles: [], + }; + }, + computed: { + hasAttachments() { + return this.attachedFiles.length; + }, + }, + methods: { + attachFile({ blob, file }) { + const reader = new FileReader(); + reader.readAsDataURL(file.file); + reader.onloadend = () => { + this.attachedFiles.push({ + currentChatId: this.currentChat.id, + resource: blob || file, + isPrivate: this.isPrivate, + thumb: reader.result, + blobSignedId: blob ? blob.signed_id : undefined, + }); + }; + }, + removeAttachment(itemIndex) { + this.attachedFiles = this.attachedFiles.filter( + (item, index) => itemIndex !== index + ); + }, + clearAttachments() { + this.attachedFiles = []; + }, + }, +};