1- import { createRouter , createWebHistory } from 'vue-router' ;
1+ import {
2+ createRouter ,
3+ createWebHistory ,
4+ type RouteLocationNormalizedGeneric ,
5+ type RouteLocationNormalizedLoadedGeneric
6+ } from 'vue-router' ;
27import HomeView from '../views/HomeView.vue' ;
38import LoginView from '../views/LoginView.vue' ;
49import { useAuthStore } from '@/stores/auth' ;
510import { type UserData } from '@/models' ;
11+ import EventDetails from '@/components/EventDetails.vue' ;
12+ import NoEventDetails from '@/components/NoEventDetails.vue' ;
13+
14+ import { type Event } from '@/models' ;
15+
16+ async function handleEventIdRedirects (
17+ to : RouteLocationNormalizedGeneric ,
18+ _from : RouteLocationNormalizedLoadedGeneric
19+ ) {
20+ const response = await fetch ( `/api/v1/event/${ to . params . id } ` ) ;
21+
22+ if ( response . status == 404 ) {
23+ return {
24+ path : '/'
25+ } ;
26+ }
27+
28+ if ( response . status != 200 ) {
29+ throw Error ( 'Bad Return Code' ) ;
30+ }
31+
32+ const jsonData : Event = await response . json ( ) ;
33+
34+ const isInPast = new Date ( jsonData . startTime ) . getTime ( ) < Date . now ( ) ;
35+
36+ const isGoingToPast = to . path . includes ( 'history' ) ;
37+
38+ if ( ( isGoingToPast && isInPast ) || ( ! isGoingToPast && ! isInPast ) ) {
39+ return true ;
40+ }
41+
42+ if ( isInPast ) {
43+ return { path : `/history/${ to . params . id } ` } ;
44+ } else {
45+ return { path : `${ to . params . id } ` } ;
46+ }
47+ }
648
749const router = createRouter ( {
850 history : createWebHistory ( import . meta. env . BASE_URL ) ,
951 routes : [
1052 {
1153 path : '/' ,
12- name : 'home' ,
1354 component : HomeView ,
14- props : { showPast : false }
55+ props : { showPast : false } ,
56+ children : [
57+ {
58+ path : '' ,
59+ name : 'home' ,
60+ component : NoEventDetails
61+ } ,
62+ {
63+ path : ':id' ,
64+ component : EventDetails ,
65+ beforeEnter : handleEventIdRedirects ,
66+ props : ( route ) => ( { id : Number ( route . params . id ) } )
67+ }
68+ ]
1569 } ,
1670 {
1771 path : '/login' ,
@@ -20,9 +74,26 @@ const router = createRouter({
2074 } ,
2175 {
2276 path : '/history' ,
23- name : 'history' ,
2477 component : HomeView ,
25- props : { showPast : true }
78+ props : { showPast : true } ,
79+ children : [
80+ {
81+ path : '' ,
82+ name : 'history' ,
83+ component : NoEventDetails
84+ } ,
85+ {
86+ path : ':id' ,
87+ component : EventDetails ,
88+ beforeEnter : handleEventIdRedirects ,
89+ props : ( route ) => ( { id : Number ( route . params . id ) } )
90+ }
91+ ]
92+ } ,
93+ {
94+ path : '/event/:id' ,
95+ beforeEnter : handleEventIdRedirects ,
96+ component : NoEventDetails // This never gets used but has to be here for the beforeEnter to be called
2697 }
2798 ]
2899} ) ;
0 commit comments