refactor router to resume live games
[vchess.git] / client / src / router.js
... / ...
CommitLineData
1import Vue from "vue";
2import Router from "vue-router";
3import Hall from "./views/Hall.vue";
4
5Vue.use(Router);
6
7function loadView(view) {
8 return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${view}.vue`)
9}
10
11import { ajax } from "@/utils/ajax";
12import { store } from "@/store";
13
14const router = new Router({
15 routes: [
16 {
17 path: "/",
18 name: "hall",
19 component: Hall,
20 },
21 {
22 path: "/variants",
23 name: "variants",
24 component: loadView("Variants"),
25 },
26 {
27 path: "/variants/:vname([a-zA-Z0-9]+)",
28 name: "rules",
29 component: loadView("Rules"),
30 },
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) => {
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 }
49 next();
50 }
51 );
52 },
53 component: Hall,
54 //redirect: "/", //problem: redirection before end of AJAX request
55 },
56// {
57// path: "/about",
58// name: "about",
59// // route level code-splitting
60// // this generates a separate chunk (about.[hash].js) for this route
61// // which is lazy-loaded when the route is visited.
62// component: loadView('About'),
63// //function() {
64// // return import(/* webpackChunkName: "about" */ "./views/About.vue");
65// //}
66// },
67 // TODO: gameRef, problemId: https://router.vuejs.org/guide/essentials/dynamic-matching.html
68 ]
69});
70
71router.beforeEach((to, from, next) => {
72 window.scrollTo(0, 0); //TODO: check if a live game is running; if yes, call next('/game')
73 //https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards
74 next();
75});
76
77export default router;