2018-09-13 10:51:20 +03:00
|
|
|
import { ActionTree, GetterTree, MutationTree, ActionContext, Module } from 'vuex'
|
2018-11-25 20:41:15 +03:00
|
|
|
import { ICategoryInfo, ICategoryFull, CategoryService } from 'client/service/Category'
|
2018-09-13 10:51:20 +03:00
|
|
|
|
2018-11-21 13:31:31 +03:00
|
|
|
interface ICategoryState {
|
2018-11-25 20:41:15 +03:00
|
|
|
categoryList: ICategoryInfo[],
|
|
|
|
category: ICategoryFull
|
2018-09-13 10:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:31:31 +03:00
|
|
|
const state: ICategoryState = {
|
2018-11-25 20:41:15 +03:00
|
|
|
categoryList: [],
|
|
|
|
category: null
|
2018-09-13 10:51:20 +03:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:31:31 +03:00
|
|
|
const getters: GetterTree<ICategoryState, any> = {}
|
2018-09-13 10:51:20 +03:00
|
|
|
|
2018-11-21 13:31:31 +03:00
|
|
|
const actions: ActionTree<ICategoryState, any> = {
|
2018-11-25 20:41:15 +03:00
|
|
|
async reloadCategory ({ dispatch, state }: ActionContext<ICategoryState, any>) {
|
|
|
|
const category = state.category
|
|
|
|
if (!category) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dispatch('loadCategory', category.uid)
|
|
|
|
},
|
|
|
|
async loadCategory (
|
|
|
|
{ commit }: ActionContext<ICategoryState, any>,
|
|
|
|
categoryId: ICategoryInfo['uid']
|
|
|
|
): Promise<any> {
|
|
|
|
const data: ICategoryFull = await CategoryService.getCategoryById(categoryId)
|
|
|
|
// TODO create set function for all the store
|
|
|
|
commit('setCategory', data)
|
|
|
|
},
|
2018-11-21 13:31:31 +03:00
|
|
|
async loadCategoryList ({ commit }: ActionContext<ICategoryState, any>): Promise<any> {
|
|
|
|
const data: ICategoryInfo[] = await CategoryService.getCategoryList()
|
2018-09-13 10:51:20 +03:00
|
|
|
commit('setCategoryList', data)
|
2018-10-29 21:17:13 +03:00
|
|
|
},
|
2018-11-21 13:31:31 +03:00
|
|
|
async createCategory (
|
|
|
|
{ dispatch }: ActionContext<ICategoryState, any>,
|
|
|
|
{ title, group }: { title: ICategoryInfo['title'], group: ICategoryInfo['group'] }
|
|
|
|
): Promise<ICategoryInfo['uid']> {
|
2018-10-29 21:17:13 +03:00
|
|
|
const createdId = await CategoryService.createCategory({
|
|
|
|
title,
|
|
|
|
group
|
|
|
|
})
|
|
|
|
dispatch('loadCategoryList')
|
|
|
|
return createdId
|
2018-09-13 10:51:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:31:31 +03:00
|
|
|
const mutations: MutationTree<ICategoryState> = {
|
|
|
|
setCategoryList: (state: ICategoryState, payload: ICategoryInfo[]) => {
|
2018-09-13 10:51:20 +03:00
|
|
|
state.categoryList = payload
|
2018-11-25 20:41:15 +03:00
|
|
|
},
|
|
|
|
setCategory: (state: ICategoryState, payload: ICategoryFull) => {
|
|
|
|
state.category = payload
|
2018-09-13 10:51:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:31:31 +03:00
|
|
|
const category: Module<ICategoryState, any> = {
|
2018-09-13 10:51:20 +03:00
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations
|
2018-11-25 20:41:15 +03:00
|
|
|
}
|
2018-09-13 10:51:20 +03:00
|
|
|
|
2018-11-21 13:31:31 +03:00
|
|
|
export default category
|