Przeglądaj źródła

:recycle: fixing route guards | moving around loging handling

tags/0.0.1
J 4 lat temu
rodzic
commit
a36c2bdd75

+ 1
- 1
backend/package.json Wyświetl plik

@@ -42,4 +42,4 @@
42 42
         "nyc": "^15.1.0",
43 43
         "sinon": "^11.1.2"
44 44
     }
45
-}
45
+}

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

@@ -8,7 +8,8 @@ import * as sss from '@/sss/import.css'
8 8
 
9 9
 import SideBar from './components/SideBar.vue'
10 10
 
11
-import { Chatter, StonkAlert, currentProfile } from './services'
11
+import { Chatter, StonkAlert, currentProfile } from '@/services'
12
+import { surveyFactory } from '@/utils'
12 13
 
13 14
 const DEFAULT_PID = 45
14 15
 
@@ -22,7 +23,8 @@ export default {
22 23
             return currentProfile.id ?  currentProfile.id : DEFAULT_PID
23 24
         }
24 25
     },
25
-    created() {
26
+    async created() {
27
+        await this.setupSurveyFactory()
26 28
         this.setupChatter()
27 29
         this.setupToaster()
28 30
     },
@@ -41,6 +43,16 @@ export default {
41 43
             const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID
42 44
             c.setup(testAccountUUID)
43 45
             console.log('---')
46
+        },
47
+        /**
48
+         * Get all the questions early because lots
49
+         * of things depend on the count
50
+         * 
51
+         * Note: only make the survey if the
52
+         * SurveyView is accessed
53
+         */
54
+        async setupSurveyFactory() {
55
+            await surveyFactory.getQuestions()
44 56
         }
45 57
     },
46 58
 }

+ 1
- 15
frontend/src/main.js Wyświetl plik

@@ -1,28 +1,14 @@
1 1
 import { createApp } from 'vue'
2 2
 import router from './router'
3
-import { currentProfile } from './services'
3
+import { checkLoginStatus } from './router/guards'
4 4
 
5 5
 import App from './App.vue'
6 6
 import MainNav from './components/MainNav.vue'
7 7
 
8
-/**
9
- * Router guard functions
10
- */
11
-const checkLoginStatus = (destination, nextCb) => {
12
-    const requiresCompleteProfile = destination.meta.requiresAuth
13
-
14
-    if(!currentProfile.isLoggedIn() && requiresCompleteProfile) {
15
-        nextCb('login')
16
-    } else {
17
-        nextCb()
18
-    }
19
-}
20
-
21 8
 /**
22 9
  * Check between route changes for login/timeout
23 10
  */
