Some small enhancements and fixes
[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 const headers = {
17 "Content-Type": "application/json;charset=UTF-8",
18 "X-Requested-With": "XMLHttpRequest"
19 };
20 fetch(
21 params.serverUrl + "/variants",
22 {
23 method: "GET",
24 headers: headers
25 }
26 )
27 .then(res => res.json())
28 .then(json => {
29 if (!Array.isArray(json.variantArray)) {
30 alert("Variants loading failed: reload the page");
31 return;
32 }
33 this.state.variants = json.variantArray.sort(
34 (v1,v2) => v1.name.localeCompare(v2.name));
35 });
36 let mysid = localStorage.getItem("mysid");
37 // Assign mysid only once (until next time user clear browser data)
38 if (!mysid) {
39 mysid = getRandString();
40 localStorage.setItem("mysid", mysid);
41 }
42 // Quick user setup using local storage:
43 this.state.user = {
44 id: localStorage.getItem("myid") || 0,
45 name: localStorage.getItem("myname") || "", //"" for "anonymous"
46 email: "", //unknown yet
47 notify: false, //email notifications
48 sid: mysid
49 };
50 // Slow verification through the server:
51 // NOTE: still superficial identity usurpation possible, but difficult.
52 fetch(
53 params.serverUrl + "/whoami",
54 {
55 method: "GET",
56 headers: headers,
57 credentials: params.credentials
58 }
59 )
60 .then(res => res.json())
61 .then(json => {
62 this.state.user.id = json.id;
63 const storedId = localStorage.getItem("myid");
64 if (json.id > 0 && !storedId)
65 // User cleared localStorage
66 localStorage.setItem("myid", json.id);
67 else if (json.id == 0 && !!storedId)
68 // User cleared cookie
69 localStorage.removeItem("myid");
70 this.state.user.name = json.name;
71 const storedName = localStorage.getItem("myname");
72 if (!!json.name && !storedName)
73 // User cleared localStorage
74 localStorage.setItem("myname", json.name);
75 else if (!json.name && !!storedName)
76 // User cleared cookie
77 localStorage.removeItem("myname");
78 this.state.user.email = json.email;
79 this.state.user.notify = json.notify;
80 });
81 // Settings initialized with values from localStorage
82 const getItemDefaultTrue = (item) => {
83 const value = localStorage.getItem(item);
84 if (!value) return true;
85 return value == "true";
86 };
87 this.state.settings = {
88 bcolor: localStorage.getItem("bcolor") || "lichess",
89 sound: getItemDefaultTrue("sound"),
90 hints: getItemDefaultTrue("hints"),
91 highlight: getItemDefaultTrue("highlight"),
92 gotonext: getItemDefaultTrue("gotonext"),
93 randomness: parseInt(localStorage.getItem("randomness"))
94 };
95 if (isNaN(this.state.settings.randomness))
96 // Default: random asymmetric
97 this.state.settings.randomness = 2;
98 const supportedLangs = ["en", "es", "fr"];
99 const navLanguage = navigator.language.substr(0,2);
100 this.state.lang =
101 localStorage["lang"] ||
102 (supportedLangs.includes(navLanguage) ? navLanguage : "en");
103 this.setTranslations();
104 },
105 updateSetting: function(propName, value) {
106 this.state.settings[propName] = value;
107 localStorage.setItem(propName, value);
108 },
109 setTranslations: async function() {
110 // Import translations from "./translations/$lang.js"
111 const tModule = await import("@/translations/" + this.state.lang + ".js");
112 this.state.tr = tModule.translations;
113 },
114 setLanguage(lang) {
115 this.state.lang = lang;
116 this.setTranslations();
117 }
118 };