import { ActionTree, GetterTree, MutationTree, ActionContext, Module } from 'vuex' import { ICategoryItem, CategoryItemService } from 'client/service/CategoryItem' interface CategoryItemState { categoryItemList: ICategoryItem[] } const state: CategoryItemState = { categoryItemList: [] } const getters: GetterTree = {} const actions: ActionTree = { async loadCategoryItem({ commit }: ActionContext): Promise { const data: ICategoryItem[] = await CategoryItemService.getCategoryItem() commit('setCategoryItem', data) }, async createItem({ dispatch }, { category, name }: ICategoryItem): Promise { const createdId = await CategoryItemService.addItem({ category, name }) dispatch('loadCategoryItem') return createdId }, async deleteItem({ dispatch }, { id }: ICategoryItem) { const deletedId = await CategoryItemService.deleteItem({ id }) dispatch('loadCategoryItem') return deletedId } } const mutations: MutationTree = { setCategoryItem: (state: CategoryItemState, payload: ICategoryItem[]) => { state.categoryItemList = payload } } const categoryItem: Module = { namespaced: true, state, getters, actions, mutations } export default categoryItem