druxt-views 0.21.0 → 0.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -79,6 +79,17 @@ export default {
79
79
  default: 'default',
80
80
  },
81
81
 
82
+ /**
83
+ * Module settings object.
84
+ *
85
+ * @type {ModuleSettings}
86
+ * @default {}
87
+ */
88
+ settings: {
89
+ type: Object,
90
+ default: () => ({}),
91
+ },
92
+
82
93
  /**
83
94
  * JSON:API Resource type.
84
95
  *
@@ -330,6 +341,14 @@ export default {
330
341
  },
331
342
  },
332
343
 
344
+ mounted() {
345
+ // If static, re-fetch data allowing for cache-bypass.
346
+ // @TODO - Don't re-fetch in serverless configuration.
347
+ if (this.$store.app.context.isStatic) {
348
+ this.$fetch()
349
+ }
350
+ },
351
+
333
352
  methods: {
334
353
  /**
335
354
  * Builds the query for the JSON:API request.
@@ -447,12 +466,22 @@ export default {
447
466
  async fetchData(settings) {
448
467
  const viewId = this.viewId || (((this.view || {}).data || {}).attributes || {}).drupal_internal__id
449
468
  if (viewId) {
469
+ // Check if we need to bypass cache.
470
+ let bypassCache = false
471
+ if (typeof (settings.query || {}).bypassCache === 'boolean') {
472
+ bypassCache = settings.query.bypassCache
473
+ }
474
+
475
+ // Build query.
450
476
  const query = this.getQuery(settings)
477
+
478
+ // Execute the resquest.
451
479
  this.resource = await this.getResults({
452
480
  displayId: this.displayId,
453
481
  prefix: this.lang,
454
482
  query: stringify(query),
455
- viewId
483
+ viewId,
484
+ bypassCache
456
485
  })
457
486
  }
458
487
  },
@@ -475,10 +504,22 @@ export default {
475
504
  /**
476
505
  * Component settings.
477
506
  */
478
- settings: ({ $druxt }, wrapperSettings) => {
479
- const settings = merge($druxt.settings.views || {}, wrapperSettings, { arrayMerge: (dest, src) => src })
507
+ settings: (context, wrapperSettings) => {
508
+ const { $druxt, settings } = context
509
+
510
+ // Start with the `nuxt.config.js` `druxt.settings.views` settings and
511
+ // merge the Wrapper component settings on top.
512
+ let mergedSettings = merge(($druxt.settings || {}).views || {}, wrapperSettings || {}, { arrayMerge: (dest, src) => src })
513
+ // Merge the DruxtViews component `settings` property on top.
514
+ mergedSettings = merge(mergedSettings || {}, settings || {}, { arrayMerge: (dest, src) => src })
515
+
516
+ // Evaluate the bypass cache function.
517
+ if (typeof (mergedSettings.query || {}).bypassCache === 'function') {
518
+ mergedSettings.query.bypassCache = !!mergedSettings.query.bypassCache(context)
519
+ }
520
+
480
521
  return {
481
- query: settings.query || {},
522
+ query: mergedSettings.query || {},
482
523
  }
483
524
  },
484
525
 
@@ -693,28 +734,40 @@ export default {
693
734
  * or the Wrapper component `druxt` object.
694
735
  *
695
736
  * @typedef {object} ModuleSettings
696
- * @param {boolean} bundleFilter - Whether to automatically detect Resource types to filter, based on the View `bundle` filter.
697
- * @param {string[]} fields - An array of fields to filter from the JSON:API Views Resource types.
698
- * @param {string[]} resourceTypes - An array of Resource types to be used by the Fields filter.
699
- *
700
- * @example @lang js
701
- * {
702
- * bundleFilter: false,
703
- * fields: [],
704
- * resourceTypes: []
705
- * }
737
+ * @param {object} query - View results query settings:
738
+ * @param {(boolean|function)} query.bypassCache - Whether to pull the data from the Vuex store or from the JSON:API.
739
+ * @param {boolean} query.bundleFilter - Whether to automatically detect Resource types to filter, based on the View `bundle` filter.
740
+ * @param {string[]} query.fields - An array of fields to filter from the JSON:API Views Resource types.
741
+ * @param {string[]} query.resourceTypes - An array of Resource types to be used by the Fields filter.
706
742
  *
707
- * @example @lang vue
743
+ * @example <caption>DruxtView Wrapper component</caption> @lang vue
708
744
  * <script>
709
745
  * export default {
710
746
  * druxt: {
711
747
  * query: {
748
+ * bypassCache: ({ $store }) => $store.$auth.loggedIn,
712
749
  * bundleFilter: false,
713
- * fields: ['title']
750
+ * fields: [],
714
751
  * resourceTypes: ['node--article'],
715
752
  * },
716
- * }
753
+ * },
717
754
  * }
755
+ *
756
+ * @example <caption>DruxtView component with settings</caption> @lang vue
757
+ * <template>
758
+ * <DruxtView
759
+ * display-id="block_1"
760
+ * view-id="promoted_items
761
+ * :settings="{
762
+ * query: {
763
+ * bypassCache: true,
764
+ * bundleFilter: false,
765
+ * fields: ['title']
766
+ * resourceTypes: [],
767
+ * }
768
+ * }"
769
+ * />
770
+ * </template>
718
771
  */
719
772
 
