Move localStorage operations into pageState.
This commit is contained in:
parent
ed72d3bff9
commit
be977af775
|
@ -18,6 +18,27 @@ export class PageState extends Page {
|
|||
// by using a bunch of functions and modifying localStorage in order to remove some of
|
||||
// the clunkyness of this approach, but for now, this works.
|
||||
|
||||
get apiURL() {
|
||||
return localStorage.getItem('apiURL') || "";
|
||||
}
|
||||
set apiURL(value) {
|
||||
localStorage.setItem('apiURL', value);
|
||||
}
|
||||
|
||||
get token() {
|
||||
return localStorage.getItem('token') || "";
|
||||
}
|
||||
set token(value) {
|
||||
localStorage.setItem('token', value);
|
||||
}
|
||||
|
||||
get language() {
|
||||
return localStorage.getItem('language') || "";
|
||||
}
|
||||
set language(value) {
|
||||
localStorage.setItem('language', value);
|
||||
}
|
||||
|
||||
get currentBaseMount() {
|
||||
return localStorage.getItem('currentBaseMount') || "";
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ function getLocale() {
|
|||
"ru": ru,
|
||||
"de": de,
|
||||
"it": it,
|
||||
}[localStorage.getItem("language") || "en"];
|
||||
}[pageState.language];
|
||||
}
|
||||
|
||||
export function formatDistance(d1, d2) {
|
||||
|
|
|
@ -171,7 +171,7 @@ function onLoad() {
|
|||
|
||||
setInterval(async () => {
|
||||
if (pageState.currentPageString != "UNSEAL") {
|
||||
if (!localStorage.getItem('apiurl')) { return; }
|
||||
if (pageState.apiURL.length != 0) { return; }
|
||||
let sealStatus = await getSealStatus();
|
||||
if (sealStatus.sealed) {
|
||||
changePage("UNSEAL");
|
||||
|
@ -183,7 +183,7 @@ function onLoad() {
|
|||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
i18next.init({
|
||||
lng: localStorage.getItem("language") || "en",
|
||||
lng: pageState.language,
|
||||
fallbackLng: 'en',
|
||||
debug: true,
|
||||
resources: {
|
||||
|
@ -192,7 +192,6 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||
ru: { translation: translation_ru },
|
||||
nl: { translation: translation_nl },
|
||||
fr: { translation: translation_fr },
|
||||
|
||||
},
|
||||
interpolation: {
|
||||
format: function (value, format, _) {
|
||||
|
|
|
@ -11,7 +11,7 @@ export class HomePage extends Page {
|
|||
}
|
||||
async render() {
|
||||
pageContent.innerHTML = "";
|
||||
if (!localStorage.getItem("language")) {
|
||||
if (pageState.language.length == 0) {
|
||||
changePage("SET_LANGUAGE");
|
||||
return;
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ export class HomePage extends Page {
|
|||
return;
|
||||
}
|
||||
if (!getToken()) {
|
||||
localStorage.removeItem("token");
|
||||
pageState.token = "";
|
||||
changePage("LOGIN");
|
||||
return;
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ export class HomePage extends Page {
|
|||
} catch (e) {
|
||||
setErrorText(e.message);
|
||||
if (e.message == "permission denied") {
|
||||
localStorage.removeItem("token");
|
||||
pageState.token = "";
|
||||
changePage("LOGIN");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -122,7 +122,7 @@ export class LoginPage extends Page {
|
|||
e.preventDefault();
|
||||
let formData = new FormData(tokenLoginForm);
|
||||
const token = formData.get("token");
|
||||
localStorage.setItem('token', token);
|
||||
pageState.token = token;
|
||||
lookupSelf().then(_ => {
|
||||
changePage("HOME");
|
||||
}).catch(e => {
|
||||
|
@ -134,7 +134,7 @@ export class LoginPage extends Page {
|
|||
e.preventDefault();
|
||||
let formData = new FormData(usernameLoginForm);
|
||||
usernameLogin(formData.get("username"), formData.get("password")).then(res => {
|
||||
localStorage.setItem("token", res);
|
||||
pageState.token = res;
|
||||
changePage("HOME");
|
||||
}).catch(e => {
|
||||
document.getElementById("usernameInput").classList.add("uk-form-danger");
|
||||
|
|
|
@ -23,7 +23,7 @@ export class MePage extends Page {
|
|||
tag: "a",
|
||||
text: i18next.t("log_out_btn"),
|
||||
onclick: () => {
|
||||
localStorage.removeItem("token");
|
||||
pageState.token = "";
|
||||
changePage("HOME");
|
||||
}
|
||||
})
|
||||
|
|
|
@ -50,7 +50,8 @@ export class SetLanguagePage extends Page {
|
|||
e.preventDefault();
|
||||
let formData = new FormData(setLanguageForm);
|
||||
let language = formData.get("language");
|
||||
localStorage.setItem('language', language);
|
||||
pageState.language = language;
|
||||
console.log(pageState.language);
|
||||
i18next.changeLanguage(language).then((_) => {
|
||||
changePage("HOME", false);
|
||||
location.reload();
|
||||
|
|
|
@ -43,7 +43,7 @@ export class SetVaultURLPage extends Page {
|
|||
document.getElementById("setVaultURLForm").addEventListener("submit", function (e) {
|
||||
e.preventDefault();
|
||||
let formData = new FormData(document.querySelector('#setVaultURLForm'));
|
||||
localStorage.setItem('apiurl', formData.get("vaultURL"));
|
||||
pageState.apiURL = formData.get("vaultURL");
|
||||
changePage("HOME");
|
||||
});
|
||||
}
|
||||
|
|
|
@ -19,9 +19,9 @@ export function verifyJSONString(str) {
|
|||
}
|
||||
|
||||
export function getToken() {
|
||||
return localStorage.getItem('token');
|
||||
return pageState.token.length > 0 ? pageState.token : null;
|
||||
}
|
||||
|
||||
export function getAPIURL() {
|
||||
return localStorage.getItem('apiurl');
|
||||
return pageState.apiURL.length > 0 ? pageState.apiURL : null
|
||||
}
|
Loading…
Reference in a new issue