Stop saving session state and move i18n to appState
This commit is contained in:
parent
eab60fe159
commit
c22f632b53
|
@ -3,7 +3,6 @@ export const appListeners = (app, state, emitter) => {
|
||||||
document.title = app.siteConfig.siteName;
|
document.title = app.siteConfig.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(() => {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { I18n } from './i18n';
|
|
||||||
import { globalView } from './views/global';
|
import { globalView } from './views/global';
|
||||||
import { homeView } from './views/home';
|
import { homeView } from './views/home';
|
||||||
import { loginView } from './views/login';
|
import { loginView } from './views/login';
|
||||||
|
@ -6,13 +5,11 @@ import { searchView } from './views/search';
|
||||||
import { errorView } from './views/404';
|
import { errorView } from './views/404';
|
||||||
|
|
||||||
export const appRoutes = (app) => {
|
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, errorView));
|
||||||
|
|
||||||
app.route('/404', (state, emit) => globalView(state, emit, i18n, errorView));
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,8 @@
|
||||||
|
import { I18n } from "./i18n";
|
||||||
|
|
||||||
export const appState = (app, state, emitter) => {
|
export const appState = (app, state, emitter) => {
|
||||||
const sessionState = app.getSessionState();
|
state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0];
|
||||||
if (sessionState) {
|
state.viewStates = {};
|
||||||
Object.keys(sessionState).forEach(key => {
|
state.isLoggedIn = false;
|
||||||
if (typeof state[key] === 'undefined') {
|
state.i18n = new I18n(state); // Global I18n class passed to all views
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -19,14 +19,4 @@ export const appUtilities = (app) => {
|
||||||
savedSettings[settingsKey] = value;
|
savedSettings[settingsKey] = value;
|
||||||
return window.localStorage.setItem('settings', 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));
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -2,7 +2,8 @@ import html from 'choo/html';
|
||||||
|
|
||||||
import headerImage from '../../dev/images/header.png';
|
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
|
// Create a wrapper for view content that includes global header/footer
|
||||||
return html`<body>
|
return html`<body>
|
||||||
<header>
|
<header>
|
||||||
|
|
Loading…
Reference in New Issue