import axios from 'axios';

export default {
  namespaced: true,
  state: {
    categories: [],
    category: {}
  },
  mutations: {
    setCategories(state, categories) {
      state.categories = categories;
    },
    setCategory(state, category) {
      state.category = category;
      state.categories.push(category);
    },
    removeCategory(state, category) {
      state.categories.pop(category);
    }
  },
  getters: {},
  actions: {
    getCategories({
      commit
    }, restId) {
      axios
        .get(`/api/MenuCategory/GetRestaurantCategories/${restId}`)
        .then(result => commit("setCategories", result.data));
    },
    addCategory({
      commit
    }, category) {
      axios
        .post('/api/MenuCategory', category)
        .then(result => commit("setCategory", result.data));
    },
    updateCategory({
      commit
    }, category) {
      axios
        .put('/api/MenuCategory', category)
        .then(result => commit("setCategory", result.data));
    },
    deleteCategory({
      commit
    }, id) {
      axios
        .delete(`/api/MenuCategory/${id}`)
        .then(result => commit("removeCategory", result.data));
    }
  }
}
