Переглянути джерело

:recycle: fixing route guards | moving around loging handling

tags/0.0.1
J 4 роки тому
джерело
коміт
a36c2bdd75

+ 1
- 1
backend/package.json Переглянути файл

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

+ 14
- 2
frontend/src/App.vue Переглянути файл

8
 
8
 
9
 import SideBar from './components/SideBar.vue'
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
 const DEFAULT_PID = 45
14
 const DEFAULT_PID = 45
14
 
15
 
22
             return currentProfile.id ?  currentProfile.id : DEFAULT_PID
23
             return currentProfile.id ?  currentProfile.id : DEFAULT_PID
23
         }
24
         }
24
     },
25
     },
25
-    created() {
26
+    async created() {
27
+        await this.setupSurveyFactory()
26
         this.setupChatter()
28
         this.setupChatter()
27
         this.setupToaster()
29
         this.setupToaster()
28
     },
30
     },
41
             const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID
43
             const testAccountUUID = import.meta.env.VITE_TEST_ACCOUNT_UUID
42
             c.setup(testAccountUUID)
44
             c.setup(testAccountUUID)
43
             console.log('---')
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 Переглянути файл

1
 import { createApp } from 'vue'
1
 import { createApp } from 'vue'
2
 import router from './router'
2
 import router from './router'
3
-import { currentProfile } from './services'
3
+import { checkLoginStatus } from './router/guards'
4
 
4
 
5
 import App from './App.vue'
5
 import App from './App.vue'
6
 import MainNav from './components/MainNav.vue'
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
  * Check between route changes for login/timeout
9
  * Check between route changes for login/timeout
23
  */
10
  */
24
 router.beforeEach((to, from, next) => {
11
 router.beforeEach((to, from, next) => {
25
-    console.log(currentProfile.isLoggedIn())
26
     /**
12
     /**
27
      * Use the loginService to deal with login details
13
      * Use the loginService to deal with login details
28
      */
14
      */

+ 22
- 0
frontend/src/router/guards.js Переглянути файл

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 Переглянути файл

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

+ 4
- 0
frontend/src/services/login.service.js Переглянути файл

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

+ 2
- 2
frontend/src/services/survey.service.js Переглянути файл

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

+ 2
- 0
frontend/src/utils/index.js Переглянути файл

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

+ 15
- 7
frontend/src/utils/survey.js Переглянути файл

1
 import { Survey } from '../entities'
1
 import { Survey } from '../entities'
2
-import { fetchQuestionsByProfileId } from '../services'
2
+import { fetchQuestions } from '../services'
3
 
3
 
4
 class SurveyFactory {
4
 class SurveyFactory {
5
     constructor(responses) {
5
     constructor(responses) {
6
         this.responsesByCategory = responses
6
         this.responsesByCategory = responses
7
+        this.questions = []
7
     }
8
     }
8
-    async setSteps(langFile) {
9
+    _setSteps(langFile) {
9
         const stepsToProcess = [...Object.values(langFile) ]
10
         const stepsToProcess = [...Object.values(langFile) ]
10
-        const questions = await fetchQuestionsByProfileId()
11
         const seenIds = []
11
         const seenIds = []
12
         const stepsInCommon = stepsToProcess.map(step => {
12
         const stepsInCommon = stepsToProcess.map(step => {
13
             // Match question to step
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
             if(match) { seenIds.push(match.response_key_id) }
15
             if(match) { seenIds.push(match.response_key_id) }
16
             return {
16
             return {
17
                 response_key_category: match ? match.response_key_category: 'profile',
17
                 response_key_category: match ? match.response_key_category: 'profile',
21
                 responses: this.responsesByCategory[step] ? this.responsesByCategory[step] : [] 
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
         return [...stepsInCommon, ...unseen]
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
         return new Survey(steps, roleTree)
37
         return new Survey(steps, roleTree)
30
     }
38
     }
31
 }
39
 }

+ 0
- 3
frontend/src/views/LoginView.vue Переглянути файл

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

+ 8
- 2
frontend/src/views/SurveyView.vue Переглянути файл

72
 </template>
72
 </template>
73
 
73
 
74
 <script>
74
 <script>
75
+import { checkLoginStatus } from '../router/guards'
75
 import { surveyFactory } from '../utils'
76
 import { surveyFactory } from '../utils'
76
 import { allSteps, possible, stepToComponentMap } from '../utils/lang'
77
 import { allSteps, possible, stepToComponentMap } from '../utils/lang'
78
+
77
 import SurveyForm from '../components/form.vue'
79
 import SurveyForm from '../components/form.vue'
78
 import ButtonMulti from '../components/form/button-multi.vue'
80
 import ButtonMulti from '../components/form/button-multi.vue'
79
 import ButtonChoice from '../components/form/button-choice.vue'
81
 import ButtonChoice from '../components/form/button-choice.vue'
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
             allSteps['usa'],
130
             allSteps['usa'],
125
             possible['usa']['roles'],
131
             possible['usa']['roles'],
126
         )
132
         )

Завантаження…
Відмінити
Зберегти