1
0
Fork 0

Move localStorage operations into pageState.

This commit is contained in:
Kitteh 2021-04-30 10:38:23 +01:00
parent ed72d3bff9
commit be977af775
9 changed files with 35 additions and 14 deletions

View file

@ -18,6 +18,27 @@ export class PageState extends Page {
// by using a bunch of functions and modifying localStorage in order to remove some of // 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. // 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() { get currentBaseMount() {
return localStorage.getItem('currentBaseMount') || ""; return localStorage.getItem('currentBaseMount') || "";
} }

View file

@ -9,7 +9,7 @@ function getLocale() {
"ru": ru, "ru": ru,
"de": de, "de": de,
"it": it, "it": it,
}[localStorage.getItem("language") || "en"]; }[pageState.language];
} }
export function formatDistance(d1, d2) { export function formatDistance(d1, d2) {

View file

@ -171,7 +171,7 @@ function onLoad() {
setInterval(async () => { setInterval(async () => {
if (pageState.currentPageString != "UNSEAL") { if (pageState.currentPageString != "UNSEAL") {
if (!localStorage.getItem('apiurl')) { return; } if (pageState.apiURL.length != 0) { return; }
let sealStatus = await getSealStatus(); let sealStatus = await getSealStatus();
if (sealStatus.sealed) { if (sealStatus.sealed) {
changePage("UNSEAL"); changePage("UNSEAL");
@ -183,7 +183,7 @@ function onLoad() {
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {
i18next.init({ i18next.init({
lng: localStorage.getItem("language") || "en", lng: pageState.language,
fallbackLng: 'en', fallbackLng: 'en',
debug: true, debug: true,
resources: { resources: {
@ -192,7 +192,6 @@ document.addEventListener('DOMContentLoaded', function () {
ru: { translation: translation_ru }, ru: { translation: translation_ru },
nl: { translation: translation_nl }, nl: { translation: translation_nl },
fr: { translation: translation_fr }, fr: { translation: translation_fr },
}, },
interpolation: { interpolation: {
format: function (value, format, _) { format: function (value, format, _) {

View file

@ -11,7 +11,7 @@ export class HomePage extends Page {
} }
async render() { async render() {
pageContent.innerHTML = ""; pageContent.innerHTML = "";
if (!localStorage.getItem("language")) { if (pageState.language.length == 0) {
changePage("SET_LANGUAGE"); changePage("SET_LANGUAGE");
return; return;
} }
@ -20,7 +20,7 @@ export class HomePage extends Page {
return; return;
} }
if (!getToken()) { if (!getToken()) {
localStorage.removeItem("token"); pageState.token = "";
changePage("LOGIN"); changePage("LOGIN");
return; return;
} }
@ -65,7 +65,7 @@ export class HomePage extends Page {
} catch (e) { } catch (e) {
setErrorText(e.message); setErrorText(e.message);
if (e.message == "permission denied") { if (e.message == "permission denied") {
localStorage.removeItem("token"); pageState.token = "";
changePage("LOGIN"); changePage("LOGIN");
} }
} }

View file

@ -122,7 +122,7 @@ export class LoginPage extends Page {
e.preventDefault(); e.preventDefault();
let formData = new FormData(tokenLoginForm); let formData = new FormData(tokenLoginForm);
const token = formData.get("token"); const token = formData.get("token");
localStorage.setItem('token', token); pageState.token = token;
lookupSelf().then(_ => { lookupSelf().then(_ => {
changePage("HOME"); changePage("HOME");
}).catch(e => { }).catch(e => {
@ -134,7 +134,7 @@ export class LoginPage extends Page {
e.preventDefault(); e.preventDefault();
let formData = new FormData(usernameLoginForm); let formData = new FormData(usernameLoginForm);
usernameLogin(formData.get("username"), formData.get("password")).then(res => { usernameLogin(formData.get("username"), formData.get("password")).then(res => {
localStorage.setItem("token", res); pageState.token = res;
changePage("HOME"); changePage("HOME");
}).catch(e => { }).catch(e => {
document.getElementById("usernameInput").classList.add("uk-form-danger"); document.getElementById("usernameInput").classList.add("uk-form-danger");

View file

@ -23,7 +23,7 @@ export class MePage extends Page {
tag: "a", tag: "a",
text: i18next.t("log_out_btn"), text: i18next.t("log_out_btn"),
onclick: () => { onclick: () => {
localStorage.removeItem("token"); pageState.token = "";
changePage("HOME"); changePage("HOME");
} }
}) })

View file

@ -50,7 +50,8 @@ export class SetLanguagePage extends Page {
e.preventDefault(); e.preventDefault();
let formData = new FormData(setLanguageForm); let formData = new FormData(setLanguageForm);
let language = formData.get("language"); let language = formData.get("language");
localStorage.setItem('language', language); pageState.language = language;
console.log(pageState.language);
i18next.changeLanguage(language).then((_) => { i18next.changeLanguage(language).then((_) => {
changePage("HOME", false); changePage("HOME", false);
location.reload(); location.reload();

View file

@ -43,7 +43,7 @@ export class SetVaultURLPage extends Page {
document.getElementById("setVaultURLForm").addEventListener("submit", function (e) { document.getElementById("setVaultURLForm").addEventListener("submit", function (e) {
e.preventDefault(); e.preventDefault();
let formData = new FormData(document.querySelector('#setVaultURLForm')); let formData = new FormData(document.querySelector('#setVaultURLForm'));
localStorage.setItem('apiurl', formData.get("vaultURL")); pageState.apiURL = formData.get("vaultURL");
changePage("HOME"); changePage("HOME");
}); });
} }

View file

@ -19,9 +19,9 @@ export function verifyJSONString(str) {
} }
export function getToken() { export function getToken() {
return localStorage.getItem('token'); return pageState.token.length > 0 ? pageState.token : null;
} }
export function getAPIURL() { export function getAPIURL() {
return localStorage.getItem('apiurl'); return pageState.apiURL.length > 0 ? pageState.apiURL : null
} }