Readlebee/app/appUtilities.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

const appUtilities = (app) => {
app.getSettingsItem = settingsKey => {
let savedSettings = typeof window !== 'undefined' && window.localStorage.getItem('settings');
if (savedSettings) {
savedSettings = JSON.parse(savedSettings);
if (typeof savedSettings[settingsKey] !== 'undefined') {
return savedSettings[settingsKey];
}
}
return null;
}
app.setSettingsItem = (settingsKey, value) => {
if (typeof window === 'undefined') return null;
let savedSettings = window.localStorage.getItem('settings');
if (savedSettings) {
savedSettings = JSON.parse(savedSettings);
} else {
savedSettings = {};
}
savedSettings[settingsKey] = value;
return window.localStorage.setItem('settings', JSON.stringify(savedSettings));
}
2019-10-18 05:35:32 +02:00
app.checkIfLoggedIn = (appState) => {
if (typeof window === 'undefined') return false;
2019-10-18 05:35:32 +02:00
return fetch('/api/account/validate', { method: 'post' })
.then(response => response.json())
.then(response => {
if (response.error !== false) {
console.warn(appState.i18n.__(response.message));
2019-10-18 05:35:32 +02:00
return false;
}
console.info(appState.i18n.__(response.message));
2019-10-18 05:35:32 +02:00
appState.isLoggedIn = true;
return true;
});
}
}
module.exports = { appUtilities };