Commit | Line | Data |
---|---|---|
c66a829b BA |
1 | import { ajax } from "./utils/ajax"; |
2 | import { getRandString } from "./utils/alea"; | |
3 | import params from "./parameters"; //for socket connection | |
4 | ||
4486a21e | 5 | // Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87 |
c66a829b BA |
6 | export const store = |
7 | { | |
8 | state: { | |
9 | variants: [], | |
10 | tr: {}, | |
11 | user: {}, | |
12 | conn: null, | |
13 | settings: {}, | |
14 | lang: "", | |
15 | }, | |
cdb34c93 | 16 | socketCloseListener: null, |
5f131484 | 17 | initialize(page) { |
c66a829b | 18 | ajax("/variants", "GET", res => { this.state.variants = res.variantArray; }); |
98f48579 BA |
19 | let mysid = localStorage["mysid"]; |
20 | if (!mysid) | |
21 | { | |
22 | mysid = getRandString(); | |
23 | localStorage["mysid"] = mysid; //done only once (unless user clear browser data) | |
24 | } | |
5ea8d113 | 25 | // Quick user setup using local storage: |
c66a829b | 26 | this.state.user = { |
98f48579 BA |
27 | id: localStorage["myid"] || 0, |
28 | name: localStorage["myname"] || "", //"" for "anonymous" | |
a7f9f050 BA |
29 | email: "", //unknown yet |
30 | notify: false, //email notifications | |
98f48579 | 31 | sid: mysid, |
c66a829b | 32 | }; |
5ea8d113 BA |
33 | // Slow verification through the server: |
34 | // NOTE: still superficial identity usurpation possible, but difficult. | |
35 | ajax("/whoami", "GET", res => { | |
36 | this.state.user.id = res.id; | |
37 | this.state.user.name = res.name; | |
38 | this.state.user.email = res.email; | |
39 | this.state.user.notify = res.notify; | |
40 | }); | |
f41ce580 BA |
41 | this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid + |
42 | "&page=" + encodeURIComponent(page)); | |
c66a829b BA |
43 | // Settings initialized with values from localStorage |
44 | this.state.settings = { | |
dfeb96ea BA |
45 | bcolor: localStorage.getItem("bcolor") || "lichess", |
46 | sound: parseInt(localStorage.getItem("sound")) || 1, | |
47 | hints: localStorage.getItem("hints") == "true", | |
48 | highlight: localStorage.getItem("highlight") == "true", | |
c66a829b | 49 | }; |
cdb34c93 | 50 | this.socketCloseListener = () => { |
66d03f23 | 51 | // Next line may fail at first, but should retry and eventually success (TODO?) |
80ee5d5a BA |
52 | this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid + |
53 | "&page=" + encodeURIComponent(page)); | |
f6f2bef1 | 54 | }; |
cdb34c93 | 55 | this.state.conn.onclose = this.socketCloseListener; |
c66a829b BA |
56 | const supportedLangs = ["en","es","fr"]; |
57 | this.state.lang = localStorage["lang"] || | |
4f887105 | 58 | (supportedLangs.includes(navigator.language) |
c66a829b | 59 | ? navigator.language |
4f887105 | 60 | : "en"); |
c66a829b BA |
61 | this.setTranslations(); |
62 | }, | |
dfeb96ea BA |
63 | updateSetting: function(propName, value) { |
64 | this.state.settings[propName] = value; | |
65 | localStorage.setItem(propName, value); | |
66 | }, | |
c66a829b | 67 | setTranslations: async function() { |
7c3cd379 | 68 | // Import translations from "./translations/$lang.js" |
c66a829b BA |
69 | const tModule = await import("@/translations/" + this.state.lang + ".js"); |
70 | this.state.tr = tModule.translations; | |
71 | }, | |
72 | setLanguage(lang) { | |
73 | this.state.lang = lang; | |
74 | this.setTranslations(); | |
75 | }, | |
76 | }; |