From d23f153570f79a3fce6b0f241401ed0c9d20ee7c Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Fri, 18 Jun 2021 16:38:25 -0300 Subject: [PATCH] Checks query before reloading filters facets. --- .../filter-types/checkbox/Checkbox.vue | 11 +++++----- .../filter-types/selectbox/Selectbox.vue | 11 +++++----- .../filter-types/taxonomy/Checkbox.vue | 8 ++++--- src/views/admin/js/event-bus-search.js | 21 +++++++++++++++++++ 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/views/admin/components/filter-types/checkbox/Checkbox.vue b/src/views/admin/components/filter-types/checkbox/Checkbox.vue index 85b2fc69d..31920063a 100644 --- a/src/views/admin/components/filter-types/checkbox/Checkbox.vue +++ b/src/views/admin/components/filter-types/checkbox/Checkbox.vue @@ -78,21 +78,22 @@ if (!isEqual) this.onSelect(); }, - 'query'() { - if (!this.isUsingElasticSearch) - this.loadOptions(); - }, facetsFromItemSearch: { handler() { if (this.isUsingElasticSearch) this.loadOptions(); }, immediate: true - } + }, }, mounted() { if (!this.isUsingElasticSearch && !this.filtersAsModal) this.loadOptions(); + + this.$eventBusSearch.$on('has-to-reload-facets', (shouldReload) => { + if ( !this.isUsingElasticSearch && shouldReload ) + this.loadOptions(); + }); }, methods: { loadOptions() { diff --git a/src/views/admin/components/filter-types/selectbox/Selectbox.vue b/src/views/admin/components/filter-types/selectbox/Selectbox.vue index 0824b5bb6..5c1d62867 100644 --- a/src/views/admin/components/filter-types/selectbox/Selectbox.vue +++ b/src/views/admin/components/filter-types/selectbox/Selectbox.vue @@ -37,10 +37,6 @@ } }, watch: { - 'query'() { - if (!this.isUsingElasticSearch) - this.loadOptions(); - }, facetsFromItemSearch: { handler() { if (this.isUsingElasticSearch) @@ -49,9 +45,14 @@ immediate: true } }, - mounted(){ + mounted() { if (!this.isUsingElasticSearch) this.loadOptions(); + + this.$eventBusSearch.$on('has-to-reload-facets', (shouldReload) => { + if ( !this.isUsingElasticSearch && shouldReload ) + this.loadOptions(); + }); }, methods: { loadOptions(){ diff --git a/src/views/admin/components/filter-types/taxonomy/Checkbox.vue b/src/views/admin/components/filter-types/taxonomy/Checkbox.vue index 2464af055..fc2ad82db 100644 --- a/src/views/admin/components/filter-types/taxonomy/Checkbox.vue +++ b/src/views/admin/components/filter-types/taxonomy/Checkbox.vue @@ -111,9 +111,6 @@ this.isLoadingOptions = this.isLoadingItems; }, immediate: true - }, - 'query'() { - this.loadOptions(); } }, created() { @@ -129,6 +126,11 @@ mounted(){ if (!this.filtersAsModal) this.loadOptions(); + + this.$eventBusSearch.$on('has-to-reload-facets', (shouldReload) => { + if ( !this.isUsingElasticSearch && shouldReload ) + this.loadOptions(); + }); }, beforeDestroy() { diff --git a/src/views/admin/js/event-bus-search.js b/src/views/admin/js/event-bus-search.js index 362c016f7..ae21f3e83 100644 --- a/src/views/admin/js/event-bus-search.js +++ b/src/views/admin/js/event-bus-search.js @@ -160,7 +160,28 @@ export default { } else { this.$store.dispatch('search/set_postquery', this.$route.query); } + + // Checks current metaqueries and taxqueries to alert filters that should reload + // For some reason, this process is not working accessing to.query, so we need to check the path string. + const oldQueryString = from.fullPath.replace(from.path + '?', ''); + const newQueryString = to.fullPath.replace(from.path + '?', ''); + + const oldQueryArray = oldQueryString.split('&'); + const newQueryArray = newQueryString.split('&'); + const oldMetaQueryArray = oldQueryArray.filter(queryItem => queryItem.startsWith('metaquery')); + const newMetaQueryArray = newQueryArray.filter(queryItem => queryItem.startsWith('metaquery')); + const oldTaxQueryArray = oldQueryArray.filter(queryItem => queryItem.startsWith('taxquery')); + const newTaxQueryArray = newQueryArray.filter(queryItem => queryItem.startsWith('taxquery')); + + if ( + JSON.stringify(oldMetaQueryArray) != JSON.stringify(newMetaQueryArray) || + JSON.stringify(oldTaxQueryArray) != JSON.stringify(newTaxQueryArray) + ) { + this.$emit('has-to-reload-facets', true); + } + + // Finally, loads items if (to.fullPath != from.fullPath) { this.loadItems(to); }