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