11import Vue from "vue" ;
22import VueRouter from "vue-router" ;
3- import { auth } from "@/api" ;
3+ import { auth , quiz } from "@/api" ;
44import store from "@/store" ;
55
66Vue . use ( VueRouter ) ;
77
8+ function isChallengeOpen ( ) {
9+ return store . state . Quiz . quizHasStarted && ! store . state . Quiz . quizHasEnded ;
10+ }
11+
12+ function isChallengePending ( ) {
13+ return ! store . state . Quiz . quizHasStarted && ! store . state . Quiz . quizHasEnded ;
14+ }
15+
16+ function isChallengeClosed ( ) {
17+ return store . state . Quiz . quizHasEnded ;
18+ }
19+
820const routes = [
921 {
1022 path : "/home" ,
1123 name : "home" ,
12- redirect : {
13- name : "quiz"
24+ beforeEnter ( to , from , next ) {
25+ if ( isChallengeOpen ( ) || isChallengePending ( ) ) {
26+ next ( { name : 'quiz' } ) ;
27+ return ;
28+ }
29+
30+ if ( isChallengeClosed ( ) ) {
31+ next ( { name : 'voting' } )
32+ return ;
33+ }
1434 }
1535 } ,
1636 {
@@ -63,7 +83,10 @@ const routes = [
6383 {
6484 path : "/voting" ,
6585 name : "voting" ,
66- component : ( ) => import ( "@/views/Voting/Ballot.vue" )
86+ component : ( ) => import ( "@/views/Voting/Ballot.vue" ) ,
87+ meta : {
88+ challengeOver : true
89+ }
6790 } ,
6891 {
6992 // dev only
@@ -82,13 +105,8 @@ const routes = [
82105 component : async ( ) => {
83106 await store . dispatch ( "Quiz/refresh" ) ;
84107
85- // CHALLENGE IS OVER
86- if ( store . state . Quiz . quizHasEnded ) {
87- return import ( "@/views/Quiz/QuizFinished" ) ;
88- }
89-
90108 // CHALLENGE HAS NOT STARTED
91- if ( ! store . state . Quiz . quizHasStarted ) {
109+ if ( ! isChallengeOpen ( ) ) {
92110 return import ( "@/views/Quiz/QuizCountdown" ) ;
93111 }
94112
@@ -106,28 +124,30 @@ const routes = [
106124 if ( store . state . Quiz . isLastQuestion ) {
107125 return import ( "@/views/Quiz/QuizFinalQuestion" ) ;
108126 }
127+
109128 // NORMAL QUIZ MODE
110129 return import ( "@/views/Quiz/Quiz" ) ;
111130 } ,
112131 beforeEnter ( from , to , next ) {
113132 // USER MUST SEE INTRO VIDEO
114- if ( ! store . state . Quiz . hasSeenIntro && store . state . User . rank == 1 ) {
133+ if ( isChallengeOpen ( ) && ! store . state . Quiz . hasSeenIntro && store . state . User . rank == 1 ) {
115134 next ( { name : "quiz-intro" } ) ;
116135 return ;
117136 }
118137 next ( ) ;
119138 } ,
120139 meta : {
121- secured : true
140+ secured : true ,
141+ challengeOpenOrPending : true
122142 }
123143 } ,
124144 {
125145 path : "/quiz/intro" ,
126146 name : "quiz-intro" ,
127147 component : ( ) => import ( "@/views/Quiz/QuizIntro" ) ,
128- async beforeEnter ( to , from , next ) {
129- await store . dispatch ( "Quiz/refresh" ) ;
130- next ( ) ;
148+ meta : {
149+ secured : true ,
150+ challengeOpenOrPending : true
131151 }
132152 } ,
133153 {
@@ -144,19 +164,54 @@ const router = new VueRouter({
144164 routes
145165} ) ;
146166
147- router . beforeEach ( ( to , from , next ) => {
148- const isAuthenticated = ! ! auth . currentUser ( ) . auth ;
167+ router . beforeEach ( async ( to , from , next ) => {
149168 const requireAuth = to . matched . some ( record => record . meta . secured ) ;
150169 const requireAnon = to . matched . some ( record => record . meta . anon ) ;
170+ const requireChallengePending = to . matched . some ( record => record . meta . challengePending ) ;
171+ const requireChallengeOpen = to . matched . some ( record => record . meta . challengeOpen ) ;
172+ const requireChallengeClosed = to . matched . some ( record => record . meta . challengeOver ) ;
173+ const requireChallengeOpenPending = to . matched . some ( record => ( record . meta . challengeOpenOrPending ) ) ;
174+
175+ if ( requireChallengePending || requireChallengeOpen || requireChallengeClosed || requireChallengeOpenPending ) {
176+ await store . dispatch ( "Quiz/refresh" ) ;
177+
178+ const challengeIsClosed = isChallengeClosed ( ) ;
179+ const challengeIsPending = isChallengePending ( ) ;
180+ const challengeIsOpen = isChallengeOpen ( ) ;
181+
182+ if ( ! challengeIsClosed && requireChallengeClosed ) {
183+ next ( { name : 'home' } ) ;
184+ return ;
185+ }
186+
187+ if ( ! challengeIsOpen && requireChallengeOpen ) {
188+ next ( { name : 'home' } ) ;
189+ return ;
190+ }
191+
192+ if ( ! challengeIsPending && requireChallengePending ) {
193+ next ( { name : 'home' } ) ;
194+ return ;
195+ }
151196
152- if ( ! isAuthenticated && requireAuth ) {
153- next ( { name : "register" } ) ;
154- return ;
197+ if ( ( ! challengeIsOpen && ! challengeIsPending ) && requireChallengeOpenPending ) {
198+ next ( { name : 'home' } ) ;
199+ return ;
200+ }
155201 }
156202
157- if ( isAuthenticated && requireAnon ) {
158- next ( { name : "home" } ) ;
159- return ;
203+ if ( requireAuth || requireAnon ) {
204+ const isAuthenticated = ! ! auth . currentUser ( ) . auth ;
205+
206+ if ( ! isAuthenticated && requireAuth ) {
207+ next ( { name : "register" } ) ;
208+ return ;
209+ }
210+
211+ if ( isAuthenticated && requireAnon ) {
212+ next ( { name : "home" } ) ;
213+ return ;
214+ }
160215 }
161216
162217 next ( ) ;
0 commit comments