10ad10e694bffdafd592cb964ae1e56331a4d0ff
[vchess.git] / client / src / store.js
1 // NOTE: do not use ajax() here because ajax.js require the store for translations
2 import params from "./parameters"; //for server URL
3 import { getRandString } from "./utils/alea";
4
5 // Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
6 export const store = {
7 state: {
8 variants: [],
9 tr: {},
10 user: {},
11 settings: {},
12 lang: ""
13 },
14 socketCloseListener: null,
15 initialize() {
16 fetch(
17 params.serverUrl + "/variants",
18 {method: "GET"},
19 )
20 .then(res => res.json())
21 .then(json => {
22 this.state.variants = json.variantArray.sort(
23 (v1,v2) => v1.name.localeCompare(v2.name));
24 });
25 let mysid = localStorage.getItem("mysid");
26 // Assign mysid only once (until next time user clear browser data)
27 if (!mysid) {
28 mysid = getRandString();
29 localStorage.setItem("mysid", mysid);
30 }
31 // Quick user setup using local storage:
32 this.state.user = {
33 id: localStorage.getItem("myid") || 0,
34 name: localStorage.getItem("myname") || "", //"" for "anonymous"
35 email: "", //unknown yet
36 notify: false, //email notifications
37 sid: mysid
38 };
39 // Slow verification through the server:
40 // NOTE: still superficial identity usurpation possible, but difficult.
41 fetch(
42 params.serverUrl + "/whoami",
43 {
44 method: "GET",
45 credentials: params.credentials
46 }
47 )
48 .then(res => res.json())
49 .then(json => {
50 this.state.user.id = json.id;
51 const storedId = localStorage.getItem("myid");
52 if (json.id > 0 && !storedId)
53 // User cleared localStorage
54 localStorage.setItem("myid", json.id);
55 else if (json.id == 0 && !!storedId)
56 // User cleared cookie
57 localStorage.removeItem("myid");
58 this.state.user.name = json.name;
59 const storedName = localStorage.getItem("myname");
60 if (!!json.name && !storedName)
61 // User cleared localStorage
62 localStorage.setItem("myname", json.name);
63 else if (!json.name && !!storedName)
64 // User cleared cookie
65 localStorage.removeItem("myname");
66 this.state.user.email = json.email;
67 this.state.user.notify = json.notify;
68 });
69 // Settings initialized with values from localStorage
70 const getItemDefaultTrue = (item) => {
71 const value = localStorage.getItem(item);
72 if (!value) return true;
73 return value == "true";
74 };
75 this.state.settings = {
76 bcolor: localStorage.getItem("bcolor") || "lichess",
77 sound: getItemDefaultTrue("sound"),
78 hints: getItemDefaultTrue("hints"),
79 highlight: getItemDefaultTrue("highlight")
80 };
81 const supportedLangs = ["en", "es", "fr"];
82 const navLanguage = navigator.language.substr(0,2);
83 this.state.lang =
84 localStorage["lang"] ||
85 (supportedLangs.includes(navLanguage) ? navLanguage : "en");
86 this.setTranslations();
87 },
88 updateSetting: function(propName, value) {
89 this.state.settings[propName] = value;
90 localStorage.setItem(propName, value);
91 },
92 setTranslations: async function() {
93 // Import translations from "./translations/$lang.js"
94 const tModule = await import("@/translations/" + this.state.lang + ".js");
95 this.state.tr = tModule.translations;
96 },
97 setLanguage(lang) {
98 this.state.lang = lang;
99 this.setTranslations();
100 }
101 };