720
773
  /**
@@ -101,17 +101,36 @@ const DruxtViewsStore = ({ store }) => {
101
101
  if (!state.results[viewId][displayId][prefix])
102
102
  Vue.set(state.results[viewId][displayId], prefix, {});
103
103
  Vue.set(state.results[viewId][displayId][prefix], hash, results);
104
+ },
105
+ flushResults(state, { viewId, displayId, prefix, hash }) {
106
+ if (!viewId)
107
+ Vue.set(state, "results", {});
108
+ else if (viewId && !displayId && !prefix && !hash)
109
+ Vue.set(state.results, viewId, {});
110
+ else if (viewId && displayId && !prefix && !hash)
111
+ Vue.set(state.results[viewId], displayId, {});
112
+ else if (viewId && displayId && prefix && !hash)
113
+ Vue.set(state.results[viewId][displayId], prefix, {});
114
+ else if (viewId && displayId && (prefix || prefix === void 0) && hash)
115
+ Vue.set(state.results[viewId][displayId][prefix], hash, {});
104
116
  }
105
117
  },
106
118
  actions: {
107
- async getResults({ commit, state }, { viewId, displayId, query, prefix }) {
119
+ async getResults({ commit, state }, { viewId, displayId, query, prefix, bypassCache = false }) {
108
120
  const hash = query ? md5(this.$druxt.buildQueryUrl("", query)) : "_default";
121
+ let cache;
109
122
  if (typeof (((state.results[viewId] || {})[displayId] || {})[prefix] || {})[hash] !== "undefined") {
110
- return state.results[viewId][displayId][prefix][hash];
123
+ cache = state.results[viewId][displayId][prefix][hash];
124
+ if (!bypassCache)
125
+ return cache;
126
+ }
127
+ try {
128
+ const results = await this.$druxt.getResource(`views--${viewId}`, displayId, query, prefix);
129
+ commit("addResults", { results, viewId, displayId, prefix, hash });
130
+ return results;
131
+ } catch (e) {
132
+ return cache ? cache : false;
111
133
  }
112
- const results = await this.$druxt.getResource(`views--${viewId}`, displayId, query, prefix);
113
- commit("addResults", { results, viewId, displayId, prefix, hash });
114
- return results;
115
134
  }
116
135
  }
117
136
  };
@@ -111,17 +111,36 @@ const DruxtViewsStore = ({ store }) => {
111
111
  if (!state.results[viewId][displayId][prefix])
112
112
  Vue__default["default"].set(state.results[viewId][displayId], prefix, {});
113
113
  Vue__default["default"].set(state.results[viewId][displayId][prefix], hash, results);
114
+ },
115
+ flushResults(state, { viewId, displayId, prefix, hash }) {
116
+ if (!viewId)
117
+ Vue__default["default"].set(state, "results", {});
118
+ else if (viewId && !displayId && !prefix && !hash)
119
+ Vue__default["default"].set(state.results, viewId, {});
120
+ else if (viewId && displayId && !prefix && !hash)
121
+ Vue__default["default"].set(state.results[viewId], displayId, {});
122
+ else if (viewId && displayId && prefix && !hash)
123
+ Vue__default["default"].set(state.results[viewId][displayId], prefix, {});
124
+ else if (viewId && displayId && (prefix || prefix === void 0) && hash)
125
+ Vue__default["default"].set(state.results[viewId][displayId][prefix], hash, {});
114
126
  }
115
127
  },
116
128
  actions: {
117
- async getResults({ commit, state }, { viewId, displayId, query, prefix }) {
129
+ async getResults({ commit, state }, { viewId, displayId, query, prefix, bypassCache = false }) {
118
130
  const hash = query ? md5__default["default"](this.$druxt.buildQueryUrl("", query)) : "_default";
131
+ let cache;
119
132
  if (typeof (((state.results[viewId] || {})[displayId] || {})[prefix] || {})[hash] !== "undefined") {
120
- return state.results[viewId][displayId][prefix][hash];
133
+ cache = state.results[viewId][displayId][prefix][hash];
134
+ if (!bypassCache)
135
+ return cache;
136
+ }
137
+ try {
138
+ const results = await this.$druxt.getResource(`views--${viewId}`, displayId, query, prefix);
139
+ commit("addResults", { results, viewId, displayId, prefix, hash });
140
+ return results;
141
+ } catch (e) {
142
+ return cache ? cache : false;
121
143
  }
122
- const results = await this.$druxt.getResource(`views--${viewId}`, displayId, query, prefix);
123
- commit("addResults", { results, viewId, displayId, prefix, hash });
124
- return results;
125
144
  }
126
145
  }
127
146
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "druxt-views",
3
- "version": "0.21.0",
3
+ "version": "0.22.1",
4
4
  "description": "Drupal Views components for Druxt with support for filters, pagination and sorting.",
5
5
  "keywords": [
6
6
  "cms",
@@ -41,17 +41,17 @@
41
41
  "templates"
42
42
  ],
43
43
  "dependencies": {
44
- "axios": "^0.27.2",
45
- "deepmerge": "^4.2.2",
46
- "drupal-jsonapi-params": "^2.1.0",
47
- "druxt": "^0.21.0",
48
- "druxt-blocks": "^0.17.0",
49
- "druxt-entity": "^0.27.0",
50
- "druxt-router": "^0.29.0",
44
+ "axios": "0.27.2",
45
+ "deepmerge": "^4.3.1",
46
+ "drupal-jsonapi-params": "^2.2.0",
47
+ "druxt": "^0.22.0",
48
+ "druxt-blocks": "^0.17.2",
49
+ "druxt-entity": "^0.28.0",
50
+ "druxt-router": "^0.29.1",
51
51
  "md5": "^2.3.0"
52
52
  },
53
53
  "optionalDependencies": {
54
- "vue": "^2.7.10",
54
+ "vue": "^2.7.14",
55
55
  "vuex": "^3.6.2"
56
56
  },
57
57
  "publishConfig": {