Better challenge display: 'with' instead of 'from'
[vchess.git] / client / src / store.js
CommitLineData
c66a829b
BA
1import { ajax } from "./utils/ajax";
2import { getRandString } from "./utils/alea";
c66a829b 3
4486a21e 4// Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
6808d7a1 5export const store = {
c66a829b
BA
6 state: {
7 variants: [],
8 tr: {},
9 user: {},
c66a829b 10 settings: {},
6808d7a1 11 lang: ""
c66a829b 12 },
cdb34c93 13 socketCloseListener: null,
8418f0d7 14 initialize() {
6808d7a1
BA
15 ajax("/variants", "GET", res => {
16 this.state.variants = res.variantArray;
17 });
9ef63965 18 let mysid = localStorage.getItem("mysid");
910d631b 19 // Assign mysid only once (until next time user clear browser data)
6808d7a1 20 if (!mysid) {
98f48579 21 mysid = getRandString();
910d631b 22 localStorage.setItem("mysid", mysid);
98f48579 23 }
5ea8d113 24 // Quick user setup using local storage:
c66a829b 25 this.state.user = {
9ef63965
BA
26 id: localStorage.getItem("myid") || 0,
27 name: localStorage.getItem("myname") || "", //"" for "anonymous"
a7f9f050
BA
28 email: "", //unknown yet
29 notify: false, //email notifications
6808d7a1 30 sid: mysid
c66a829b 31 };
5ea8d113
BA
32 // Slow verification through the server:
33 // NOTE: still superficial identity usurpation possible, but difficult.
34 ajax("/whoami", "GET", res => {
35 this.state.user.id = res.id;
9ef63965 36 const storedId = localStorage.getItem("myid");
6808d7a1 37 if (res.id > 0 && !storedId)
910d631b 38 // User cleared localStorage
9ef63965 39 localStorage.setItem("myid", res.id);
6808d7a1 40 else if (res.id == 0 && !!storedId)
910d631b 41 // User cleared cookie
9ef63965 42 localStorage.removeItem("myid");
5ea8d113 43 this.state.user.name = res.name;
9ef63965 44 const storedName = localStorage.getItem("myname");
6808d7a1 45 if (!!res.name && !storedName)
910d631b 46 // User cleared localStorage
9ef63965 47 localStorage.setItem("myname", res.name);
6808d7a1 48 else if (!res.name && !!storedName)
910d631b 49 // User cleared cookie
9ef63965 50 localStorage.removeItem("myname");
5ea8d113
BA
51 this.state.user.email = res.email;
52 this.state.user.notify = res.notify;
53 });
c66a829b
BA
54 // Settings initialized with values from localStorage
55 this.state.settings = {
dfeb96ea
BA
56 bcolor: localStorage.getItem("bcolor") || "lichess",
57 sound: parseInt(localStorage.getItem("sound")) || 1,
58 hints: localStorage.getItem("hints") == "true",
6808d7a1 59 highlight: localStorage.getItem("highlight") == "true"
c66a829b 60 };
6808d7a1
BA
61 const supportedLangs = ["en", "es", "fr"];
62 this.state.lang =
63 localStorage["lang"] ||
64 (supportedLangs.includes(navigator.language) ? navigator.language : "en");
8418f0d7 65 this.setTranslations();
c66a829b 66 },
dfeb96ea
BA
67 updateSetting: function(propName, value) {
68 this.state.settings[propName] = value;
69 localStorage.setItem(propName, value);
70 },
c66a829b 71 setTranslations: async function() {
7c3cd379 72 // Import translations from "./translations/$lang.js"
c66a829b
BA
73 const tModule = await import("@/translations/" + this.state.lang + ".js");
74 this.state.tr = tModule.translations;
75 },
76 setLanguage(lang) {
77 this.state.lang = lang;
78 this.setTranslations();
6808d7a1 79 }
c66a829b 80};