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