Add a questionable way to save Choo state for the session
This commit is contained in:
parent
0ebafaeb9e
commit
596721fb2d
30
app/index.js
30
app/index.js
|
@ -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) => {
|
||||||
// Default state variables
|
const sessionState = app.getSessionState();
|
||||||
state.currentView = 'home';
|
if (sessionState) {
|
||||||
state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0];
|
Object.keys(sessionState).forEach(key => {
|
||||||
state.viewStates = {};
|
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
|
// 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(() => {
|
||||||
|
|
Loading…
Reference in New Issue