1
1
mirror of https://github.com/aelve/guide.git synced 2024-12-26 06:11:33 +03:00
guide/front/client/store/modules/category.ts

76 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-09-13 10:51:20 +03:00
import { ActionTree, GetterTree, MutationTree, ActionContext, Module } from 'vuex'
import { ICategoryInfo, ICategoryFull, CategoryService } from 'client/service/Category'
2018-09-13 10:51:20 +03:00
interface ICategoryState {
categoryList: ICategoryInfo[],
category: ICategoryFull
2018-09-13 10:51:20 +03:00
}
const state = (): ICategoryState => ({
categoryList: [],
category: null
})
2018-09-13 10:51:20 +03:00
const getters: GetterTree<ICategoryState, any> = {}
2018-09-13 10:51:20 +03:00
const actions: ActionTree<ICategoryState, any> = {
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)
},
async loadCategory (
{ commit }: ActionContext<ICategoryState, any>,
2019-01-22 09:04:30 +03:00
categoryId: ICategoryInfo['id']
): Promise<any> {
const data: ICategoryFull = await CategoryService.getCategoryById(categoryId)
// TODO create set function for all the store
commit('setCategory', data)
},
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
},
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
},
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
}
}
const mutations: MutationTree<ICategoryState> = {
setCategoryList: (state: ICategoryState, payload: ICategoryInfo[]) => {
2018-09-13 10:51:20 +03:00
state.categoryList = payload
},
setCategory: (state: ICategoryState, payload: ICategoryFull) => {
state.category = payload
2018-09-13 10:51:20 +03:00
}
}
const category: Module<ICategoryState, any> = {
2018-09-13 10:51:20 +03:00
namespaced: true,
state,
getters,
actions,
mutations
}
2018-09-13 10:51:20 +03:00
export default category