Przeglądaj źródła

:recycle: more simplifications | storing stating in components > large services

tags/0.0.1
J 4 lat temu
rodzic
commit
a0662cdc9f

+ 25
- 11
frontend/src/App.vue Wyświetl plik

@@ -1,6 +1,15 @@
1 1
 <template lang="pug">
2
-SideBar(v-if="showSidebar" :pid="pid" @updatePid="setPid" @hide="showSidebar = false")
3
-RouterView(:pid="pid" @show-sidebar="showSidebar = !showSidebar" @updatePid="setPid")
2
+SideBar(
3
+    v-if="showSidebar"
4
+    :pid="getPid"
5
+    @updatePid="setPid"
6
+    @hide="showSidebar = false"
7
+)
8
+RouterView(
9
+    :pid="getPid"
10
+    @updatePid="setPid"
11
+    @show-sidebar="showSidebar = !showSidebar"
12
+)
4 13
 </template>
5 14
 
6 15
 <script>
@@ -11,21 +20,27 @@ import SideBar from './components/SideBar.vue'
11 20
 import { Chatter, currentProfile, StonkAlert } from './services'
12 21
 import { surveyFactory } from './utils'
13 22
 
14
-const DEV = import.meta.env.VITE_DEV == 'true'
15
-const DEFAULT_PID = 45
23
+const DEV_MODE = import.meta.env.VITE_DEV == 'true'
24
+const DEV_PID = 45
16 25
 
