Add isFrontend check to Choo state

This commit is contained in:
Robbie Antenesse 2020-09-21 20:49:02 -06:00
parent d2c0c289ab
commit 391f2734d8
8 changed files with 22 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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 => {

View File

@ -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`<body><i class="icon-loading animate-spin"></i></body>`;
}
// Create a wrapper for view content that includes global header/footer

View File

@ -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)
),

View File

@ -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() !== '';
}

View File

@ -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);
});

View File

@ -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\>.+?\<\/body\>/, chooApp.toString('/' + request.params.chooRoute, chooApp.state));
const html = htmlContainer.toString().replace(/\<body\>(.|\n)+?\<\/body\>/, chooApp.toString('/' + request.params.chooRoute, state));
return reply.type('text/html').send(html);
});
}