Small fix
[vchess.git] / client / src / router.js
CommitLineData
625022fd
BA
1import Vue from "vue";
2import Router from "vue-router";
cf2343ce 3import Hall from "./views/Hall.vue";
625022fd
BA
4
5Vue.use(Router);
6
7function loadView(view) {
8 return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${view}.vue`)
9}
10
1aeed627
BA
11import { ajax } from "@/utils/ajax";
12import { store } from "@/store";
13
786e0065 14const router = new Router({
625022fd
BA
15 routes: [
16 {
17 path: "/",
cf2343ce
BA
18 name: "hall",
19 component: Hall,
625022fd 20 },
5b020e73
BA
21 {
22 path: "/variants",
23 name: "variants",
24 component: loadView("Variants"),
25 },
cf2343ce
BA
26 {
27 path: "/variants/:vname([a-zA-Z0-9]+)",
28 name: "rules",
29 component: loadView("Rules"),
30 },
1aeed627
BA
31 {
32 path: "/authenticate/:token",
33 name: "authenticate",
34 beforeEnter: (to, from, next) => {
35 ajax(
36 "/authenticate",
37 "GET",
38 {token: to.params["token"]},
39 (res) => {
98f48579
BA
40 if (!res.errmsg) //if not already logged in
41 {
42 store.state.user.id = res.id;
43 store.state.user.name = res.name;
44 store.state.user.email = res.email;
45 store.state.user.notify = res.notify;
46 localStorage["myname"] = res.name;
47 localStorage["myid"] = res.id;
48 }
a7f9f050 49 next();
1aeed627
BA
50 }
51 );
1aeed627 52 },
deca03e8
BA
53 component: Hall,
54 //redirect: "/", //problem: redirection before end of AJAX request
1aeed627 55 },
f7121527
BA
56 {
57 path: "/game/:id",
58 name: "game",
59 component: loadView("Game"),
60 },
ccd4a2b7
BA
61// {
62// path: "/about",
63// name: "about",
64// // route level code-splitting
65// // this generates a separate chunk (about.[hash].js) for this route
66// // which is lazy-loaded when the route is visited.
67// component: loadView('About'),
68// //function() {
69// // return import(/* webpackChunkName: "about" */ "./views/About.vue");
70// //}
71// },
8d61fc4a 72 // TODO: gameRef, problemId: https://router.vuejs.org/guide/essentials/dynamic-matching.html
625022fd
BA
73 ]
74});
786e0065
BA
75
76router.beforeEach((to, from, next) => {
77 window.scrollTo(0, 0); //TODO: check if a live game is running; if yes, call next('/game')
78 //https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards
79 next();
4486a21e
BA
80 //TODO: si une partie en cours dans storage, rediriger vers cette partie
81 //(à condition que l'URL n'y corresponde pas déjà !)
82 // (l'identifiant de l'utilisateur si connecté)
83// if (!!localStorage["variant"])
84// location.hash = "#game?id=" + localStorage["gameId"];
786e0065
BA
85});
86
87export default router;