Przeglądaj źródła

:sparkles: working pubnub chat gui

tags/0.0.1^2
J 3 lat temu
rodzic
commit
d31a4c9449

+ 5
- 1
frontend/src/router/index.js Wyświetl plik

@@ -34,7 +34,11 @@ const routes = [
34 34
     {
35 35
         path: '/chat/:tid',
36 36
         component: ChatView,
37
-        meta: { requiresAuth: true, requiresCompleteProfile: true },
37
+        meta: {
38
+            requiresAuth: true,
39
+            requiresCompleteProfile: true,
40
+            props: true,
41
+        },
38 42
     },
39 43
     {
40 44
         path: `/survey`,

+ 6
- 14
frontend/src/services/chat.service.js Wyświetl plik

@@ -64,30 +64,23 @@ class Chatter {
64 64
         // Setup the main channel
65 65
         //  subscriptions array will be built dynamically from the "this.groupings" object
66 66
         this.subscriptions = [MAIN_CHANNEL]
67
+
67 68
         this.listeners = {
68 69
             status: async e => {
69 70
                 // await this.publish(this.subscriptions[0], testMessage)
70 71
                 if (e.category !== 'PNConnectedCategory') return
71 72
             },
72
-            message: this._onMessage,
73
+            message: null, // Set manually in chat view
73 74
             presence: this._onPresence,
74 75
         }
75 76
     }
76
-    /**
77
-     * Callback that fires on every message
78
-     * @param {event} e
79
-     */
80
-    async _onMessage(e) {
81
-        if (e.message) {
82
-            console.log(
83
-                `received message: ${e.message.title} - ${e.message.description}`,
84
-                e,
85
-            )
86
-        }
87
-    }
88 77
     async _onPresence(e) {
89 78
         return
90 79
     }
80
+    setOnMessage(cb) {
81
+        this.listeners.message = cb
82
+    }
83
+
91 84
     async setup(uuid, groupings) {
92 85
         this.uuid = `${uuid}`
93 86
         this.provider = await setupPubnub(this.uuid)
@@ -129,7 +122,6 @@ class Chatter {
129 122
     //  step 2: build the this.subscriptions array from the this.groupings object
130 123
     // fetch all groupings for this profile and then store them in the chatter groupings object for reference
131 124
     async _setupAllChannels(groupings) {
132
-        console.log(`fetched groupings for profileId: ${this.uusid}`, groupings)
133 125
         groupings.forEach(grouping => {
134 126
             this.subscriptions.push(grouping.grouping_name)
135 127
         })

+ 51
- 14
frontend/src/views/ChatView.vue Wyświetl plik

@@ -1,5 +1,5 @@
1 1
 <template lang="pug">
2
-main.view--chat.f-col.start.w-full
2
+main.view--chat
3 3
     header 
4 4
         h2 Chat Page
5 5
  
@@ -7,8 +7,21 @@ main.view--chat.f-col.start.w-full
7 7
         h3 {{ profile.id }} 
8 8
             span chatting with: {{ target.profile_id }}
9 9
         p subscriptions: {{ profile.chatter.subscriptions }}
10
-        p target: {{ target }}
11
-    
10
+
11
+        ul(id="messages").w-flex.column
12
+            li(v-for="message, i in messages" class="pa1")
13
+                w-card.w-flex.row
14
+                    article
15
+                        p {{ message.message }}
16
+                    footer
17
+                        p {{ message.publisher }} | {{ message.timetoken }}
18
+                        w-button(class="my12" @click="openDrawer = i" text) setting
19
+                    w-drawer(v-model="openDrawer" absolute width="160px")
20
+                        p some settings things
21
+
22
+        input(v-model="toSend" @keyup.enter="sendMessage")
23
+        button(@click="sendMessage") send
24
+
12 25
     p(v-else-if="profile.isLoggedIn && !target") No match found between profile {{ $route.params.tid }} and {{profile.id}}... 
13 26
     p(v-else) Loading...
14 27
 
@@ -22,27 +35,51 @@ export default {
22 35
     name: 'ProfileView',
23 36
     data: () => ({
24 37
         target: null,
38
+        toSend: '',
39
+        messages: [],
40
+        openDrawer: null,
25 41
     }),
26 42
     computed: {
27 43
         profile: () => currentProfile,
28 44
     },
29 45
     watch: {
30
-        async profile() {
31
-            await this.loadTargetProfile(this.$route.params.tid)
32
-            console.log('target :>> ', this.$route.params.tid)
46
+        profile() {
47
+            this.loadTargetProfile()
48
+            currentProfile.chatter.setOnMessage(this._onMessage)
33 49
         },
34 50
     },
35
-    async created() {
36
-        await this.loadTargetProfile()
51
+    created() {
52
+        this.loadTargetProfile()
53
+        currentProfile.chatter.setOnMessage(this._onMessage)
37 54
     },
38 55
     methods: {
39
-        async loadTargetProfile() {
56
+        /**
57
+         * Pubnub message callback fires when message event
58
+         * is detected. We define is HERE because we need the
59
+         * component state and `this` context
60
+         */
61
+        _onMessage(e) {
62
+            if (!e.message) return
63
+            this.messages.push(e)
64
+        },
65
+        getGrouping() {
66
+            return currentProfile.groupings.find(
67
+                g => g.profile.profile_id == this.$route.params.tid,
68
+            )
69
+        },
70
+        sendMessage() {
71
+            const grouping = this.getGrouping()
72
+            currentProfile.chatter.publish(grouping.grouping_name, this.toSend)
73
+            this.toSend = ''
74
+        },
75
+        loadTargetProfile() {
40 76
             try {
41
-                const match = this.profile.groupings.find(
42
-                    grouping =>
43
-                        grouping.profile.profile_id == this.$route.params.tid,
44
-                )
45
-                this.target = match.profile
77
+                const grouping = this.getGrouping()
78
+                if (!grouping) {
79
+                    throw `no match found for ${currentProfile.id.value}`
80
+                }
81
+
82
+                this.target = grouping.profile
46 83
             } catch (err) {
47 84
                 console.error(err)
48 85
             }

Ładowanie…
Anuluj
Zapisz