diff --git a/app/appListeners.js b/app/appListeners.js index 24d332b..d884600 100644 --- a/app/appListeners.js +++ b/app/appListeners.js @@ -53,7 +53,7 @@ const appListeners = (app, state, emitter) => { }).then(result => callback(result)); }); - if (typeof window !== 'undefined') { + if (state.isFrontend) { state.i18n.fetchLocaleUI().then(() => { app.checkIfLoggedIn(state).then(isLoggedIn => { emitter.emit(state.events.RENDER); // This should hopefully only run once after the DOM is loaded. It prevents routing issues where 'render' hasn't been defined yet diff --git a/app/appState.js b/app/appState.js index 3ff2673..a4141af 100644 --- a/app/appState.js +++ b/app/appState.js @@ -1,10 +1,12 @@ const { I18n } = require("./i18n"); const appState = (app, state, emitter) => { + state.isFrontend = typeof window !== 'undefined'; + state.events.SET_LANGUAGE = 'setLanguage'; state.events.ADD_TO_SHELF = 'addToShelf'; - if (typeof window !== 'undefined') { + if (state.isFrontend) { state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (window.navigator.language || window.navigator.userLanguage).split('-')[0]; state.isLoggedIn = false; state.i18n = new I18n(state); // Global I18n class passed to all views diff --git a/app/appUtilities.js b/app/appUtilities.js index 09f57aa..682bc13 100644 --- a/app/appUtilities.js +++ b/app/appUtilities.js @@ -1,6 +1,6 @@ const appUtilities = (app) => { app.getSettingsItem = settingsKey => { - let savedSettings = typeof window !== 'undefined' && window.localStorage.getItem('settings'); + let savedSettings = app.state.isFrontend && window.localStorage.getItem('settings'); if (savedSettings) { savedSettings = JSON.parse(savedSettings); if (typeof savedSettings[settingsKey] !== 'undefined') { @@ -10,7 +10,7 @@ const appUtilities = (app) => { return null; } app.setSettingsItem = (settingsKey, value) => { - if (typeof window === 'undefined') return null; + if (!app.state.isFrontend) return null; let savedSettings = window.localStorage.getItem('settings'); if (savedSettings) { @@ -23,7 +23,8 @@ const appUtilities = (app) => { } app.checkIfLoggedIn = (appState) => { - if (typeof window === 'undefined') return false; + if (!app.state.isFrontend) return false; + return fetch('/api/account/validate', { method: 'post' }) .then(response => response.json()) .then(response => { diff --git a/app/views/global.js b/app/views/global.js index 6a65b0c..6afe7e1 100644 --- a/app/views/global.js +++ b/app/views/global.js @@ -17,7 +17,7 @@ if (typeof window !== 'undefined') { const globalView = (state, emit, view) => { const { i18n } = state; - if (typeof window !== 'undefined' && i18n.needsFetch) { + if (state.isFrontend && i18n.needsFetch) { return html``; } // Create a wrapper for view content that includes global header/footer diff --git a/app/views/home/index.js b/app/views/home/index.js index 6a0c257..5e2ddce 100644 --- a/app/views/home/index.js +++ b/app/views/home/index.js @@ -11,7 +11,7 @@ const homeView = (state, emit, i18n) => { // Returning an array in a view allows non-shared parent HTML elements. // This one doesn't have the problem right now, but it's good to remember. return [ - (!controller.isLoggedIn || typeof window === 'undefined' + (!controller.isLoggedIn ? loggedOutView(controller, emit) : loggedInView(controller, emit) ), diff --git a/app/views/search/controller.js b/app/views/search/controller.js index 369f6b1..84355c2 100644 --- a/app/views/search/controller.js +++ b/app/views/search/controller.js @@ -33,7 +33,7 @@ class SearchController extends ViewController { } get hasQuery() { - return typeof window !== 'undefined' + return this.appState.isFrontend && this.appState.query.hasOwnProperty('for') && this.appState.query.for.trim() !== ''; } diff --git a/app/views/shelves/userShelves/index.js b/app/views/shelves/userShelves/index.js index b82250b..f6a3280 100644 --- a/app/views/shelves/userShelves/index.js +++ b/app/views/shelves/userShelves/index.js @@ -20,7 +20,7 @@ const userShelvesView = (shelvesController, emit) => { ]; } - if (shelvesController.state.myShelves.length <= 0) { + if (shelvesController.appState.isFrontend && shelvesController.state.myShelves.length <= 0) { shelvesController.getUserShelves().then(() => { emit(shelvesController.appState.events.RENDER); }); diff --git a/server/routes/public.js b/server/routes/public.js index caa3257..39b278d 100644 --- a/server/routes/public.js +++ b/server/routes/public.js @@ -11,19 +11,19 @@ async function routes(fastify, options) { return reply.sendFile(request.params.chooRoute); } // Otherwise, send allow Choo to route it. - // const state = Object.assign({}, chooApp.state); - chooApp.state.language = request.language; - chooApp.state.isLoggedIn = request.isLoggedInUser; + const state = Object.assign({}, chooApp.state); + state.language = request.language; + state.isLoggedIn = request.isLoggedInUser; - chooApp.state.i18n = new chooI18n(chooApp.state); - chooApp.state.i18n.availableLanguages = fastify.i18n.available.slice(); - chooApp.state.i18n.default = Object.assign({}, fastify.i18n.default); + state.i18n = new chooI18n(state); + state.i18n.availableLanguages = fastify.i18n.available.slice(); + state.i18n.default = Object.assign({}, fastify.i18n.default); - const locale = typeof fastify.i18n[chooApp.state.language] !== 'undefined' ? chooApp.state.language : 'default'; - chooApp.state.i18n.language = Object.assign({}, fastify.i18n[locale]); - chooApp.state.i18n.pages = Object.assign({}, fastify.i18n.pages[locale]); + const locale = typeof fastify.i18n[state.language] !== 'undefined' ? state.language : 'default'; + state.i18n.language = Object.assign({}, fastify.i18n[locale]); + state.i18n.pages = Object.assign({}, fastify.i18n.pages[locale]); - const html = htmlContainer.toString().replace(/\.+?\<\/body\>/, chooApp.toString('/' + request.params.chooRoute, chooApp.state)); + const html = htmlContainer.toString().replace(/\(.|\n)+?\<\/body\>/, chooApp.toString('/' + request.params.chooRoute, state)); return reply.type('text/html').send(html); }); }