Experimental update: preview corr move + allow deletion of any game
[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 gotonext: getItemDefaultTrue("gotonext"),
81 randomness: parseInt(localStorage.getItem("randomness"))
82 };
83 if (isNaN(this.state.settings.randomness))
84 // Default: random asymmetric
85 this.state.settings.randomness = 2;
86 const supportedLangs = ["en", "es", "fr"];
87 const navLanguage = navigator.language.substr(0,2);
88 this.state.lang =
89 localStorage["lang"] ||
90 (supportedLangs.includes(navLanguage) ? navLanguage : "en");
91 this.setTranslations();
92 },
93 updateSetting: function(propName, value) {
94 this.state.settings[propName] = value;
95 localStorage.setItem(propName, value);
96 },
97 setTranslations: async function() {
98 // Import translations from "./translations/$lang.js"
99 const tModule = await import("@/translations/" + this.state.lang + ".js");
100 this.state.tr = tModule.translations;
101 },
102 setLanguage(lang) {
103 this.state.lang = lang;
104 this.setTranslations();
105 }
106 };