Saving current state
[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: localStorage["myuid"] || 0,
19 name: localStorage["myname"] || "", //"anonymous"
20 sid: localStorage["mysid"] || getRandString(),
21 };
22 this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + this.state.user.sid);
23 // Settings initialized with values from localStorage
24 this.state.settings = {
25 bcolor: localStorage["bcolor"] || "lichess",
26 sound: parseInt(localStorage["sound"]) || 2,
27 hints: parseInt(localStorage["hints"]) || 1,
28 coords: !!eval(localStorage["coords"]),
29 highlight: !!eval(localStorage["highlight"]),
30 sqSize: parseInt(localStorage["sqSize"]),
31 };
32 const socketCloseListener = () => {
33 this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid);
34 }
35 this.state.conn.onclose = socketCloseListener;
36 const supportedLangs = ["en","es","fr"];
37 this.state.lang = localStorage["lang"] ||
38 supportedLangs.includes(navigator.language)
39 ? navigator.language
40 : "en";
41 this.setTranslations();
42 },
43 setTranslations: async function() {
44 // Import translations from "./translations/$lang.js"
45 const tModule = await import("@/translations/" + this.state.lang + ".js");
46 this.state.tr = tModule.translations;
47 },
48 setLanguage(lang) {
49 this.state.lang = lang;
50 this.setTranslations();
51 },
52 };