Fix last update
[vchess.git] / client / src / store.js
CommitLineData
2c5d7b20 1// NOTE: do not use ajax() here because ajax.js requires the store
f0c68a04 2import params from "./parameters"; //for server URL
c66a829b 3import { getRandString } from "./utils/alea";
a5200af9 4import { delCookie } from "./utils/cookie";
c66a829b 5
2c5d7b20
BA
6// Global store: see
7// https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
6808d7a1 8export const store = {
c66a829b
BA
9 state: {
10 variants: [],
11 tr: {},
12 user: {},
c66a829b 13 settings: {},
6808d7a1 14 lang: ""
c66a829b 15 },
8418f0d7 16 initialize() {
e57c4de4
BA
17 const headers = {
18 "Content-Type": "application/json;charset=UTF-8",
19 "X-Requested-With": "XMLHttpRequest"
20 };
f0c68a04
BA
21 fetch(
22 params.serverUrl + "/variants",
e57c4de4
BA
23 {
24 method: "GET",
25 headers: headers
26 }
f0c68a04
BA
27 )
28 .then(res => res.json())
29 .then(json => {
e57c4de4
BA
30 if (!Array.isArray(json.variantArray)) {
31 alert("Variants loading failed: reload the page");
32 return;
33 }
31ccd7e3 34 this.state.variants = json.variantArray
31ccd7e3 35 .sort((v1,v2) => v1.name.localeCompare(v2.name));
6808d7a1 36 });
9ef63965 37 let mysid = localStorage.getItem("mysid");
910d631b 38 // Assign mysid only once (until next time user clear browser data)
6808d7a1 39 if (!mysid) {
98f48579 40 mysid = getRandString();
910d631b 41 localStorage.setItem("mysid", mysid);
98f48579 42 }
5ea8d113 43 // Quick user setup using local storage:
c66a829b 44 this.state.user = {
f4fce337 45 id: parseInt(localStorage.getItem("myid") || "0", 10),
9ef63965 46 name: localStorage.getItem("myname") || "", //"" for "anonymous"
a7f9f050
BA
47 email: "", //unknown yet
48 notify: false, //email notifications
6808d7a1 49 sid: mysid
c66a829b 50 };
5ea8d113 51 // Slow verification through the server:
f0c68a04
BA
52 fetch(
53 params.serverUrl + "/whoami",
54 {
55 method: "GET",
e57c4de4 56 headers: headers,
f0c68a04
BA
57 credentials: params.credentials
58 }
59 )
60 .then(res => res.json())
61 .then(json => {
a5200af9
BA
62 if (!json.id) {
63 // Removed, or wrong token
64 if (this.state.user.id > 0) {
65 this.state.user.id = 0;
66 localStorage.removeItem("myid");
67 }
68 if (!!this.state.user.name) {
69 this.state.user.name = "";
70 localStorage.removeItem("myname");
71 }
72 if (document.cookie.indexOf("token") >= 0) delCookie("token");
73 }
74 else {
75 if (this.state.user.id != json.id) {
76 this.state.user.id = json.id;
77 localStorage.setItem("myid", json.id);
78 }
79 if (this.state.user.name != json.name) {
80 this.state.user.name = json.name;
81 localStorage.setItem("myname", json.name);
82 }
83 this.state.user.email = json.email;
84 this.state.user.notify = json.notify;
85 }
5ea8d113 86 });
c66a829b 87 // Settings initialized with values from localStorage
4313762d 88 const getItemDefault = (item, defaut) => {
db1f1f9a 89 const value = localStorage.getItem(item);
4313762d 90 if (!value) return defaut;
db1f1f9a
BA
91 return value == "true";
92 };
c66a829b 93 this.state.settings = {
dfeb96ea 94 bcolor: localStorage.getItem("bcolor") || "lichess",
4313762d
BA
95 sound: getItemDefault("sound", true),
96 hints: getItemDefault("hints", true),
97 highlight: getItemDefault("highlight", true),
98 gotonext: getItemDefault("gotonext", true),
99 scrollmove: getItemDefault("scrollmove", false)
c66a829b 100 };
6808d7a1 101 const supportedLangs = ["en", "es", "fr"];
58aedcd1 102 const navLanguage = navigator.language.substr(0, 2);
6808d7a1
BA
103 this.state.lang =
104 localStorage["lang"] ||
dfa6eb76 105 (supportedLangs.includes(navLanguage) ? navLanguage : "en");
8418f0d7 106 this.setTranslations();
c66a829b 107 },
dfeb96ea
BA
108 updateSetting: function(propName, value) {
109 this.state.settings[propName] = value;
110 localStorage.setItem(propName, value);
111 },
c66a829b 112 setTranslations: async function() {
7c3cd379 113 // Import translations from "./translations/$lang.js"
c66a829b
BA
114 const tModule = await import("@/translations/" + this.state.lang + ".js");
115 this.state.tr = tModule.translations;
116 },
117 setLanguage(lang) {
118 this.state.lang = lang;
119 this.setTranslations();
6808d7a1 120 }
c66a829b 121};