Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
Updated: code to move the subscribeAction logic to asyncData (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymaheshwari1 committed Dec 22, 2020
1 parent 57cb6cd commit b854467
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pages/Category.vue
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ import {
const THEME_PAGE_SIZE = 12;
const LAZY_LOADING_ACTIVATION_BREAKPOINT = 1024;
var unsubscribeFromStoreAction = ''
var aggs = ''
const composeInitialPageState = async (store, route, forceLoad = false) => {
try {
Expand Down Expand Up @@ -432,6 +434,11 @@ export default {
async asyncData ({ store, route, context }) {
// this is for SSR purposes to prefetch data - and it's always executed before parent component methods
if (context) context.output.cacheTags.add('category')
unsubscribeFromStoreAction = store.subscribeAction(action => {
if (action.type === 'category-next/loadAvailableFiltersFrom') {
aggs = action.payload.aggregations;
}
});
await composeInitialPageState(store, route);
},
async beforeRouteEnter (to, from, next) {
Expand All @@ -449,24 +456,18 @@ export default {
// Pure CSR, with no initial category state
next(async vm => {
vm.loading = true;
await composeInitialPageState(vm.$store, to, true);
vm.$store.dispatch('category-next/cacheProducts', { route: to });
vm.loading = false;
});
}
},
mounted () {
this.unsubscribeFromStoreAction = this.$store.subscribeAction(action => {
if (action.type === 'category-next/loadAvailableFiltersFrom') {
this.aggregations = action.payload.aggregations;
}
});
this.$bus.$on('product-after-list', this.initPagination);
window.addEventListener('resize', this.getBrowserWidth);
this.getBrowserWidth();
},
beforeDestroy () {
this.unsubscribeFromStoreAction();
unsubscribeFromStoreAction();
this.$bus.$off('product-after-list', this.initPagination);
window.removeEventListener('resize', this.getBrowserWidth);
},
Expand Down Expand Up @@ -546,9 +547,9 @@ export default {
return aggregations
.reduce((result, aggregation) => {
const bucket =
this.aggregations &&
this.aggregations[aggregation] &&
this.aggregations[aggregation].buckets.find(
aggs &&
aggs[aggregation] &&
aggs[aggregation].buckets.find(
bucket => String(bucket.key) === String(filter.id)
);
Expand Down

0 comments on commit b854467

Please sign in to comment.