-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloseChat.vue
135 lines (129 loc) · 3.48 KB
/
CloseChat.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<template>
<v-container>
<v-row>
<v-col cols="12">
<v-card>
<v-card-title>Multi-room Close Chat</v-card-title>
<v-card-text
>Only authenticated users can use this chat. All subscriptions are
received.
</v-card-text>
</v-card>
</v-col>
</v-row>
<v-row>
<v-col cols="12">
<v-text-field
v-model="inputMessage"
label="New Message"
outlined
clearable
append-outer-icon="mdi-send"
@click:append-outer="sendMessage"
></v-text-field>
</v-col>
</v-row>
<v-tabs
v-model="roomName"
background-color="primary"
center-active
centered
dark
>
<v-tab
v-for="(room, index) in rooms"
:key="index"
:href="'#' + room"
@click="setSubscribeByRoomName(room)"
>{{ room }}</v-tab
>
</v-tabs>
<v-card flat>
<v-tabs-items v-model="roomName">
<v-tab-item v-for="(room, index) in rooms" :key="index" :value="room">
<v-row class="pa-2">
<v-col cols="6">
<ChatList title="Input" :list="messages[room]"></ChatList>
</v-col>
<v-col cols="6">
<ChatList
title="Subscriptions"
:list="subscriptionMessages[room]"
></ChatList>
</v-col>
</v-row>
</v-tab-item>
</v-tabs-items>
</v-card>
</v-container>
</template>
<script>
import { Auth, API, graphqlOperation } from 'aws-amplify'
import { createCloseRoomChat } from '@/graphql/mutations'
import { onCreateCloseRoomChat } from '@/graphql/subscriptions'
import ChatList from '@/components/ChatList'
export default {
components: { ChatList },
data: function() {
return {
user: null,
roomName: null,
inputMessage: '',
rooms: ['room1', 'room2'],
messages: {
room1: [],
room2: [],
},
subscriptionMessages: {
room1: [],
room2: [],
},
onCreateMultiRoomChatSubscriptions: {
room1: null,
room2: null,
},
}
},
created: async function() {
this.user = await Auth.currentUserInfo()
this.setSubscribeByRoomName('room1')
},
beforeDestroy: function() {
this.clearSubscriptions()
},
methods: {
sendMessage: async function() {
const message = await API.graphql(
graphqlOperation(createCloseRoomChat, {
input: { message: this.inputMessage, roomName: this.roomName },
}),
)
console.log(message)
this.messages[this.roomName].push(message.data.createCloseRoomChat)
this.inputMessage = ''
},
setSubscribeByRoomName(roomName) {
this.clearSubscriptions()
this.onCreateMultiRoomChatSubscriptions[roomName] = API.graphql(
graphqlOperation(onCreateCloseRoomChat, { owner: this.user.username }),
).subscribe({
next: ({ provider, value }) => {
console.log({ provider, value })
this.subscriptionMessages[
value.data.onCreateCloseRoomChat.roomName
].push(value.data.onCreateCloseRoomChat)
},
})
},
clearSubscriptions() {
this.rooms.forEach(room => {
if (this.onCreateMultiRoomChatSubscriptions[room]) {
this.onCreateMultiRoomChatSubscriptions[room].unsubscribe()
}
this.onCreateMultiRoomChatSubscriptions[room] = null
})
},
},
}
</script>
<style></style>