From 2bc968508b6c58285ff6a5bf138703672bfc4bda Mon Sep 17 00:00:00 2001 From: saschrad <saschrad@stud.ntnu.no> Date: Wed, 4 May 2022 21:49:35 +0200 Subject: [PATCH] Added auto message update --- .../ChatComponents/ChatComponent.vue | 37 +++++++++++-------- .../ChatComponents/ChatsComponent.vue | 2 +- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/components/ChatComponents/ChatComponent.vue b/src/components/ChatComponents/ChatComponent.vue index 8f9ea97..6ddbaa3 100644 --- a/src/components/ChatComponents/ChatComponent.vue +++ b/src/components/ChatComponents/ChatComponent.vue @@ -108,6 +108,20 @@ export default { let container = this.$el.querySelector(".conversation"); container.scrollTop = container.scrollHeight; }, + async reloadMessages() { + const token = this.$store.state.user.token; + const response = await fetch( + `${process.env.VUE_APP_BASEURL}chats/users/${this.recipientID}/messages`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); + this.msgs = await response.json(); + }, async sendMessage() { if (!this.recipient || this.message == null || this.message == "") return; this.canScroll = true; @@ -129,21 +143,7 @@ export default { sender: parseInt(this.userid), recipient: this.recipientID, }); - this.reloadMessages(); - }, - async reloadMessages() { - const token = this.$store.state.user.token; - const response = await fetch( - `${process.env.VUE_APP_BASEURL}chats/users/${this.recipientID}/messages`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${token}`, - }, - } - ); - this.msgs = await response.json(); + await this.reloadMessages(); }, async reloadRents() { const token = this.$store.state.user.token; @@ -188,12 +188,19 @@ export default { await this.reloadMessages(); await this.getRecipient(); await this.reloadRents(); + + ws.on("NEW_MESSAGE", () => { + this.reloadMessages(); + }, "chat"); }, updated() { if (this.canScroll) this.scroll(); this.canScroll = false; this.scrollBehavior = "smooth"; }, + unmounted() { + ws.end("NEW_MESSAGE", "chat"); + } }; </script> diff --git a/src/components/ChatComponents/ChatsComponent.vue b/src/components/ChatComponents/ChatsComponent.vue index 9bca728..0f08d74 100644 --- a/src/components/ChatComponents/ChatsComponent.vue +++ b/src/components/ChatComponents/ChatsComponent.vue @@ -89,7 +89,7 @@ export default { this.conversations = await conResponse.json(); ws.on("NEW_MESSAGE", () => { - this.reloadMessages(); + //this.reloadMessages(); }, "chats"); this.recipientID = (this.$route.query?.userID || null) if(!this.recipientID) this.hambugerDisplay = "block"; -- GitLab