Convert all remaining tabs by 2spaces
[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) {
dac39588 8 return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${view}.vue`)
625022fd
BA
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 }
dac39588
BA
49 // TODO: I don't like these 2 lines, "next('/')" should be enough
50 window.location = "/";
9330b976 51 next();
1aeed627
BA
52 }
53 );
1aeed627 54 },
deca03e8
BA
55 component: Hall,
56 //redirect: "/", //problem: redirection before end of AJAX request
1aeed627 57 },
afd3240d
BA
58 {
59 path: "/mygames",
60 name: "mygames",
61 component: loadView("MyGames"),
62 },
f7121527
BA
63 {
64 path: "/game/:id",
65 name: "game",
66 component: loadView("Game"),
67 },
afd3240d
BA
68 {
69 path: "/analyze/:vname([a-zA-Z0-9]+)",
70 name: "analyze",
652f40de 71 component: loadView("Analyze"),
afd3240d 72 },
92a523d1
BA
73 {
74 path: "/about",
75 name: "about",
76 component: loadView("About"),
77 },
625022fd
BA
78 ]
79});
786e0065
BA
80
81router.beforeEach((to, from, next) => {
92a523d1
BA
82 window.scrollTo(0, 0);
83 if (!!store.state.conn) //uninitialized at first page
84 {
85 // Notify WebSockets server (TODO: path or fullPath?)
86 store.state.conn.send(JSON.stringify({code: "pagechange", page: to.path}));
87 }
786e0065
BA
88 next();
89});
90
91export default router;