Ocelot-Social/webapp/store/categories.js
Moriz Wahl a3178a91b4
refactor(webapp): store for categories (#8551)
* after authentification, query the categories if active and store them

* get categories from store

* use category store to get categories

* get categories from store

* mock store to have access to categories

* to get rid of the active categories config variable in the frontend, the Category query returns an empty array when categories are not active

* remove CATEGORIES_ACTIVE from .env

* should return string to avoid warnings in console

* replace all env calls for categories active by getter from store

* use categoriesActive getter

* ignore order of returned categories

* mixin to get the category infos from the store, to ensure, that the quey has been called

* fix misspelling

---------

Co-authored-by: Wolfgang Huß <wolle.huss@pjannto.com>
2025-05-27 15:03:26 +02:00

45 lines
928 B
JavaScript

import CategoryQuery from '~/graphql/CategoryQuery'
export const state = () => {
return {
categories: [],
isInitialized: false,
}
}
export const mutations = {
SET_CATEGORIES(state, categories) {
state.categories = categories || []
},
SET_INIZIALIZED(state) {
state.isInitialized = true
},
}
export const getters = {
categories(state) {
return state.categories
},
categoriesActive(state) {
return !!state.categories.length
},
isInitialized(state) {
return state.isInitialized
},
}
export const actions = {
async init({ commit }) {
try {
const client = this.app.apolloProvider.defaultClient
const {
data: { Category: categories },
} = await client.query({ query: CategoryQuery() })
commit('SET_CATEGORIES', categories)
commit('SET_INIZIALIZED')
} catch (err) {
throw new Error('Could not query categories')
}
},
}