Better menu ordering ?
[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; });
9ef63965 17 let mysid = localStorage.getItem("mysid");
98f48579
BA
18 if (!mysid)
19 {
20 mysid = getRandString();
9ef63965 21 localStorage.setItem("mysid", mysid); //done only once (unless user clear browser data)
98f48579 22 }
5ea8d113 23 // Quick user setup using local storage:
c66a829b 24 this.state.user = {
9ef63965
BA
25 id: localStorage.getItem("myid") || 0,
26 name: localStorage.getItem("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;
9ef63965
BA
35 const storedId = localStorage.getItem("myid");
36 if (res.id > 0 && !storedId) //user cleared localStorage
37 localStorage.setItem("myid", res.id);
38 else if (res.id == 0 && !!storedId) //user cleared cookie
39 localStorage.removeItem("myid");
5ea8d113 40 this.state.user.name = res.name;
9ef63965
BA
41 const storedName = localStorage.getItem("myname");
42 if (!!res.name && !storedName) //user cleared localStorage
43 localStorage.setItem("myname", res.name);
44 else if (!res.name && !!storedName) //user cleared cookie
45 localStorage.removeItem("myname");
5ea8d113
BA
46 this.state.user.email = res.email;
47 this.state.user.notify = res.notify;
48 });
c66a829b
BA
49 // Settings initialized with values from localStorage
50 this.state.settings = {
dfeb96ea
BA
51 bcolor: localStorage.getItem("bcolor") || "lichess",
52 sound: parseInt(localStorage.getItem("sound")) || 1,
53 hints: localStorage.getItem("hints") == "true",
54 highlight: localStorage.getItem("highlight") == "true",
c66a829b 55 };
8418f0d7
BA
56 const supportedLangs = ["en","es","fr"];
57 this.state.lang = localStorage["lang"] ||
58 (supportedLangs.includes(navigator.language)
59 ? navigator.language
60 : "en");
61 this.setTranslations();
c66a829b 62 },
dfeb96ea
BA
63 updateSetting: function(propName, value) {
64 this.state.settings[propName] = value;
65 localStorage.setItem(propName, value);
66 },
c66a829b 67 setTranslations: async function() {
7c3cd379 68 // Import translations from "./translations/$lang.js"
c66a829b
BA
69 const tModule = await import("@/translations/" + this.state.lang + ".js");
70 this.state.tr = tModule.translations;
71 },
72 setLanguage(lang) {
73 this.state.lang = lang;
74 this.setTranslations();
75 },
76};