1 // NOTE: do not use ajax() here because ajax.js requires the store
2 import params
from "./parameters"; //for server URL
3 import { getRandString
} from "./utils/alea";
6 // https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
15 socketCloseListener: null,
18 "Content-Type": "application/json;charset=UTF-8",
19 "X-Requested-With": "XMLHttpRequest"
22 params
.serverUrl
+ "/variants",
28 .then(res
=> res
.json())
30 if (!Array
.isArray(json
.variantArray
)) {
31 alert("Variants loading failed: reload the page");
34 this.state
.variants
= json
.variantArray
35 .sort((v1
,v2
) => v1
.name
.localeCompare(v2
.name
));
37 let mysid
= localStorage
.getItem("mysid");
38 // Assign mysid only once (until next time user clear browser data)
40 mysid
= getRandString();
41 localStorage
.setItem("mysid", mysid
);
43 // Quick user setup using local storage:
45 id: localStorage
.getItem("myid") || 0,
46 name: localStorage
.getItem("myname") || "", //"" for "anonymous"
47 email: "", //unknown yet
48 notify: false, //email notifications
49 newsRead: localStorage
.getItem("newsRead") || 0,
52 // Slow verification through the server:
53 // NOTE: still superficial identity usurpation possible, but difficult.
55 params
.serverUrl
+ "/whoami",
59 credentials: params
.credentials
62 .then(res
=> res
.json())
64 this.state
.user
.id
= json
.id
;
65 const storedId
= localStorage
.getItem("myid");
66 if (json
.id
> 0 && !storedId
)
67 // User cleared localStorage
68 localStorage
.setItem("myid", json
.id
);
69 else if (json
.id
== 0 && !!storedId
)
70 // User cleared cookie
71 localStorage
.removeItem("myid");
72 this.state
.user
.name
= json
.name
;
73 const storedName
= localStorage
.getItem("myname");
74 if (!!json
.name
&& !storedName
)
75 // User cleared localStorage
76 localStorage
.setItem("myname", json
.name
);
77 else if (!json
.name
&& !!storedName
)
78 // User cleared cookie
79 localStorage
.removeItem("myname");
80 this.state
.user
.email
= json
.email
;
81 this.state
.user
.notify
= json
.notify
;
82 if (!!json
.newsRead
&& json
.newsRead
> this.state
.user
.newsRead
)
83 this.state
.user
.newsRead
= json
.newsRead
;
85 // Settings initialized with values from localStorage
86 const getItemDefaultTrue
= (item
) => {
87 const value
= localStorage
.getItem(item
);
88 if (!value
) return true;
89 return value
== "true";
91 this.state
.settings
= {
92 bcolor: localStorage
.getItem("bcolor") || "lichess",
93 sound: getItemDefaultTrue("sound"),
94 hints: getItemDefaultTrue("hints"),
95 highlight: getItemDefaultTrue("highlight"),
96 gotonext: getItemDefaultTrue("gotonext"),
97 randomness: parseInt(localStorage
.getItem("randomness"))
99 if (isNaN(this.state
.settings
.randomness
))
100 // Default: random asymmetric
101 this.state
.settings
.randomness
= 2;
102 const supportedLangs
= ["en", "es", "fr"];
103 const navLanguage
= navigator
.language
.substr(0,2);
105 localStorage
["lang"] ||
106 (supportedLangs
.includes(navLanguage
) ? navLanguage : "en");
107 this.setTranslations();
109 updateSetting: function(propName
, value
) {
110 this.state
.settings
[propName
] = value
;
111 localStorage
.setItem(propName
, value
);
113 setTranslations: async
function() {
114 // Import translations from "./translations/$lang.js"
115 const tModule
= await
import("@/translations/" + this.state
.lang
+ ".js");
116 this.state
.tr
= tModule
.translations
;
119 this.state
.lang
= lang
;
120 this.setTranslations();