Add debug traces
[vchess.git] / client / src / store.js
1 import { ajax } from "./utils/ajax";
2 import { getRandString } from "./utils/alea";
3 import params from "./parameters"; //for socket connection
4
5 export const store =
6 {
7 state: {
8 variants: [],
9 tr: {},
10 user: {},
11 conn: null,
12 settings: {},
13 lang: "",
14 },
15 initialize() {
16 ajax("/variants", "GET", res => { this.state.variants = res.variantArray; });
17 this.state.user = {
18 id: 0, //unknown yet
19 name: "", //"anonymous"
20 email: "", //unknown yet
21 notify: false, //email notifications
22 sid: localStorage["mysid"] || getRandString(),
23 };
24 ajax("/whoami", "GET", res => {
25 if (res.id > 0)
26 {
27 this.state.user.id = res.id;
28 this.state.user.name = res.name;
29 this.state.user.email = res.email;
30 this.state.user.notify = res.notify;
31 }
32 });
33 this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + this.state.user.sid);
34 // Settings initialized with values from localStorage
35 this.state.settings = {
36 bcolor: localStorage["bcolor"] || "lichess",
37 sound: parseInt(localStorage["sound"]) || 2,
38 hints: parseInt(localStorage["hints"]) || 1,
39 coords: !!eval(localStorage["coords"]),
40 highlight: !!eval(localStorage["highlight"]),
41 sqSize: parseInt(localStorage["sqSize"]),
42 };
43 const socketCloseListener = () => {
44 this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid);
45 }
46 this.state.conn.onclose = socketCloseListener;
47 const supportedLangs = ["en","es","fr"];
48 this.state.lang = localStorage["lang"] ||
49 supportedLangs.includes(navigator.language)
50 ? navigator.language
51 : "en";
52 this.setTranslations();
53 },
54 setTranslations: async function() {
55 // Import translations from "./translations/$lang.js"
56 const tModule = await import("@/translations/" + this.state.lang + ".js");
57 this.state.tr = tModule.translations;
58 },
59 setLanguage(lang) {
60 this.state.lang = lang;
61 this.setTranslations();
62 },
63 };