-
Notifications
You must be signed in to change notification settings - Fork 5
WIP - 9 - chat. #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robinlarsson
wants to merge
3
commits into
main
Choose a base branch
from
feature/9-chat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WIP - 9 - chat. #222
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| <template> | ||
| <div> | ||
| <v-card | ||
| v-if="display" | ||
| :key="componentKey" | ||
| width="256" | ||
| class="chat" | ||
| > | ||
| <v-chip-group | ||
| active-class="active-chat" | ||
| column | ||
| > | ||
| <v-chip | ||
| v-for="chat in chats" | ||
| :key="chat.chatId" | ||
| close | ||
| close-icon="mdi-close" | ||
| @click:close="leaveChat(chat.chatId)" | ||
| @click="activeChat(chat.chatId)" | ||
| > | ||
| {{ chat.chatId }} | ||
| </v-chip> | ||
| </v-chip-group> | ||
|
|
||
| <v-divider /> | ||
|
|
||
| <v-card-title> | ||
| <span class="title font-weight-light">Chat {{ currentChat.chatId }}</span> | ||
|
|
||
| <v-col class="text-right"> | ||
| <v-icon @click="toggleDisplay"> | ||
| mdi-close | ||
| </v-icon> | ||
| </v-col> | ||
| </v-card-title> | ||
|
|
||
| <v-card-text> | ||
| <div class="messages"> | ||
| <div | ||
| ref="container" | ||
| class="messages-body" | ||
| > | ||
| <div | ||
| v-for="message in currentChat.messages" | ||
| :key="message.message + message.from + message.date" | ||
| > | ||
| <span class="from">{{ message.from }}</span> | ||
| <span class="message"> | ||
| {{ message.message }} | ||
| </span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <v-textarea | ||
| v-model="newMessage" | ||
| label="Message" | ||
| rows="2" | ||
| counter | ||
| maxlength="100" | ||
| dense | ||
| full-width | ||
| autofocus | ||
| no-resize | ||
| hint="Please be respectful" | ||
| append-outer-icon="mdi-send" | ||
| @click:append-outer="sendMessage" | ||
| @keyup.enter="sendMessage" | ||
| /> | ||
| </v-card-text> | ||
| </v-card> | ||
| <v-btn | ||
| v-else | ||
| elevation="2" | ||
| fab | ||
| class="open-chat" | ||
| @click="toggleDisplay" | ||
| > | ||
| Chat | ||
| </v-btn> | ||
| </div> | ||
| </template> | ||
| <script> | ||
| export default { | ||
| name: "Chat", | ||
| props: [], | ||
| data() { | ||
| return { | ||
| display: true, | ||
| currentChat: {}, | ||
| newMessage: '', | ||
| messages: [], | ||
| componentKey: 0, | ||
| }; | ||
| }, | ||
| methods: { | ||
| // forceRerender() { | ||
| // this.componentKey += 1; | ||
| // }, | ||
| scrollToEnd () { | ||
| var content = this.$refs.container; | ||
| content.scrollTop = content.scrollHeight; | ||
| }, | ||
| toggleDisplay() { | ||
| this.display = !this.display; | ||
| }, | ||
| sendMessage() { | ||
| if (this.newMessage.trim().length > 0) { | ||
| this.$store.dispatch("chat/message", { chat: { chatId: this.currentChat.chatId, message: this.newMessage.trim() } }); | ||
| this.scrollToEnd(); | ||
| } | ||
| this.newMessage = ''; | ||
| }, | ||
| activeChat(chatId) { | ||
| this.currentChat = this.chats[chatId]; | ||
| }, | ||
| leaveChat(chatId) { | ||
| this.$store.dispatch("chat/leave", { chat: { chatId } }); | ||
| } | ||
| }, | ||
| mounted() { | ||
| this.currentChat = this.chats.server; | ||
| this.scrollToEnd(); | ||
| }, | ||
| computed: { | ||
| chats() { | ||
| return this.$store.state.chat.chats; | ||
| }, | ||
| }, | ||
| } | ||
| </script> | ||
| <style> | ||
| .open-chat { | ||
| position: fixed !important; | ||
| bottom: 33px; | ||
| right: 20px; | ||
| } | ||
| .chat { | ||
| position: fixed !important; | ||
| bottom: 33px; | ||
| right: 20px; | ||
|
|
||
| border-top-left-radius: 4px; | ||
| border-bottom-left-radius: 0; | ||
| border-top-right-radius: 4px; | ||
| border-bottom-right-radius: 0; | ||
| border-top: 1px solid black; | ||
| border-left: 1px solid black; | ||
| border-right: 1px solid black; | ||
| } | ||
|
|
||
| .chat .v-card__title { | ||
| padding: 2px; | ||
| } | ||
| .chat .v-card__text { | ||
| padding-right: 0; | ||
| padding-bottom: 0; | ||
| padding-left: 2px; | ||
| } | ||
| .chat .active-chat { | ||
| font-weight: bold; | ||
| } | ||
| .chat h3 { | ||
| font-size: 30px; | ||
| text-align: center; | ||
| } | ||
| .chat .message { | ||
| font-size: 14px; | ||
| } | ||
| .chat .from { | ||
| color: teal; | ||
| font-weight: bold; | ||
| } | ||
| .chat .messages { | ||
| display: flex; | ||
| height: 40vh; | ||
| } | ||
| .chat .messages-body { | ||
| flex-direction: column; | ||
| width: 100%; | ||
| overflow-y: scroll; | ||
| } | ||
| </style> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import Socket from "@/socket"; | ||
|
|
||
| const chatStore = { | ||
| namespaced: true, | ||
| state: { | ||
| chats: { | ||
| 'server': { | ||
| chatId: 'server', | ||
| users: [ | ||
| { "playerId": "431", "name": "RandomCat79" }, | ||
| { "playerId": "123", "name": "Zomis" } | ||
| ], | ||
| messages: [ | ||
| { message: 'Hello', from: "RandomCat79", date: Date.now() }, | ||
| { message: 'Hello there!', from: "Zomis", date: Date.now() }, | ||
| ], | ||
| }, | ||
| 'game-UR': { | ||
| chatId: 'game-UR', | ||
| users: [ | ||
| { "playerId": "431", "name": "RandomCat79" }, | ||
| { "playerId": "123", "name": "Zomis" }, | ||
| { "playerId": "444", "name": "Urmakaren" }, | ||
| ], | ||
| messages: [ | ||
| { message: 'Hello UR', from: "RandomCat79", date: Date.now() }, | ||
| { message: 'Hello you!', from: "Urmakaren", date: Date.now() }, | ||
| ], | ||
| }, | ||
| 'game-Dixit': { | ||
| chatId: 'game-Dixit', | ||
| users: [ | ||
| { "playerId": "123456", "name": "JKR" }, | ||
| { "playerId": "123", "name": "Zomis" }, | ||
| ], | ||
| messages: [ | ||
| { message: 'Hello Zomis', from: "JKR", date: Date.now() }, | ||
| { message: 'Hello you!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you2!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you3!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you4!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you5!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you6!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you7!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you8!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you9!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you10!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you11!', from: "Zomis", date: Date.now() }, | ||
| { message: 'Hello you12!', from: "Zomis", date: Date.now() }, | ||
| ], | ||
| } | ||
| } | ||
| }, | ||
| getters: { | ||
| chats: state => { return state.chats } | ||
| }, | ||
| mutations: { | ||
| addChat(state, data) { | ||
| const { chatId, users } = data; | ||
| state.chats[chatId] = { chatId, users } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to use
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I didn't see that one yet, nice! |
||
| }, | ||
| removeChat(state, data) { | ||
| const { chatId } = data; | ||
| delete state.chats[chatId]; | ||
| }, | ||
| addUserToChat(state, data) { | ||
| const { chatId, user } = data; | ||
| const chat = state.chats[chatId]; | ||
| chat.users.push(user); | ||
| }, | ||
| removeUserFromChat(state, data) { | ||
| const { chatId, user } = data; | ||
| const chat = state.chats[chatId]; | ||
| chat.users = chat.users.filter(existingUser => existingUser.playerId !== user.playerId) | ||
| }, | ||
| addMessageToChat(state, data) { | ||
| const { chatId, message, from, date } = data; | ||
| const chat = state.chats[chatId]; | ||
| chat.messages.push({ message, from, date }); | ||
| }, | ||
| }, | ||
| actions: { | ||
| join(context, data) { | ||
| const { chatId } = data.chat; | ||
| // context.commit("addChat", { chatId, users: [] }); | ||
| Socket.route(`chat/${chatId}/join`, {}); | ||
| }, | ||
| leave(context, data) { | ||
| const { chatId } = data.chat; | ||
| // context.commit("removeChat", { chatId }); | ||
| Socket.route(`chat/${chatId}/leave`, {}); | ||
| }, | ||
| message(context, data) { | ||
| const { chatId, message } = data.chat; | ||
| // context.commit("addMessageToChat", { chatId, from: 'Santa', message, date: Date.now() }); | ||
| Socket.route(`chat/${chatId}/message`, { message }); | ||
| }, | ||
| onSocketMessage(context, data) { | ||
| if (data.type === "Chat") { | ||
| if (data.chat === "join") { | ||
| const { chatId, user, users } = data; | ||
| if (user) { | ||
| context.commit("addUserToChat", { chatId, user }); | ||
| } else if (users) { | ||
| context.commit("addChat", { chatId, users }); | ||
| } | ||
| } | ||
| if (data.chat === "leave") { | ||
| context.commit("removeChat", data); | ||
| } | ||
| if (data.chat === "left") { | ||
| context.commit("removeUserFromChat", data); | ||
| } | ||
| if (data.chat === "message") { | ||
| context.commit("addMessageToChat", data); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export default chatStore; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
this.$store.stateis not correct. See for exampleInviteScreen.vuefor an example of how to usemapStateto make reactivity work correctlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!