Просмотр исходного кода

:recycle: moved chatter and toaster to login | gracefully handle toast when switching profile

tags/0.0.1^2
J 3 лет назад
Родитель
Сommit
7a95e770c7

+ 1
- 0
backend/lib/routes/notification/index.js Просмотреть файл

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

+ 14
- 21
frontend/src/App.vue Просмотреть файл

43
 import 'wave-ui/dist/wave-ui.css'
43
 import 'wave-ui/dist/wave-ui.css'
44
 import SideBar from './components/SideBar.vue'
44
 import SideBar from './components/SideBar.vue'
45
 
45
 
46
-import { Chatter, currentProfile, StonkAlert } from './services'
46
+import { currentProfile } from './services'
47
 import { surveyFactory } from './utils'
47
 import { surveyFactory } from './utils'
48
 
48
 
49
 const DEV_MODE = import.meta.env.VITE_DEV == 'true'
49
 const DEV_MODE = import.meta.env.VITE_DEV == 'true'
71
         if (DEV_MODE) {
71
         if (DEV_MODE) {
72
             this.setPid(DEV_PID)
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
     methods: {
75
     methods: {
83
         /**
76
         /**
84
          * Sync up this components state with
77
          * Sync up this components state with
85
          * the currentProfile handler
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 Просмотреть файл

1
 import { ref } from 'vue'
1
 import { ref } from 'vue'
2
-import { fetchResponsesByProfileId } from '../services'
2
+import { fetchResponsesByProfileId, Chatter, StonkAlert } from '../services'
3
 import { surveyFactory } from '../utils'
3
 import { surveyFactory } from '../utils'
4
 
4
 
5
 /**
5
 /**
9
 class Login {
9
 class Login {
10
     constructor() {
10
     constructor() {
11
         this._loading = false
11
         this._loading = false
12
-        
12
+
13
         // Make reactive with vue observer
13
         // Make reactive with vue observer
14
         this.id = ref(null)
14
         this.id = ref(null)
15
 
15
 
16
         this.responses = []
16
         this.responses = []
17
         this.tags = []
17
         this.tags = []
18
+
19
+        this.toaster = null
20
+        this.chatter = null
18
     }
21
     }
19
     get isLoading() {
22
     get isLoading() {
20
         return this._loading
23
         return this._loading
30
     }
33
     }
31
     /**
34
     /**
32
      * Combine questions retrieved from the database and
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
      * copare to responses stored
37
      * copare to responses stored
35
      * @returns {boolean}
38
      * @returns {boolean}
36
      */
39
      */
37
     get isComplete() {
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
      * Check that some responses are set
47
      * Check that some responses are set
43
      */
49
      */
44
     get hasResponses() {
50
     get hasResponses() {
45
         return this.responses.length && this.responses.length > 0
51
         return this.responses.length && this.responses.length > 0
46
-    } 
52
+    }
47
 
53
 
48
     /**
54
     /**
49
      * Login a profile by id number
55
      * Login a profile by id number
50
      * @param {number} profileId
56
      * @param {number} profileId
51
      * @returns {number} stored reactive id
57
      * @returns {number} stored reactive id
52
      */
58
      */
53
-     async login(profileId) {
59
+    async login(profileId) {
54
         console.warn('logging in:', profileId)
60
         console.warn('logging in:', profileId)
55
         this.id.value = parseInt(profileId)
61
         this.id.value = parseInt(profileId)
56
         return this.id.value
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
     async getTags() {
74
     async getTags() {
61
         try {
75
         try {
62
             const tags = []
76
             const tags = []
65
             console.error(err)
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
     async getResponses() {
86
     async getResponses() {
71
         try {
87
         try {
75
             console.error(err)
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
 const currentProfile = new Login()
110
 const currentProfile = new Login()

+ 11
- 5
frontend/src/services/notification.service.js Просмотреть файл

14
     listenFor(event, callback) {
14
     listenFor(event, callback) {
15
         this.source.addEventListener(event, callback)
15
         this.source.addEventListener(event, callback)
16
     }
16
     }
17
+    stop() {
18
+        this.source.close()
19
+    }
17
 }
20
 }
18
 
21
 
19
 /**
22
 /**
20
  * Example extension that listens for 'stonk' events
23
  * Example extension that listens for 'stonk' events
21
  */
24
  */
22
 class StonkAlert extends Toaster {
25
 class StonkAlert extends Toaster {
23
-    constructor(profileId) {
26
+    constructor(profileId, waveCb) {
24
         super(profileId)
27
         super(profileId)
25
-
28
+        this.event = 'stonk'
26
         this.stonks = {}
29
         this.stonks = {}
27
-        this.listenFor(`${profileId}.stonk`, message => {
30
+        this.listenFor(`${profileId}.${this.event}`, message => {
28
             const parsed = JSON.parse(message.data)
31
             const parsed = JSON.parse(message.data)
29
             this.stonks[parsed.name] = parsed
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 }

Загрузка…
Отмена
Сохранить