Przeglądaj źródła

Merge branch 'refactor-login-for-toast' of fyindr/siimee into dev

tags/0.0.1^2
maeda 3 lat temu
rodzic
commit
0aec6ab009

+ 1
- 0
backend/lib/routes/notification/index.js Wyświetl plik

@@ -36,6 +36,7 @@ module.exports = {
36 36
                 name: 'BDGRS',
37 37
                 price: (500 + Math.floor(Math.random() * 100)).toString(),
38 38
                 order: null,
39
+                type: 'info',
39 40
             }
40 41
 
41 42
             // Write to the input stream

+ 14
- 21
frontend/src/App.vue Wyświetl plik

@@ -43,7 +43,7 @@ w-app
43 43
 import 'wave-ui/dist/wave-ui.css'
44 44
 import SideBar from './components/SideBar.vue'
45 45
 
46
-import { Chatter, currentProfile, StonkAlert } from './services'
46
+import { currentProfile } from './services'
47 47
 import { surveyFactory } from './utils'
48 48
 
49 49
 const DEV_MODE = import.meta.env.VITE_DEV == 'true'
@@ -71,33 +71,26 @@ export default {
71 71
         if (DEV_MODE) {
72 72
             this.setPid(DEV_PID)
73 73
         }
74
-
75
-        if (currentProfile.isLoggedIn) {
76
-            console.warn(`setting up Chatter and Toaster for ${this.getPid}...`)
77
-            this.setupChatter()
78
-            this.setupToaster()
79
-        }
80
-        console.log('---')
81 74
     },
82 75
     methods: {
83 76
         /**
84 77
          * Sync up this components state with
85 78
          * the currentProfile handler
86 79
          */
87
-        setPid(profileId) {
88
-            currentProfile.login(profileId)
89
-        },
80
+        async setPid(profileId) {
81
+            if (currentProfile.isLoggedIn) {
82
+                currentProfile.logout()
83
+            }
84
+            await currentProfile.login(profileId)
90 85
 
91
-        /**
92
-         * For push notifications and chat
93
-         */
94
-        setupToaster() {
95
-            const t = new StonkAlert(this.getPid)
96
-        },
97
-        setupChatter() {
98
-            const c = new Chatter()
99
-            const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID
100
-            c.setup(testAccountUUID)
86
+            if (currentProfile.isLoggedIn) {
87
+                console.warn(
88
+                    `setting up Chatter and Toaster for ${this.getPid}...`,
89
+                )
90
+                currentProfile.setupChatter()
91
+                currentProfile.setupToaster(this.$waveui.notify)
92
+            }
93
+            console.log('---')
101 94
         },
102 95
     },
103 96
 }

+ 39
- 10
frontend/src/services/login.service.js Wyświetl plik

@@ -1,5 +1,5 @@
1 1
 import { ref } from 'vue'
2
-import { fetchResponsesByProfileId } from '../services'
2
+import { fetchResponsesByProfileId, Chatter, StonkAlert } from '../services'
3 3
 import { surveyFactory } from '../utils'
4 4
 
5 5
 /**
@@ -9,12 +9,15 @@ import { surveyFactory } from '../utils'
9 9
 class Login {
10 10
     constructor() {
11 11
         this._loading = false
12
-        
12
+
13 13
         // Make reactive with vue observer
14 14
         this.id = ref(null)
15 15
 
16 16
         this.responses = []
17 17
         this.tags = []
18
+
19
+        this.toaster = null
20
+        this.chatter = null
18 21
     }
19 22
     get isLoading() {
20 23
         return this._loading
@@ -30,12 +33,15 @@ class Login {
30 33
     }
31 34
     /**
32 35
      * Combine questions retrieved from the database and
33
-     * questions defined in out lang file and 
36
+     * questions defined in out lang file and
34 37
      * copare to responses stored
35 38
      * @returns {boolean}
36 39
      */
37 40
     get isComplete() {
38
-        return this.responses.length == surveyFactory.questionsFromDb.length && surveyFactory.questionsFromDb.length > 0
41
+        return (
42
+            this.responses.length == surveyFactory.questionsFromDb.length &&
43
+            surveyFactory.questionsFromDb.length > 0
44
+        )
39 45
     }
40 46
     /**
41 47
      * Check that some responses are set
@@ -43,20 +49,28 @@ class Login {
43 49
      */
44 50
     get hasResponses() {
45 51
         return this.responses.length && this.responses.length > 0
46
-    } 
52
+    }
47 53
 
48 54
     /**
49 55
      * Login a profile by id number
50 56
      * @param {number} profileId
51 57
      * @returns {number} stored reactive id
52 58
      */
53
-     async login(profileId) {
59
+    async login(profileId) {
54 60
         console.warn('logging in:', profileId)
55 61
         this.id.value = parseInt(profileId)
56 62
         return this.id.value
57 63
     }
58
-    logout() { this.id.value = null }
59
-    
64
+    logout() {
65
+        this.id.value = null
66
+        if (this.toaster) {
67
+            this.toaster.stop()
68
+        }
69
+        if (this.chatter) {
70
+            this.toaster.stop()
71
+        }
72
+    }
73
+
60 74
     async getTags() {
61 75
         try {
62 76
             const tags = []
@@ -65,7 +79,9 @@ class Login {
65 79
             console.error(err)
66 80
         }
67 81
     }
68
-    setTags(tags) { this.tags = tags }
82
+    setTags(tags) {
83
+        this.tags = tags
84
+    }
69 85
 
70 86
     async getResponses() {
71 87
         try {
@@ -75,7 +91,20 @@ class Login {
75 91
             console.error(err)
76 92
         }
77 93
     }
78
-    setResponses(responses) { this.responses = responses }
94
+    setResponses(responses) {
95
+        this.responses = responses
96
+    }
97
+    /**
98
+     * For push notifications and chat
99
+     */
100
+    setupToaster(waveCb) {
101
+        this.toaster = new StonkAlert(this.id.value, waveCb)
102
+    }
103
+    setupChatter() {
104
+        this.chatter = new Chatter()
105
+        const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID
106
+        this.chatter.setup(testAccountUUID)
107
+    }
79 108
 }
80 109
 
81 110
 const currentProfile = new Login()

+ 11
- 5
frontend/src/services/notification.service.js Wyświetl plik

@@ -14,22 +14,28 @@ class Toaster {
14 14
     listenFor(event, callback) {
15 15
         this.source.addEventListener(event, callback)
16 16
     }
17
+    stop() {
18
+        this.source.close()
19
+    }
17 20
 }
18 21
 
19 22
 /**
20 23
  * Example extension that listens for 'stonk' events
21 24
  */
22 25
 class StonkAlert extends Toaster {
23
-    constructor(profileId) {
26
+    constructor(profileId, waveCb) {
24 27
         super(profileId)
25
-
28
+        this.event = 'stonk'
26 29
         this.stonks = {}
27
-        this.listenFor(`${profileId}.stonk`, message => {
30
+        this.listenFor(`${profileId}.${this.event}`, message => {
28 31
             const parsed = JSON.parse(message.data)
29 32
             this.stonks[parsed.name] = parsed
30
-            console.log('updated:', this.stonks)
33
+            waveCb(this._formatToast(parsed), parsed.type)
31 34
         })
32 35
     }
36
+    _formatToast(parsed) {
37
+        return `${parsed.name}: ${parsed.profile_id} ${parsed.order} at ${parsed.price}`
38
+    }
33 39
 }
34 40
 
35
-export { Toaster, StonkAlert }
41
+export { Toaster, StonkAlert }

Ładowanie…
Anuluj
Zapisz