24 11
 router.beforeEach((to, from, next) => {
25
-    console.log(currentProfile.isLoggedIn())
26 12
     /**
27 13
      * Use the loginService to deal with login details
28 14
      */

+ 22
- 0
frontend/src/router/guards.js Wyświetl plik

@@ -0,0 +1,22 @@
1
+import { currentProfile } from '../services'
2
+
3
+const checkLoginStatus = (destination, nextCb) => {
4
+    console.warn('currentProfile logged in:', currentProfile.isLoggedIn())
5
+    console.log('destination:', destination.meta)
6
+    if (
7
+        destination.meta.requiresCompleteProfile &&
8
+        currentProfile.isLoggedIn() &&
9
+        !currentProfile.isComplete() 
10
+    ) {
11
+        nextCb('survey')
12
+    } else if(
13
+        destination.meta.requiresAuth &&
14
+        !currentProfile.isLoggedIn()
15
+    ) {
16
+        nextCb('login')
17
+    } else {
18
+        nextCb()
19
+    }
20
+}
21
+
22
+export { checkLoginStatus }

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

@@ -11,30 +11,31 @@ const routes = [
11 11
         path: '/',
12 12
         component: HomeView,
13 13
         name: 'HomeView',
14
-        meta: { requiresAuth: true, requiresProfile: true },
14
+        meta: { requiresAuth: true, requiresCompleteProfile: true },
15 15
     },
16 16
     {
17 17
         path: '/profile/:pid',
18 18
         component: ProfileView,
19 19
         name: 'ProfileView',
20
-        meta: { requiresAuth: true, requiresProfile: true },
20
+        meta: { requiresAuth: true, requiresCompleteProfile: true },
21 21
     },
22 22
     {
23 23
         path: '/matches',
24 24
         component: MatchesView,
25 25
         name: 'MatchesView',
26
-        meta: { requiresAuth: true, requiresProfile: true },
26
+        meta: { requiresAuth: true, requiresCompleteProfile: true },
27 27
     },
28 28
     {
29 29
         path: `/survey`,
30 30
         component: SurveyView,
31 31
         name: `SurveyView`,
32
-        meta: { requiresAuth: true, requiresProfile: true },
32
+        meta: { requiresAuth: true, requiresCompleteProfile: false },
33 33
     },
34 34
     {
35 35
         path: `/login`,
36 36
         component: LoginView,
37 37
         name: `LoginView`,
38
+        meta: { requiresAuth: false, requiresCompleteProfile: false },
38 39
     },
39 40
 ]
40 41
 

+ 4
- 0
frontend/src/services/login.service.js Wyświetl plik

@@ -1,4 +1,5 @@
1 1
 import { fetchQueueByProfileId, fetchResponsesByProfileId } from '../services'
2
+import { surveyFactory } from '../utils'
2 3
 
3 4
 class Login {
4 5
     constructor() {
@@ -15,6 +16,9 @@ class Login {
15 16
     isLoggedIn() {
16 17
         return this.id != null
17 18
     }
19
+    isComplete() {
20
+        return this.responses.length == surveyFactory.questions.length
21
+    }
18 22
     hasQueue() {
19 23
         return this.queue.length && this.queue.length > 0
20 24
     }

+ 2
- 2
frontend/src/services/survey.service.js Wyświetl plik

@@ -7,7 +7,7 @@ import { db } from '../utils/db'
7 7
  * @param {number} profileId
8 8
  * @returns {array} instantiated Profile objects (see: /entites/profile)
9 9
  */
10
-const fetchQuestionsByProfileId = async profileId => {
10
+const fetchQuestions = async () => {
11 11
     return await db.get(`/survey/questions`)
12 12
 }
13 13
 
@@ -54,7 +54,7 @@ const fetchResponsesByProfileId = async profileId => {
54 54
 }
55 55
 
56 56
 export {
57
-    fetchQuestionsByProfileId,
57
+    fetchQuestions,
58 58
     saveSurveyByProfileId,
59 59
     updateSurveyByProfileId,
60 60
     scoreSurveyByProfileId,

+ 2
- 0
frontend/src/utils/index.js Wyświetl plik

@@ -1,4 +1,6 @@
1 1
 import Joi from 'joi'
2
+import { fetchQuestions } from '../services'
3
+
2 4
 import { Connector } from './db'
3 5
 import { SurveyFactory } from './survey'
4 6
 import { possible } from './lang'

+ 15
- 7
frontend/src/utils/survey.js Wyświetl plik

@@ -1,17 +1,17 @@
1 1
 import { Survey } from '../entities'
2
-import { fetchQuestionsByProfileId } from '../services'
2
+import { fetchQuestions } from '../services'
3 3
 
4 4
 class SurveyFactory {
5 5
     constructor(responses) {
6 6
         this.responsesByCategory = responses
7
+        this.questions = []
7 8
     }
8
-    async setSteps(langFile) {
9
+    _setSteps(langFile) {
9 10
         const stepsToProcess = [...Object.values(langFile) ]
10
-        const questions = await fetchQuestionsByProfileId()
11 11
         const seenIds = []
12 12
         const stepsInCommon = stepsToProcess.map(step => {
13 13
             // Match question to step
14
-            const match = questions.filter(q => q.response_key_prompt == step)[0]
14
+            const match = this.questions.filter(q => q.response_key_prompt == step)[0]
15 15
             if(match) { seenIds.push(match.response_key_id) }
16 16
             return {
17 17
                 response_key_category: match ? match.response_key_category: 'profile',
@@ -21,11 +21,19 @@ class SurveyFactory {
21 21
                 responses: this.responsesByCategory[step] ? this.responsesByCategory[step] : [] 
22 22
             }
23 23
         })
24
-        const unseen = questions.filter(q => !seenIds.includes(q.response_key_id))
24
+        const unseen = this.questions.filter(q => !seenIds.includes(q.response_key_id))
25 25
         return [...stepsInCommon, ...unseen]
26 26
     }
27
-    async createSurvey(langFile, roleTree) {
28
-        const steps = await this.setSteps(langFile)
27
+    async getQuestions() {
28
+        this.questions = await fetchQuestions()
29
+    }
30
+    createSurvey(langFile, roleTree) {
31
+        if(!this.questions.length) {
32
+            console.error('Get questions before creating a survey')
33
+            return
34
+        }
35
+
36
+        const steps = this._setSteps(langFile)
29 37
         return new Survey(steps, roleTree)
30 38
     }
31 39
 }

+ 0
- 3
frontend/src/views/LoginView.vue Wyświetl plik

@@ -35,9 +35,6 @@ export default {
35 35
             const alreadyLoggedIn = await currentProfile.isLoggedIn()
36 36
             if(!alreadyLoggedIn) {
37 37
                 await currentProfile.login(this.form.profileId)
38
-                if(!currentProfile.hasResponses()) {
39
-                    toRoute.name = 'SurveyView'
40
-                }
41 38
             }
42 39
 
43 40
             this.$router.push(toRoute)

+ 8
- 2
frontend/src/views/SurveyView.vue Wyświetl plik

@@ -72,8 +72,10 @@ main.view--survey.f-col.start.w-full
72 72
 </template>
73 73
 
74 74
 <script>
75
+import { checkLoginStatus } from '../router/guards'
75 76
 import { surveyFactory } from '../utils'
76 77
 import { allSteps, possible, stepToComponentMap } from '../utils/lang'
78
+
77 79
 import SurveyForm from '../components/form.vue'
78 80
 import ButtonMulti from '../components/form/button-multi.vue'
79 81
 import ButtonChoice from '../components/form/button-choice.vue'
@@ -119,8 +121,12 @@ export default {
119 121
             )
120 122
         },
121 123
     },
122
-    async created() {
123
-        this.validSurvey = await surveyFactory.createSurvey(
124
+    /**
125
+     * Before this ever gets called, surveyFactory.questions
126
+     * must be set by App.created()
127
+     */
128
+    created() {
129
+        this.validSurvey = surveyFactory.createSurvey(
124 130
             allSteps['usa'],
125 131
             possible['usa']['roles'],
126 132
         )

Ładowanie…
Anuluj
Zapisz