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 | ||
5 | export const store = | |
6 | { | |
7 | state: { | |
8 | variants: [], | |
9 | tr: {}, | |
10 | user: {}, | |
11 | conn: null, | |
12 | settings: {}, | |
13 | lang: "", | |
14 | }, | |
15 | initialize() { | |
16 | ajax("/variants", "GET", res => { this.state.variants = res.variantArray; }); | |
98f48579 BA |
17 | let mysid = localStorage["mysid"]; |
18 | if (!mysid) | |
19 | { | |
20 | mysid = getRandString(); | |
21 | localStorage["mysid"] = mysid; //done only once (unless user clear browser data) | |
22 | } | |
c66a829b | 23 | this.state.user = { |
98f48579 BA |
24 | id: localStorage["myid"] || 0, |
25 | name: localStorage["myname"] || "", //"" for "anonymous" | |
a7f9f050 BA |
26 | email: "", //unknown yet |
27 | notify: false, //email notifications | |
98f48579 | 28 | sid: mysid, |
c66a829b | 29 | }; |
98f48579 BA |
30 | if (this.state.user.id > 0) |
31 | { | |
32 | fetch(params.serverUrl + "/whoami", { | |
33 | method: "GET", | |
34 | credentials: params.cors ? "include" : "omit", | |
35 | }).then((res) => { | |
a7f9f050 BA |
36 | this.state.user.email = res.email; |
37 | this.state.user.notify = res.notify; | |
98f48579 BA |
38 | }); |
39 | } | |
03608482 | 40 | this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + this.state.user.sid); |
c66a829b BA |
41 | // Settings initialized with values from localStorage |
42 | this.state.settings = { | |
43 | bcolor: localStorage["bcolor"] || "lichess", | |
44 | sound: parseInt(localStorage["sound"]) || 2, | |
45 | hints: parseInt(localStorage["hints"]) || 1, | |
46 | coords: !!eval(localStorage["coords"]), | |
47 | highlight: !!eval(localStorage["highlight"]), | |
48 | sqSize: parseInt(localStorage["sqSize"]), | |
49 | }; | |
50 | const socketCloseListener = () => { | |
51 | this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid); | |
52 | } | |
53 | this.state.conn.onclose = socketCloseListener; | |
54 | const supportedLangs = ["en","es","fr"]; | |
55 | this.state.lang = localStorage["lang"] || | |
56 | supportedLangs.includes(navigator.language) | |
57 | ? navigator.language | |
58 | : "en"; | |
59 | this.setTranslations(); | |
60 | }, | |
61 | setTranslations: async function() { | |
7c3cd379 | 62 | // Import translations from "./translations/$lang.js" |
c66a829b BA |
63 | const tModule = await import("@/translations/" + this.state.lang + ".js"); |
64 | this.state.tr = tModule.translations; | |
65 | }, | |
66 | setLanguage(lang) { | |
67 | this.state.lang = lang; | |
68 | this.setTranslations(); | |
69 | }, | |
70 | }; |