diff --git a/src/PageState.js b/src/PageState.js index 9c5baa7..e539310 100644 --- a/src/PageState.js +++ b/src/PageState.js @@ -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') || ""; } diff --git a/src/formatDistance.js b/src/formatDistance.js index bcbc065..60bdef9 100644 --- a/src/formatDistance.js +++ b/src/formatDistance.js @@ -9,7 +9,7 @@ function getLocale() { "ru": ru, "de": de, "it": it, - }[localStorage.getItem("language") || "en"]; + }[pageState.language]; } export function formatDistance(d1, d2) { diff --git a/src/main.js b/src/main.js index a24ffe6..ad81fd5 100644 --- a/src/main.js +++ b/src/main.js @@ -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, _) { diff --git a/src/pages/Home.js b/src/pages/Home.js index 58bd0fb..90f6a17 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -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"); } } diff --git a/src/pages/Login.js b/src/pages/Login.js index 01ab679..5889667 100644 --- a/src/pages/Login.js +++ b/src/pages/Login.js @@ -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"); diff --git a/src/pages/Me.js b/src/pages/Me.js index 50f5eb8..5eb73cc 100644 --- a/src/pages/Me.js +++ b/src/pages/Me.js @@ -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"); } }) diff --git a/src/pages/SetLanguage.js b/src/pages/SetLanguage.js index 447552c..f9c9f3b 100644 --- a/src/pages/SetLanguage.js +++ b/src/pages/SetLanguage.js @@ -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(); diff --git a/src/pages/SetVaultURL.js b/src/pages/SetVaultURL.js index ae7ac8b..07532b3 100644 --- a/src/pages/SetVaultURL.js +++ b/src/pages/SetVaultURL.js @@ -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"); }); } diff --git a/src/utils.js b/src/utils.js index 6ad541c..94d977e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 } \ No newline at end of file