diff --git a/server/server.js b/server/server.js index 170194d..10a2077 100644 --- a/server/server.js +++ b/server/server.js @@ -70,8 +70,25 @@ io.on('connection', (socket) => { }) }) - //private message - //io.to(data.room).emit('newUser', data); + // Listening for private chats + socket.on('joinPrivateRoom', ({username, room, to}) => { + console.log(`user ${username} wants to have a private chat with ${to}`); + + // Join the room + socket.join(to, () => { + + // Notify the user to talk with + io.sockets.in(room).emit('privateChat', { + username, + to + }); + }); + + // io.to(to).emit('privateChat', { + // username, + // }) + + }); }) server.listen(PORT, () => console.log(`Server Listening on port ${PORT}`)); \ No newline at end of file diff --git a/src/components/ChatDialog.vue b/src/components/ChatDialog.vue index 51cc79b..8069a9f 100644 --- a/src/components/ChatDialog.vue +++ b/src/components/ChatDialog.vue @@ -1,21 +1,27 @@ @@ -25,24 +31,61 @@ export default { name: "ChatDialog", props: { - showDialog: Boolean + showDialog: Object }, data: function() { return { + privateMessage: "" }; }, - created() {}, methods: { - closeChat(){ - this.$emit('close-chat') + closeChat() { + this.$emit("close-chat"); + }, + sendPrivateMessage() { + console.log(`${this.$store.state.username} want to send a private message to ${this.showDialog.user}` + ); + } + }, + watch: { + showDialog: function(newVal) { + if (newVal) { + // Open private chat + this.$socket.emit("joinPrivateRoom", { + ...this.$store.state, + to: this.showDialog.user + }); + } } } }; + + diff --git a/src/styles/app.scss b/src/styles/app.scss index 30df939..bc51e77 100644 --- a/src/styles/app.scss +++ b/src/styles/app.scss @@ -5,7 +5,7 @@ } .md-dialog{ - top: 81%; + top: calc(100vh - 180px); right: -135px; height: 350px; left: unset; @@ -14,6 +14,13 @@ border: 1px solid gainsboro; } +.md-dialog-actions { + padding: 0; +} +.md-dialog-content { + padding: 1rem; +} + .md-button.md-theme-default[disabled] .md-icon-font { color: rgba(0,0,0,0.38) !important; color: var(--md-theme-default-icon-disabled-on-background, rgba(0,0,0,0.38)) !important; diff --git a/src/views/Chat.vue b/src/views/Chat.vue index c428d25..0f9b1af 100644 --- a/src/views/Chat.vue +++ b/src/views/Chat.vue @@ -16,11 +16,11 @@ - - + @@ -30,10 +30,7 @@ - - + @@ -57,13 +54,32 @@ export default { this.users = data; }, newMessage: function({ message, username }) { - const isMe = this.$store.state.username === username - const msg = isMe ? ` ${message}`: `${username.toUpperCase()} - ${message} ` + const isMe = this.$store.state.username === username; + const msg = isMe + ? ` ${message}` + : `${username.toUpperCase()} - ${message} `; const msgObj = { msg, - own:isMe + own: isMe }; this.messages.push(msgObj); + }, + privateChat: function({ username, to }) { + const isMe = this.$store.state.username === to; + if (isMe && !this.openPrivateChat.chat) { + + // Join private room + this.$socket.emit("joinPrivateRoom", { + to: this.$store.state.username, + room: this.$store.state.room, + username + }); + + // open dialog + this.openPrivateChat.chat = true + this.openPrivateChat.user = username + + } } }, beforeCreate: function() { @@ -74,7 +90,10 @@ export default { room: this.$store.state.room, users: [], messages: [], - openPrivateChat: false + openPrivateChat: { + chat: false, + user: null + } }; }, methods: { @@ -92,14 +111,19 @@ export default { message: msg }); }, - openChat(user){ - this.openPrivateChat = true - console.log(user) + openChat(user) { + this.openPrivateChat = { + chat: true, + user: user + }; + console.log(user); } } }; + +