Stop saving session state and move i18n to appState

This commit is contained in:
Robbie Antenesse 2019-09-25 16:48:35 -06:00
parent eab60fe159
commit c22f632b53
5 changed files with 12 additions and 32 deletions

View File

@ -3,7 +3,6 @@ export const appListeners = (app, state, emitter) => {
document.title = app.siteConfig.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(() => {

View File

@ -1,4 +1,3 @@
import { I18n } from './i18n';
import { globalView } from './views/global';
import { homeView } from './views/home';
import { loginView } from './views/login';
@ -6,13 +5,11 @@ import { searchView } from './views/search';
import { errorView } from './views/404';
export const appRoutes = (app) => {
const i18n = new I18n(app.state); // Global I18n class passed to all views
app.route('/', (state, emit) => globalView(state, emit, homeView));
app.route('/', (state, emit) => globalView(state, emit, i18n, homeView));
app.route('/login', (state, emit) => globalView(state, emit, loginView));
app.route('/login', (state, emit) => globalView(state, emit, i18n, loginView));
app.route('/search', (state, emit) => globalView(state, emit, searchView));
app.route('/search', (state, emit) => globalView(state, emit, i18n, searchView));
app.route('/404', (state, emit) => globalView(state, emit, i18n, errorView));
app.route('/404', (state, emit) => globalView(state, emit, errorView));
}

View File

@ -1,15 +1,8 @@
import { I18n } from "./i18n";
export const appState = (app, 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
state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0];
state.viewStates = {};
state.isLoggedIn = false;
}
state.i18n = new I18n(state); // Global I18n class passed to all views
}

View File

@ -19,14 +19,4 @@ export const appUtilities = (app) => {
savedSettings[settingsKey] = value;
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));
}
}

View File

@ -2,7 +2,8 @@ import html from 'choo/html';
import headerImage from '../../dev/images/header.png';
export const globalView = (state, emit, i18n, view) => {
export const globalView = (state, emit, view) => {
const { i18n } = state;
// Create a wrapper for view content that includes global header/footer
return html`<body>
<header>