Skip to content

Commit

Permalink
feat(#158): add multilingual support
Browse files Browse the repository at this point in the history
  • Loading branch information
robedwards-cti authored and Decipher committed Jan 15, 2022
1 parent d7e92b2 commit 2c59b43
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 7 additions & 6 deletions packages/druxt/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ class DruxtClient {
* @example @lang js
* const collection = await this.$druxt.getCollection('node--recipe')
*/
async getCollection(type, query) {
const { href } = await this.getIndex(type)
async getCollection(type, query, prefix) {
const { href } = await this.getIndex(type, prefix)
if (!href) {
return false
}
Expand Down Expand Up @@ -321,10 +321,11 @@ class DruxtClient {
* const { href } = await this.$druxt.getIndex('node--article')
*
* @param {string} resource - (Optional) A specific resource to query.
* @param {string} prefix - (Optional) The endpoint prefix.
*
* @returns {object} The resource index object or the specified resource.
*/
async getIndex(resource) {
async getIndex(resource, prefix = '') {
if (this.index && !resource) {
return this.index
}
Expand All @@ -334,7 +335,7 @@ class DruxtClient {
}

const url = this.options.endpoint
const { data } = await this.get(url)
const { data } = await this.get(prefix + url)
let index = data.links

// Throw error if index is invalid.
Expand Down Expand Up @@ -427,12 +428,12 @@ class DruxtClient {
*
* @returns {object} The JSON:API resource data.
*/
async getResource(type, id, query) {
async getResource(type, id, query, prefix) {
if (!id || !type) {
return false
}

let { href } = await this.getIndex(type)
let { href } = await this.getIndex(type, prefix)
// @TODO - Add test coverage.
if (!href) {
href = this.options.endpoint + '/' + type.replace('--', '/')
Expand Down
14 changes: 10 additions & 4 deletions packages/druxt/src/stores/druxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ const DruxtStore = ({ store }) => {
* query: new DrupalJsonApiParams().addFilter('status', '1'),
* })
*/
async getCollection ({ commit, state }, { type, query }) {
async getCollection ({ commit, state, rootState }, { type, query }) {
// Get the route prefix from the druxt-router store.
const prefix = rootState.druxtRouter.route.prefix

// Generate a hash using query data excluding the 'fields' and 'include' data.
const queryObject = getDrupalJsonApiParams(query).getQueryObject()
const hash = query ? md5(JSON.stringify({ ...queryObject, fields: {}, include: [] })) : '_default'
Expand All @@ -173,7 +176,7 @@ const DruxtStore = ({ store }) => {
}

// Get the collection using the DruxtClient instance.
const collection = await this.$druxt.getCollection(type, query)
const collection = await this.$druxt.getCollection(type, query, prefix)

// Store the collection in the DruxtStore.
commit('addCollection', { collection: { ...collection }, type, hash })
Expand All @@ -196,7 +199,10 @@ const DruxtStore = ({ store }) => {
* @example @lang js
* const resource = await this.$store.dispatch('druxt/getResource', { type: 'node--article', id })
*/
async getResource ({ commit, dispatch, state }, { type, id, query }) {
async getResource ({ commit, dispatch, state, rootState }, { type, id, query }) {
// Get the route prefix from the druxt-router store.
const prefix = rootState.druxtRouter.route.prefix

// Get the resource from the store if it's avaialble.
const storedResource = (state.resources[type] || {})[id] ?
{ ...state.resources[type][id] }
Expand Down Expand Up @@ -276,7 +282,7 @@ const DruxtStore = ({ store }) => {
// Request the resource from the DruxtClient if required.
let resource
if (!storedResource || fields) {
resource = await this.$druxt.getResource(type, id, getDrupalJsonApiParams(queryObject))
resource = await this.$druxt.getResource(type, id, getDrupalJsonApiParams(queryObject), prefix)
commit('addResource', { resource: { ...resource } })
}

Expand Down

0 comments on commit 2c59b43

Please sign in to comment.