254 lines
6.8 KiB
TypeScript
Raw Normal View History

2021-07-25 13:23:25 +02:00
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
2021-08-11 22:21:26 +02:00
import AdminApplication from '@/components/Administration/AdminApplication.vue'
import AdminMenu from '@/components/Administration/AdminMenu.vue'
2021-10-27 18:51:59 +02:00
import AdminSports from '@/components/Administration/AdminSports.vue'
2021-10-31 11:21:21 +01:00
import AdminUsers from '@/components/Administration/AdminUsers.vue'
import Profile from '@/components/User/ProfileDisplay/index.vue'
import UserInfos from '@/components/User/ProfileDisplay/UserInfos.vue'
import UserPreferences from '@/components/User/ProfileDisplay/UserPreferences.vue'
import ProfileEdition from '@/components/User/ProfileEdition/index.vue'
import UserInfosEdition from '@/components/User/ProfileEdition/UserInfosEdition.vue'
import UserPictureEdition from '@/components/User/ProfileEdition/UserPictureEdition.vue'
import UserPreferencesEdition from '@/components/User/ProfileEdition/UserPreferencesEdition.vue'
2021-08-14 19:24:19 +02:00
import store from '@/store'
import { USER_STORE } from '@/store/constants'
const getTabFromPath = (path: string): string => {
const regex = /(\/profile)(\/edit)*(\/*)/
const tag = path.replace(regex, '').toUpperCase()
return tag === '' ? 'PROFILE' : tag.toUpperCase()
}
const routes: Array<RouteRecordRaw> = [
{
2021-07-25 13:23:25 +02:00
path: '/',
2021-07-25 13:34:44 +02:00
name: 'Dashboard',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'main' */ '@/views/DashBoard.vue'),
},
2021-08-08 11:49:01 +02:00
{
path: '/login',
name: 'Login',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'main' */ '@/views/LoginOrRegister.vue'),
2021-08-14 19:43:19 +02:00
props: { action: 'login' },
},
{
path: '/register',
name: 'Register',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'main' */ '@/views/LoginOrRegister.vue'),
2021-08-14 19:43:19 +02:00
props: { action: 'register' },
2021-08-08 11:49:01 +02:00
},
2021-10-20 17:38:25 +02:00
{
path: '/password-reset/sent',
name: 'PasswordEmailSent',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'reset' */ '@/views/PasswordResetView.vue'),
2021-10-20 17:38:25 +02:00
props: { action: 'request-sent' },
},
{
path: '/password-reset/request',
name: 'PasswordResetRequest',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'reset' */ '@/views/PasswordResetView.vue'),
2021-10-20 17:38:25 +02:00
props: { action: 'reset-request' },
},
{
path: '/password-reset/password-updated',
name: 'PasswordUpdated',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'reset' */ '@/views/PasswordResetView.vue'),
2021-10-20 17:38:25 +02:00
props: { action: 'password-updated' },
},
{
path: '/password-reset',
name: 'PasswordReset',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'reset' */ '@/views/PasswordResetView.vue'),
2021-10-20 17:38:25 +02:00
props: { action: 'reset' },
},
{
path: '/profile',
name: 'Profile',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'profile' */ '@/views/ProfileView.vue'),
children: [
{
path: '',
name: 'UserProfile',
component: Profile,
props: (route) => ({
tab: getTabFromPath(route.path),
}),
children: [
{
path: '',
name: 'UserInfos',
component: UserInfos,
},
{
path: 'preferences',
name: 'UserPreferences',
component: UserPreferences,
},
],
},
{
path: 'edit',
name: 'UserProfileEdition',
component: ProfileEdition,
props: (route) => ({
tab: getTabFromPath(route.path),
}),
children: [
{
path: '',
name: 'UserInfosEdition',
component: UserInfosEdition,
},
{
path: 'picture',
name: 'UserPictureEdition',
component: UserPictureEdition,
},
{
path: 'preferences',
name: 'UserPreferencesEdition',
component: UserPreferencesEdition,
},
],
},
],
2021-10-13 17:29:12 +02:00
},
2021-10-03 19:23:17 +02:00
{
path: '/statistics',
name: 'Statistics',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'main' */ '@/views/StatisticsView.vue'),
2021-10-03 19:23:17 +02:00
},
2021-10-05 15:23:41 +02:00
{
path: '/workouts',
name: 'Workouts',
2021-10-23 21:14:47 +02:00
component: () =>
import(
/* webpackChunkName: 'workouts' */ '@/views/workouts/WorkoutsView.vue'
),
2021-10-05 15:23:41 +02:00
},
{
path: '/workouts/:workoutId',
name: 'Workout',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'workouts' */ '@/views/workouts/Workout.vue'),
2021-09-27 10:01:17 +02:00
props: { displaySegment: false },
},
2021-09-28 12:39:12 +02:00
{
path: '/workouts/:workoutId/edit',
name: 'EditWorkout',
2021-10-23 21:14:47 +02:00
component: () =>
import(
/* webpackChunkName: 'workouts' */ '@/views/workouts/EditWorkout.vue'
),
2021-09-28 12:39:12 +02:00
},
2021-09-27 10:01:17 +02:00
{
path: '/workouts/:workoutId/segment/:segmentId',
name: 'WorkoutSegment',
2021-10-23 21:14:47 +02:00
component: () =>
import(/* webpackChunkName: 'workouts' */ '@/views/workouts/Workout.vue'),
2021-09-27 10:01:17 +02:00
props: { displaySegment: true },
},
2021-09-29 11:32:05 +02:00
{
path: '/workouts/add',
name: 'AddWorkout',
2021-10-23 21:14:47 +02:00
component: () =>
import(
/* webpackChunkName: 'workouts' */ '@/views/workouts/AddWorkout.vue'
),
},
2021-10-23 21:34:02 +02:00
{
path: '/admin',
name: 'Administration',
component: () =>
import(/* webpackChunkName: 'admin' */ '@/views/AdminView.vue'),
children: [
{
path: '',
name: 'AdministrationMenu',
component: AdminMenu,
},
{
path: 'application',
name: 'ApplicationAdministration',
component: AdminApplication,
},
{
path: 'application/edit',
name: 'ApplicationAdministrationEdition',
component: AdminApplication,
props: { edition: true },
},
2021-10-27 18:51:59 +02:00
{
path: 'sports',
name: 'SportsAdministration',
component: AdminSports,
},
2021-10-31 11:21:21 +01:00
{
path: 'users',
name: 'UsersAdministration',
component: AdminUsers,
},
],
2021-10-23 21:34:02 +02:00
},
2021-10-23 21:14:47 +02:00
{
path: '/:pathMatch(.*)*',
name: 'not-found',
component: () =>
import(/* webpackChunkName: 'main' */ '@/views/NotFoundView.vue'),
2021-09-29 11:32:05 +02:00
},
2021-07-25 13:23:25 +02:00
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
2021-07-25 13:23:25 +02:00
})
2021-10-20 17:38:25 +02:00
const pathsWithoutAuthentication = [
'/login',
'/password-reset',
'/password-reset/password-updated',
'/password-reset/request',
'/password-reset/sent',
'/register',
]
2021-08-14 19:24:19 +02:00
router.beforeEach((to, from, next) => {
store
.dispatch(USER_STORE.ACTIONS.CHECK_AUTH_USER)
.then(() => {
if (
store.getters[USER_STORE.GETTERS.IS_AUTHENTICATED] &&
2021-10-20 17:38:25 +02:00
pathsWithoutAuthentication.includes(to.path)
2021-08-14 19:24:19 +02:00
) {
return next('/')
} else if (
!store.getters[USER_STORE.GETTERS.IS_AUTHENTICATED] &&
2021-10-20 17:38:25 +02:00
!pathsWithoutAuthentication.includes(to.path)
2021-08-14 19:24:19 +02:00
) {
const path =
to.path === '/'
? { path: '/login' }
: { path: '/login', query: { from: to.fullPath } }
next(path)
} else {
next()
}
})
.catch((error) => {
console.error(error)
next()
})
})
2021-07-25 13:23:25 +02:00
export default router