17 26
 export default {
18 27
     components: { SideBar },
19 28
     data: () => ({
20
-        pid: null,
21 29
         showSidebar: false
22 30
     }),
31
+    computed: {
32
+        getPid: () => currentProfile.id.value
33
+    },
23 34
     async created() {
35
+        /** Get questions so we can compare against profile responses */
24 36
         await surveyFactory.getQuestions()
25 37
 
26
-        if(DEV) {
27
-            this.setPid(DEFAULT_PID)
28
-        }
38
+        /** 
39
+         * Development mode turns router guards off so
40
+         * we hard set the profile id instead of 
41
+         * using the login form
42
+        */
43
+        if(DEV_MODE) { this.setPid(DEV_PID) }
29 44
 
30 45
         this.setupChatter()
31 46
         this.setupToaster()
@@ -36,9 +51,8 @@ export default {
36 51
          * Sync up this components state with
37 52
          * the currentProfile handler
38 53
          */
39
-        setPid(pid) {
40
-            currentProfile.login(pid)
41
-            this.pid = currentProfile.id
54
+        setPid(profileId) {
55
+            currentProfile.login(profileId)
42 56
         },
43 57
 
44 58
         /**

+ 13
- 37
frontend/src/services/login.service.js Wyświetl plik

@@ -1,12 +1,14 @@
1
+import { ref } from 'vue'
1 2
 import { fetchQueueByProfileId, fetchResponsesByProfileId } from '../services'
2 3
 import { surveyFactory } from '../utils'
3 4
 
4 5
 class Login {
5 6
     constructor() {
6 7
         this._loading = false
8
+        
9
+        // Make reactive with vue observer
10
+        this.id = ref(null)
7 11
 
8
-        this.id = null
9
-        this.queue = []
10 12
         this.matches = []
11 13
         this.responses = []
12 14
     }
@@ -14,14 +16,11 @@ class Login {
14 16
         return this._loading
15 17
     }
16 18
     isLoggedIn() {
17
-        return this.id != null
19
+        return this.id.value != null
18 20
     }
19 21
     isComplete() {
20 22
         return this.responses.length == surveyFactory.questions.length
21 23
     }
22
-    hasQueue() {
23
-        return this.queue.length && this.queue.length > 0
24
-    }
25 24
     hasMatches() {
26 25
         return this.matches.length && this.matches.length > 0
27 26
     } 
@@ -29,28 +28,15 @@ class Login {
29 28
         return this.responses.length && this.responses.length > 0
30 29
     } 
31 30
 
32
-    async getQueue() {
33
-        this._loading = true
34
-        try {
35
-            const queueList = await fetchQueueByProfileId(this.id)
36
-            const formatted = this._reformatProfiles(queueList)
37
-            this._setQueue(formatted)
38
-            this._loading = false
39
-        } catch (err) {
40
-            console.error(err)
41
-        }
42
-    }
43
-    _reformatProfiles(profiles) {
44
-        const formattedList = profiles.map(profile => {
45
-            return {
46
-                pid: profile.profile_id,
47
-                name: profile.user_name,
48
-                avatar: '',
49
-            }
50
-        })
51
-        return formattedList
31
+    /**
32
+     * Login a profile id
33
+     */
34
+     async login(pid) {
35
+        this.id.value = parseInt(pid)
36
+        console.warn('logging in:', this.id.value)
37
+        return this.id.value
52 38
     }
53
-    _setQueue(queue) { this.queue = queue }
39
+    logout() { this.id.value = null }
54 40
 
55 41
     async getResponses() {
56 42
         try {
@@ -61,16 +47,6 @@ class Login {
61 47
         }
62 48
     }
63 49
     _setResponses(responses) { this.responses = responses }
64
-    
65
-    /**
66
-     * Login a profile id
67
-     */
68
-    async login(pid) {
69
-        console.warn('logging in:', pid)
70
-        this.id = parseInt(pid)
71
-        return this.id
72
-    }
73
-    logout() { this.id = null }
74 50
 }
75 51
 
76 52
 const currentProfile = new Login()

+ 5
- 44
frontend/src/views/HomeView.vue Wyświetl plik

@@ -1,9 +1,9 @@
1 1
 <template lang="pug">
2 2
 main.view--home.f-col.start.w-full
3 3
     header
4
-        h2 home
4
+        h2 home - profile: {{ pid }}
5 5
 
6
-    article(v-if="!loading")
6
+    article(v-if="cards.length && !loading")
7 7
         ProfileCardList(:profiles="cards" :pid="pid" @reload-queue="getQueue")
8 8
 
9 9
     p(v-else) Loading...
@@ -30,11 +30,10 @@ export default {
30 30
         loading: true,
31 31
     }),
32 32
     watch: {
33
-        pid() {
34
-            this.getQueue()
35
-        },
33
+        /** Fetch the queue if pid changes */
34
+        async pid() { await this.getQueue() },
36 35
     },
37
-    async created() {
36
+    async mounted() {
38 37
         await this.getQueue()
39 38
     },
40 39
     methods: {
@@ -57,44 +56,6 @@ export default {
57 56
             }
58 57
             this.loading = false
59 58
         },
60
-
61
-        // For Batch Data Parsing & Processing
62
-        _parseBatch(allBatches) {
63
-            const finished = { profiles: [], users: [], responses: [] }
64
-            allBatches.forEach(batch => {
65
-                const split = batch.value.split('\n')
66
-                split.splice(0, 5)
67
-                split.unshift('{')
68
-                const p = JSON.parse(split.join(''))
69
-                finished.profiles = [...p.profiles, ...finished.profiles]
70
-                finished.users = [...p.users, ...finished.users]
71
-                finished.responses = [...p.responses, ...finished.responses]
72
-            })
73
-            // console.log('parsed batch', finished)
74
-            return finished
75
-        },
76
-        _processProfilesFromBatch(parsed) {
77
-            const findUser = profile => {
78
-                return parsed.users.filter(u => u.user_id == profile.user_id)[0]
79
-            }
80
-            parsed.profiles.forEach(p => {
81
-                // console.log(parsed)
82
-                const user = findUser(p)
83
-                p.pid = p.profile_id
84
-                p.name = user.user_name
85
-                p.email = user.user_email
86
-                p.avatar = p.profile_media[0]
87
-                p.responses = parsed.responses.filter(
88
-                    r => r.profile_id == p.profile_id,
89
-                )
90
-                p.responses.forEach(r => {
91
-                    if (r.response_key_id == 7) {
92
-                        p.zipcode = r.val
93
-                    }
94
-                })
95
-                this.swipables.push(p)
96
-            })
97
-        },
98 59
     },
99 60
 }
100 61
 </script>

+ 15
- 18
frontend/src/views/MatchesView.vue Wyświetl plik

@@ -4,7 +4,7 @@ main.view--matches.f-col.start.w-full
4 4
         h2 Match Page
5 5
 
6 6
     article(v-if="!loading")
7
-        ProfileCardList(:profiles="profiles" :pid="pid" :is-grid="true")
7
+        ProfileCardList(:profiles="cards" :pid="pid" :is-grid="true")
8 8
     
9 9
     p(v-else) Loading...
10 10
     
@@ -24,40 +24,37 @@ export default {
24 24
         },
25 25
     },
26 26
     data: () => ({
27
-        matches: null,
27
+        cards: [],
28 28
         loading: true,
29 29
     }),
30
-    computed: {
31
-        profiles() {
32
-            if (this.loading || this.matches.length < 1) return []
33
-            return this.matches.map(m => {
30
+    watch: {
31
+        pid() { this.getMatches() },
32
+    },
33
+    async created() {
34
+        await this.getMatches()
35
+    },
36
+    methods: {
37
+        _reformatMatches(matches) {
38
+            return matches.map(m => {
34 39
                 return {
35 40
                     pid: m.profile.profile_id,
36
-                    avatar: m.profile.profile_media[0],
37 41
                     name: m.profile.user_name,
42
+                    avatar: m.profile.profile_media[0],
38 43
                 }
39 44
             })
40 45
         },
41
-    },
42
-    watch: {
43
-        pid() {
44
-            this.getMatches()
45
-        },
46
-    },
47
-    methods: {
48 46
         async getMatches() {
49 47
             this.loading = true
50 48
             try {
51
-                this.matches = await fetchMembershipsByProfileId(this.pid)
49
+                const matchList = await fetchMembershipsByProfileId(this.pid)
50
+                console.log('matchList :', matchList)
51
+                this.cards = this._reformatMatches(matchList)
52 52
             } catch (err) {
53 53
                 console.error(err)
54 54
             }
55 55
             this.loading = false
56 56
         },
57 57
     },
58
-    mounted() {
59
-        this.getMatches()
60
-    },
61 58
 }
62 59
 </script>
63 60
 

Ładowanie…
Anuluj
Zapisz