소스 검색

:sparkles: working pubnub chat gui

tags/0.0.1^2
J 3 년 전
부모
커밋
d31a4c9449
3개의 변경된 파일62개의 추가작업 그리고 29개의 파일을 삭제
  1. 5
    1
      frontend/src/router/index.js
  2. 6
    14
      frontend/src/services/chat.service.js
  3. 51
    14
      frontend/src/views/ChatView.vue

+ 5
- 1
frontend/src/router/index.js 파일 보기

34
     {
34
     {
35
         path: '/chat/:tid',
35
         path: '/chat/:tid',
36
         component: ChatView,
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
         path: `/survey`,
44
         path: `/survey`,

+ 6
- 14
frontend/src/services/chat.service.js 파일 보기

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

+ 51
- 14
frontend/src/views/ChatView.vue 파일 보기

1
 <template lang="pug">
1
 <template lang="pug">
2
-main.view--chat.f-col.start.w-full
2
+main.view--chat
3
     header 
3
     header 
4
         h2 Chat Page
4
         h2 Chat Page
5
  
5
  
7
         h3 {{ profile.id }} 
7
         h3 {{ profile.id }} 
8
             span chatting with: {{ target.profile_id }}
8
             span chatting with: {{ target.profile_id }}
9
         p subscriptions: {{ profile.chatter.subscriptions }}
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
     p(v-else-if="profile.isLoggedIn && !target") No match found between profile {{ $route.params.tid }} and {{profile.id}}... 
25
     p(v-else-if="profile.isLoggedIn && !target") No match found between profile {{ $route.params.tid }} and {{profile.id}}... 
13
     p(v-else) Loading...
26
     p(v-else) Loading...
14
 
27
 
22
     name: 'ProfileView',
35
     name: 'ProfileView',
23
     data: () => ({
36
     data: () => ({
24
         target: null,
37
         target: null,
38
+        toSend: '',
39
+        messages: [],
40
+        openDrawer: null,
25
     }),
41
     }),
26
     computed: {
42
     computed: {
27
         profile: () => currentProfile,
43
         profile: () => currentProfile,
28
     },
44
     },
29
     watch: {
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
     methods: {
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
             try {
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
             } catch (err) {
83
             } catch (err) {
47
                 console.error(err)
84
                 console.error(err)
48
             }
85
             }

Loading…
취소
저장