export const state = () => ({
  items: []
})

export const getters = {
  hasCategories(state) {
    return state.items.length > 0
  }
}

export const actions = {
  async fetchCategories({state, commit, getters}) {
    if (getters.hasCategories) return

    try{
      console.log('category.js action call fetchCategories')
      const categories = await this.$axios.$get('/api/v1/categories')
      console.log('category.js action done fetchCategories', categories)
      console.log('category.js mutations call setCategories')
      commit('setCategories', categories)
      console.log('category.js mutations done setCategories')
      return state.items

    }catch(error){
      return error
    }
  }
}

export const mutations = {
  setCategories(state, categories) {
    state.items = categories
  }
}