Attempt to fix selected piece over smartphone menu
[vchess.git] / client / src / store.js
CommitLineData
c66a829b
BA
1import { ajax } from "./utils/ajax";
2import { getRandString } from "./utils/alea";
c66a829b 3
4486a21e 4// Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
c66a829b
BA
5export const store =
6{
7 state: {
8 variants: [],
9 tr: {},
10 user: {},
c66a829b
BA
11 settings: {},
12 lang: "",
13 },
cdb34c93 14 socketCloseListener: null,
8418f0d7 15 initialize() {
c66a829b 16 ajax("/variants", "GET", res => { this.state.variants = res.variantArray; });
98f48579
BA
17 let mysid = localStorage["mysid"];
18 if (!mysid)
19 {
20 mysid = getRandString();
21 localStorage["mysid"] = mysid; //done only once (unless user clear browser data)
22 }
5ea8d113 23 // Quick user setup using local storage:
c66a829b 24 this.state.user = {
98f48579
BA
25 id: localStorage["myid"] || 0,
26 name: localStorage["myname"] || "", //"" for "anonymous"
a7f9f050
BA
27 email: "", //unknown yet
28 notify: false, //email notifications
98f48579 29 sid: mysid,
c66a829b 30 };
5ea8d113
BA
31 // Slow verification through the server:
32 // NOTE: still superficial identity usurpation possible, but difficult.
33 ajax("/whoami", "GET", res => {
34 this.state.user.id = res.id;
35 this.state.user.name = res.name;
36 this.state.user.email = res.email;
37 this.state.user.notify = res.notify;
38 });
c66a829b
BA
39 // Settings initialized with values from localStorage
40 this.state.settings = {
dfeb96ea
BA
41 bcolor: localStorage.getItem("bcolor") || "lichess",
42 sound: parseInt(localStorage.getItem("sound")) || 1,
43 hints: localStorage.getItem("hints") == "true",
44 highlight: localStorage.getItem("highlight") == "true",
c66a829b 45 };
8418f0d7
BA
46 const supportedLangs = ["en","es","fr"];
47 this.state.lang = localStorage["lang"] ||
48 (supportedLangs.includes(navigator.language)
49 ? navigator.language
50 : "en");
51 this.setTranslations();
c66a829b 52 },
dfeb96ea
BA
53 updateSetting: function(propName, value) {
54 this.state.settings[propName] = value;
55 localStorage.setItem(propName, value);
56 },
c66a829b 57 setTranslations: async function() {
7c3cd379 58 // Import translations from "./translations/$lang.js"
c66a829b
BA
59 const tModule = await import("@/translations/" + this.state.lang + ".js");
60 this.state.tr = tModule.translations;
61 },
62 setLanguage(lang) {
63 this.state.lang = lang;
64 this.setTranslations();
65 },
66};