Started code review + some fixes (unfinished)
[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");
6808d7a1 19 if (!mysid) {
98f48579 20 mysid = getRandString();
9ef63965 21 localStorage.setItem("mysid", mysid); //done only once (unless user clear browser data)
98f48579 22 }
5ea8d113 23 // Quick user setup using local storage:
c66a829b 24 this.state.user = {
9ef63965
BA
25 id: localStorage.getItem("myid") || 0,
26 name: localStorage.getItem("myname") || "", //"" for "anonymous"
a7f9f050
BA
27 email: "", //unknown yet
28 notify: false, //email notifications
6808d7a1 29 sid: mysid
c66a829b 30 };
5ea8d113
BA
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;
9ef63965 35 const storedId = localStorage.getItem("myid");
6808d7a1
BA
36 if (res.id > 0 && !storedId)
37 //user cleared localStorage
9ef63965 38 localStorage.setItem("myid", res.id);
6808d7a1
BA
39 else if (res.id == 0 && !!storedId)
40 //user cleared cookie
9ef63965 41 localStorage.removeItem("myid");
5ea8d113 42 this.state.user.name = res.name;
9ef63965 43 const storedName = localStorage.getItem("myname");
6808d7a1
BA
44 if (!!res.name && !storedName)
45 //user cleared localStorage
9ef63965 46 localStorage.setItem("myname", res.name);
6808d7a1
BA
47 else if (!res.name && !!storedName)
48 //user cleared cookie
9ef63965 49 localStorage.removeItem("myname");
5ea8d113
BA
50 this.state.user.email = res.email;
51 this.state.user.notify = res.notify;
52 });
c66a829b
BA
53 // Settings initialized with values from localStorage
54 this.state.settings = {
dfeb96ea
BA
55 bcolor: localStorage.getItem("bcolor") || "lichess",
56 sound: parseInt(localStorage.getItem("sound")) || 1,
57 hints: localStorage.getItem("hints") == "true",
6808d7a1 58 highlight: localStorage.getItem("highlight") == "true"
c66a829b 59 };
6808d7a1
BA
60 const supportedLangs = ["en", "es", "fr"];
61 this.state.lang =
62 localStorage["lang"] ||
63 (supportedLangs.includes(navigator.language) ? navigator.language : "en");
8418f0d7 64 this.setTranslations();
c66a829b 65 },
dfeb96ea
BA
66 updateSetting: function(propName, value) {
67 this.state.settings[propName] = value;
68 localStorage.setItem(propName, value);
69 },
c66a829b 70 setTranslations: async function() {
7c3cd379 71 // Import translations from "./translations/$lang.js"
c66a829b
BA
72 const tModule = await import("@/translations/" + this.state.lang + ".js");
73 this.state.tr = tModule.translations;
74 },
75 setLanguage(lang) {
76 this.state.lang = lang;
77 this.setTranslations();
6808d7a1 78 }
c66a829b 79};