Skip to content

Commit cc4f61d

Browse files
committed
fetch and display new messages
1 parent 1891262 commit cc4f61d

File tree

1 file changed

+27
-11
lines changed
  • frontend/src/components/app/matches

1 file changed

+27
-11
lines changed

frontend/src/components/app/matches/Chat.vue

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,41 @@ export default {
6464
message: '',
6565
loggedInUserId: null,
6666
newMessageCount: null,
67+
fetchMessagesIntervalId: null,
6768
}),
6869
methods: {
70+
scrollChatToBottom() {
71+
this.$nextTick(() => {
72+
const messageBox = document.getElementById('messageBox');
73+
if (messageBox) {
74+
messageBox.scrollTop = messageBox.scrollHeight;
75+
}
76+
});
77+
},
6978
closeChat() {
7079
this.$emit('close-chat');
7180
},
7281
async sendMessage() {
82+
if (!this.message.length) {
83+
return;
84+
}
7385
await this.$http.post('/messages/send', {
7486
to_uid: this.user.id.toString(),
7587
content: this.message,
7688
});
7789
this.messages.push({ to_id: this.chatWithUserId, content: this.message });
7890
this.message = '';
79-
this.$nextTick(() => {
80-
const messageBox = document.getElementById('messageBox');
81-
if (messageBox) {
82-
messageBox.scrollTop = messageBox.scrollHeight;
91+
this.scrollChatToBottom();
92+
},
93+
async fetchNewMessages() {
94+
const messagesRequest = await this.$http.get(`/conversations/${this.chatWithUserId}`);
95+
const newMessages = messagesRequest.data.messages;
96+
if (newMessages.length > this.messages.length) {
97+
for (let i = this.messages.length; i < newMessages.length; i += 1) {
98+
this.messages.push(newMessages[i]);
8399
}
84-
});
100+
this.scrollChatToBottom();
101+
}
85102
},
86103
},
87104
async beforeMount() {
@@ -90,12 +107,11 @@ export default {
90107
const userRequest = await this.$http.get(`/users/${this.chatWithUserId}`);
91108
this.user = userRequest.data;
92109
this.loggedInUserId = this.$store.getters.getLoggedInUser.id;
93-
this.$nextTick(() => {
94-
const messageBox = document.getElementById('messageBox');
95-
if (messageBox) {
96-
messageBox.scrollTop = messageBox.scrollHeight;
97-
}
98-
});
110+
this.scrollChatToBottom();
111+
this.fetchMessagesIntervalId = setInterval(this.fetchNewMessages, 1000);
112+
},
113+
beforeDestroy() {
114+
window.clearInterval(this.fetchMessagesIntervalId);
99115
},
100116
};
101117
</script>

0 commit comments

Comments
 (0)