From 596721fb2d5ce6c97fe39680039303ff89cefebf Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 12 Sep 2019 17:30:56 -0600 Subject: [PATCH] Add a questionable way to save Choo state for the session --- app/index.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/app/index.js b/app/index.js index bc35dee..ee5480a 100644 --- a/app/index.js +++ b/app/index.js @@ -31,22 +31,42 @@ app.use((state, emitter) => { savedSettings = {}; } 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.use((state, emitter) => { - // Default state variables - state.currentView = 'home'; - state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0]; - state.viewStates = {}; + const sessionState = app.getSessionState(); + if (sessionState) { + Object.keys(sessionState).forEach(key => { + if (typeof state[key] === 'undefined') { + state[key] = sessionState[key]; + } + }); + } else { + // Default state variables + state.currentView = 'home'; + state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0]; + state.viewStates = {}; + } // Listeners emitter.on('DOMContentLoaded', () => { document.title = config.siteName; // Emitter listeners emitter.on('render', callback => { + app.setSessionState(); // This is a dirty hack to get the callback to call *after* re-rendering. if (callback && typeof callback === "function") { setTimeout(() => {