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
|
|
|
}
|
|
|
|
|
2019-04-18 00:52:43 +03:00
|
|
|
const state = (): ICategoryState => ({
|
2018-11-25 20:41:15 +03:00
|
|
|
categoryList: [],
|
|
|
|
category: null
|
2019-04-18 00:52:43 +03:00
|
|
|
})
|
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
|
|
|
|
}
|
2019-02-16 15:15:28 +03:00
|
|
|
await dispatch('loadCategory', category.id)
|
2018-11-25 20:41:15 +03:00
|
|
|
},
|
|
|
|
async loadCategory (
|
|
|
|
{ commit }: ActionContext<ICategoryState, any>,
|
2019-01-22 09:04:30 +03:00
|
|
|
categoryId: ICategoryInfo['id']
|
2018-11-25 20:41:15 +03:00
|
|
|
): 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'] }
|
2019-01-22 09:04:30 +03:00
|
|
|
): Promise<ICategoryInfo['id']> {
|
2018-10-29 21:17:13 +03:00
|
|
|
const createdId = await CategoryService.createCategory({
|
|
|
|
title,
|
|
|
|
group
|
|
|
|
})
|
|
|
|
dispatch('loadCategoryList')
|
|
|
|
return createdId
|
2019-02-16 10:16:37 +03:00
|
|
|
},
|
2019-03-18 12:48:07 +03:00
|
|
|
async updateCategoryInfo (context: ActionContext<ICategoryState, any>, {id, title, group, status, sections}) {
|
|
|
|
await CategoryService.updateCategoryInfo({id, title, group, status, sections})
|
|
|
|
},
|
2019-02-16 10:16:37 +03:00
|
|
|
async deleteCategory (
|
|
|
|
{ dispatch }: ActionContext<ICategoryState, any>,
|
|
|
|
id: ICategoryInfo['id']
|
|
|
|
): Promise<void> {
|
|
|
|
await CategoryService.deleteCategory(id)
|
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
|