| 1 | import { ajax } from "./utils/ajax"; |
| 2 | import { getRandString } from "./utils/alea"; |
| 3 | |
| 4 | // Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87 |
| 5 | export const store = |
| 6 | { |
| 7 | state: { |
| 8 | variants: [], |
| 9 | tr: {}, |
| 10 | user: {}, |
| 11 | settings: {}, |
| 12 | lang: "", |
| 13 | }, |
| 14 | socketCloseListener: null, |
| 15 | initialize() { |
| 16 | ajax("/variants", "GET", res => { this.state.variants = res.variantArray; }); |
| 17 | let mysid = localStorage.getItem("mysid"); |
| 18 | if (!mysid) |
| 19 | { |
| 20 | mysid = getRandString(); |
| 21 | localStorage.setItem("mysid", mysid); //done only once (unless user clear browser data) |
| 22 | } |
| 23 | // Quick user setup using local storage: |
| 24 | this.state.user = { |
| 25 | id: localStorage.getItem("myid") || 0, |
| 26 | name: localStorage.getItem("myname") || "", //"" for "anonymous" |
| 27 | email: "", //unknown yet |
| 28 | notify: false, //email notifications |
| 29 | sid: mysid, |
| 30 | }; |
| 31 | // Slow verification through the server: |
| 32 | // NOTE: still superficial identity usurpation possible, but difficult. |
| 33 | ajax("/whoami", "GET", res => { |
| 34 | this.state.user.id = res.id; |
| 35 | const storedId = localStorage.getItem("myid"); |
| 36 | if (res.id > 0 && !storedId) //user cleared localStorage |
| 37 | localStorage.setItem("myid", res.id); |
| 38 | else if (res.id == 0 && !!storedId) //user cleared cookie |
| 39 | localStorage.removeItem("myid"); |
| 40 | this.state.user.name = res.name; |
| 41 | const storedName = localStorage.getItem("myname"); |
| 42 | if (!!res.name && !storedName) //user cleared localStorage |
| 43 | localStorage.setItem("myname", res.name); |
| 44 | else if (!res.name && !!storedName) //user cleared cookie |
| 45 | localStorage.removeItem("myname"); |
| 46 | this.state.user.email = res.email; |
| 47 | this.state.user.notify = res.notify; |
| 48 | }); |
| 49 | // Settings initialized with values from localStorage |
| 50 | this.state.settings = { |
| 51 | bcolor: localStorage.getItem("bcolor") || "lichess", |
| 52 | sound: parseInt(localStorage.getItem("sound")) || 1, |
| 53 | hints: localStorage.getItem("hints") == "true", |
| 54 | highlight: localStorage.getItem("highlight") == "true", |
| 55 | }; |
| 56 | const supportedLangs = ["en","es","fr"]; |
| 57 | this.state.lang = localStorage["lang"] || |
| 58 | (supportedLangs.includes(navigator.language) |
| 59 | ? navigator.language |
| 60 | : "en"); |
| 61 | this.setTranslations(); |
| 62 | }, |
| 63 | updateSetting: function(propName, value) { |
| 64 | this.state.settings[propName] = value; |
| 65 | localStorage.setItem(propName, value); |
| 66 | }, |
| 67 | setTranslations: async function() { |
| 68 | // Import translations from "./translations/$lang.js" |
| 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 | }; |