Skip to content

Commit

Permalink
[#21] Updated fetch methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 29, 2020
1 parent 50f1ab6 commit 24202f7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
7 changes: 7 additions & 0 deletions src/components/DruxtBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ export default {
* @see {@link https://druxt.github.io/druxt-entity/api/mixins/componentSuggestion.html|DruxtEntityComponentSuggestionMixin}
*/
tokenType: () => 'block'
},
created() {
// Workaround for Vuepress docs.
if (!this.$fetch && DruxtRouterEntityMixin.fetch) {
DruxtRouterEntityMixin.fetch.call(this)
}
}
}
</script>
66 changes: 43 additions & 23 deletions src/components/DruxtBlockRegion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,10 @@ export default {
},
/**
* Fetch requested blocks from Druxt.js Router.
* Nuxt.js fetch method.
*/
async fetch() {
const query = new DrupalJsonApiParams()
query
.addFilter('region', this.name)
.addFilter('status', '1')
.addFilter('theme', this.theme)
.addGroup('visibility', 'OR')
.addFilter('visibility.request_path', null, 'IS NULL', 'visibility')
query.addGroup('pages', 'AND', 'visibility')
.addFilter('visibility.request_path.pages', this.route.resolvedPath, 'CONTAINS', 'pages')
.addFilter('visibility.request_path.negate', 0, '=', 'pages')
query.addGroup('front', 'AND', 'visibility')
.addFilter('visibility.request_path.pages', '<front>', 'CONTAINS', 'front')
.addFilter('visibility.request_path.negate', this.route.isHomePath ? 0 : 1, '=', 'front')
const options = {
headers: { 'Druxt-Request-Path': this.$store.state.druxtRouter.route.resolvedPath }
}
this.blocks = await this.getResources({ resource: 'block--block', query })
await this.fetch()
},
/**
Expand Down Expand Up @@ -176,13 +156,53 @@ export default {
})
},
/**
* Nuxt.js watch property.
*/
watch: {
/**
* Updates blocks on Route change.
*/
$route: function() {
this.$fetch()
this.fetch()
}
},
created() {
// Workaround for Vuepress docs.
if (!this.blocks.length) {
this.fetch()
}
},
methods: {
/**
* Fetch requested blocks from Druxt.js Router.
*/
async fetch() {
const query = new DrupalJsonApiParams()
query
.addFilter('region', this.name)
.addFilter('status', '1')
.addFilter('theme', this.theme)
.addGroup('visibility', 'OR')
.addFilter('visibility.request_path', null, 'IS NULL', 'visibility')
query.addGroup('pages', 'AND', 'visibility')
.addFilter('visibility.request_path.pages', this.route.resolvedPath, 'CONTAINS', 'pages')
.addFilter('visibility.request_path.negate', 0, '=', 'pages')
query.addGroup('front', 'AND', 'visibility')
.addFilter('visibility.request_path.pages', '<front>', 'CONTAINS', 'front')
.addFilter('visibility.request_path.negate', this.route.isHomePath ? 0 : 1, '=', 'front')
const options = {
headers: { 'Druxt-Request-Path': this.$store.state.druxtRouter.route.resolvedPath }
}
this.blocks = await this.getResources({ resource: 'block--block', query })
},
/**
* Maps `druxtRouter/getResources` Vuex action to `this.getResources`.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/components/__tests__/DruxtBlockRegion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ describe('Component - DruxtBlockRegion', () => {

const watch = {
...DruxtBlockRegion.watch,
$fetch: jest.fn()
fetch: jest.fn()
}
watch.$route()
expect(watch.$fetch).toHaveBeenCalled()
expect(watch.fetch).toHaveBeenCalled()
})
})
2 changes: 1 addition & 1 deletion src/mixins/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DruxtEntityContextMixin } from 'druxt-entity'
* }
* </script>
*
* @see {@link https://druxt.github.io/druxt-entity/api/mixins/componentSuggestion|DruxtEntityComponentSuggestionMixin}
* @see {@link https://entity.druxtjs.org/api/mixins/componentSuggestion.html|DruxtEntityComponentSuggestionMixin}
*/
const DruxtBlocksBlockMixin = {
/**
Expand Down

0 comments on commit 24202f7

Please sign in to comment.