Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#684): add druxtMenu/flushEntities mutation #689

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-ties-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"druxt-menu": minor
---

feat(#684): added druxtMenu/flushEntities Vuex mutation.
1 change: 1 addition & 0 deletions packages/menu/src/components/DruxtMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export default {
mounted() {
// If logged in and statically generated, re-fetch the menu.
if (this?.$auth?.loggedIn && this?.$store?.app?.context?.isStatic) {
this.$store.commit('druxtMenu/flushEntities', { prefix: this.lang })
const settings = this.$options.druxt.settings(this, this.component.settings)
this.$options.druxt.fetchData.call(this, settings)
}
Expand Down
29 changes: 28 additions & 1 deletion packages/menu/src/stores/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,21 @@ const DruxtMenuStore = ({ store }) => {
const entity = entities[index]
Vue.set(state.entities[prefix], entity.id, entity)
}
}
},

/**
* @name flushEntities
* @mutator {object} flushEntities=entities Removes JSON:API menu item entities from the Vuex state object.
* @param {flushEntitiesContext} context
*
* @example @lang js
* // Flush all menu entities.
* this.$store.commit('druxt/flushCollection', {})
*/
flushEntities (state, { prefix }) {
if (!prefix || typeof state.entities !== 'object') Vue.set(state, 'entities', {})
if (prefix) Vue.set(state.entities, prefix, {})
},
},

/**
Expand Down Expand Up @@ -120,3 +134,16 @@ export { DruxtMenuStore }
* @typedef {object} State
* @property {object} entities - The Drupal JSON:API Menu Item entities.
*/

/**
* Parameters for the `flushEntities` mutation.
*
* @typedef {object} flushEntitiesContext
*
* @param {string} [prefix] - (Optional) The JSON:API endpoint prefix or langcode.
*
* @example @lang js
* {
* prefix: 'en'
* }
*/
16 changes: 16 additions & 0 deletions packages/menu/test/stores/menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,20 @@ describe('DruxtStore', () => {
expect(store.$druxtMenu.get).toHaveBeenCalledWith('main', { test: true }, undefined)
store.dispatch('druxtMenu/get', 'name')
})

test('AddEntities', async () => {
expect(store.state.druxtMenu.entities).toStrictEqual({})
store.commit('druxtMenu/addEntities', { entities: [{ id: 'test' }] })
expect(Object.entries(store.state.druxtMenu.entities[undefined]).length).toBe(1)
expect(store.state.druxtMenu.entities[undefined].test).toStrictEqual({ id: 'test' })
})
Decipher marked this conversation as resolved.
Show resolved Hide resolved

test('flushEntities', async () => {
expect(store.state.druxtMenu.entities).toStrictEqual({})
store.commit('druxtMenu/addEntities', { entities: [{ id: 'test' }] })
expect(Object.entries(store.state.druxtMenu.entities[undefined]).length).toBe(1)
store.commit('druxtMenu/flushEntities', { prefix: 'undefined' })
expect(Object.entries(store.state.druxtMenu.entities[undefined]).length).toBe(0)
store.commit('druxtMenu/flushEntities', {})
})
Decipher marked this conversation as resolved.
Show resolved Hide resolved
})