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

67 lines
1.9 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
}
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)
},
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'] }
): 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
}
}
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