From 2ab48553cf25547a6ddba50a05aee774079b5977 Mon Sep 17 00:00:00 2001 From: pkarw Date: Wed, 16 Jan 2019 23:16:42 +0100 Subject: [PATCH] Improved offline mode handlers --- core/pages/Category.js | 10 ++++++++++ core/store/lib/search.ts | 30 ++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/core/pages/Category.js b/core/pages/Category.js index dc744949a2..9592a089f9 100644 --- a/core/pages/Category.js +++ b/core/pages/Category.js @@ -1,6 +1,7 @@ import Vue from 'vue' import toString from 'lodash-es/toString' +import i18n from '@vue-storefront/i18n' import store from '@vue-storefront/store' import EventBus from '@vue-storefront/core/compatibility/plugins/event-bus' import { baseFilterProductsQuery, buildFilterProductsQuery } from '@vue-storefront/core/helpers' @@ -242,6 +243,15 @@ export default { this.$store.dispatch('category/products', this.$store.state.category.current_product_query) EventBus.$emitFilter('category-after-load', { store: this.$store, route: this.$route }) } + }).catch(err => { + if (err.message.indexOf('query returned empty result') > 0) { + this.$store.dispatch('notification/spawnNotification', { + type: 'error', + message: i18n.t('The product, category or CMS page is not available in Offline mode. Redirecting to Home.'), + action1: { label: i18n.t('OK') } + }) + this.$router.push(localizedRoute('/', currentStoreView().storeCode)) + } }) }, onUserPricesRefreshed () { diff --git a/core/store/lib/search.ts b/core/store/lib/search.ts index 95e7768e90..d7f3ba885a 100644 --- a/core/store/lib/search.ts +++ b/core/store/lib/search.ts @@ -61,22 +61,6 @@ export const quickSearchByQuery = async ({ query, start = 0, size = 50, entityT resolve(res) console.debug('Result from cache for ' + cacheKey + ' (' + entityType + '), ms=' + (new Date().getTime() - benchmarkTime.getTime())) servedFromCache = true - } else { - if (!isOnline()) { - console.debug('No results and offline ' + cacheKey + ' (' + entityType + '), ms=' + (new Date().getTime() - benchmarkTime.getTime())) - res = { - items: [], - total: 0, - start: 0, - perPage: 0, - aggregations: {}, - offline: true, - cache: true, - noresults: true - } - servedFromCache = true - resolve(res) - } } }).catch((err) => { console.error('Cannot read cache for ' + cacheKey + ', ' + err) @@ -109,6 +93,20 @@ export const quickSearchByQuery = async ({ query, start = 0, size = 50, entityT } } }).catch(err => { + if (!servedFromCache) { + console.debug('No results and offline ' + cacheKey + ' (' + entityType + '), ms=' + (new Date().getTime() - benchmarkTime.getTime())) + const res = { + items: [], + total: 0, + start: 0, + perPage: 0, + aggregations: {}, + offline: true, + cache: true, + noresults: true + } + resolve(res) + } reject(err) }) })