Add a questionable way to save Choo state for the session

This commit is contained in:
Robbie Antenesse 2019-09-12 17:30:56 -06:00
parent 0ebafaeb9e
commit 596721fb2d
1 changed files with 25 additions and 5 deletions

View File

@ -31,22 +31,42 @@ app.use((state, emitter) => {
savedSettings = {}; savedSettings = {};
} }
savedSettings[settingsKey] = value; savedSettings[settingsKey] = value;
return window.localStorage.setItem(JSON.stringify(savedSettings)); return window.localStorage.setItem('settings', JSON.stringify(savedSettings));
}
app.getSessionState = () => {
let sessionState = window.sessionStorage.getItem('sessionState');
if (sessionState) {
return JSON.parse(sessionState);
}
return null;
}
app.setSessionState = () => {
return window.sessionStorage.setItem('sessionState', JSON.stringify(app.state));
} }
}); });
// App state and emitters // App state and emitters
app.use((state, emitter) => { app.use((state, emitter) => {
const sessionState = app.getSessionState();
if (sessionState) {
Object.keys(sessionState).forEach(key => {
if (typeof state[key] === 'undefined') {
state[key] = sessionState[key];
}
});
} else {
// Default state variables // Default state variables
state.currentView = 'home'; state.currentView = 'home';
state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0]; state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0];
state.viewStates = {}; state.viewStates = {};
}
// Listeners // Listeners
emitter.on('DOMContentLoaded', () => { emitter.on('DOMContentLoaded', () => {
document.title = config.siteName; document.title = config.siteName;
// Emitter listeners // Emitter listeners
emitter.on('render', callback => { emitter.on('render', callback => {
app.setSessionState();
// This is a dirty hack to get the callback to call *after* re-rendering. // This is a dirty hack to get the callback to call *after* re-rendering.
if (callback && typeof callback === "function") { if (callback && typeof callback === "function") {
setTimeout(() => { setTimeout(() => {