Draft Hiddenqueen, Grasshopper and Knightmate chess (rules unwritten)
[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 15 ajax("/variants", "GET", res => {
e9b0b504
BA
16 this.state.variants = res.variantArray.sort(
17 (v1,v2) => v1.name.localeCompare(v2.name));
6808d7a1 18 });
9ef63965 19 let mysid = localStorage.getItem("mysid");
910d631b 20 // Assign mysid only once (until next time user clear browser data)
6808d7a1 21 if (!mysid) {
98f48579 22 mysid = getRandString();
910d631b 23 localStorage.setItem("mysid", mysid);
98f48579 24 }
5ea8d113 25 // Quick user setup using local storage:
c66a829b 26 this.state.user = {
9ef63965
BA
27 id: localStorage.getItem("myid") || 0,
28 name: localStorage.getItem("myname") || "", //"" for "anonymous"
a7f9f050
BA
29 email: "", //unknown yet
30 notify: false, //email notifications
6808d7a1 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;
9ef63965 37 const storedId = localStorage.getItem("myid");
6808d7a1 38 if (res.id > 0 && !storedId)
910d631b 39 // User cleared localStorage
9ef63965 40 localStorage.setItem("myid", res.id);
6808d7a1 41 else if (res.id == 0 && !!storedId)
910d631b 42 // User cleared cookie
9ef63965 43 localStorage.removeItem("myid");
5ea8d113 44 this.state.user.name = res.name;
9ef63965 45 const storedName = localStorage.getItem("myname");
6808d7a1 46 if (!!res.name && !storedName)
910d631b 47 // User cleared localStorage
9ef63965 48 localStorage.setItem("myname", res.name);
6808d7a1 49 else if (!res.name && !!storedName)
910d631b 50 // User cleared cookie
9ef63965 51 localStorage.removeItem("myname");
5ea8d113
BA
52 this.state.user.email = res.email;
53 this.state.user.notify = res.notify;
54 });
c66a829b 55 // Settings initialized with values from localStorage
db1f1f9a
BA
56 const getItemDefaultTrue = (item) => {
57 const value = localStorage.getItem(item);
58 if (!value) return true;
59 return value == "true";
60 };
c66a829b 61 this.state.settings = {
dfeb96ea 62 bcolor: localStorage.getItem("bcolor") || "lichess",
db1f1f9a
BA
63 sound: getItemDefaultTrue("sound"),
64 hints: getItemDefaultTrue("hints"),
65 highlight: getItemDefaultTrue("highlight")
c66a829b 66 };
6808d7a1 67 const supportedLangs = ["en", "es", "fr"];
dfa6eb76 68 const navLanguage = navigator.language.substr(0,2);
6808d7a1
BA
69 this.state.lang =
70 localStorage["lang"] ||
dfa6eb76 71 (supportedLangs.includes(navLanguage) ? navLanguage : "en");
8418f0d7 72 this.setTranslations();
c66a829b 73 },
dfeb96ea
BA
74 updateSetting: function(propName, value) {
75 this.state.settings[propName] = value;
76 localStorage.setItem(propName, value);
77 },
c66a829b 78 setTranslations: async function() {
7c3cd379 79 // Import translations from "./translations/$lang.js"
c66a829b
BA
80 const tModule = await import("@/translations/" + this.state.lang + ".js");
81 this.state.tr = tModule.translations;
82 },
83 setLanguage(lang) {
84 this.state.lang = lang;
85 this.setTranslations();
6808d7a1 86 }
c66a829b 87};