1
1
mirror of https://github.com/aelve/guide.git synced 2024-12-25 13:51:45 +03:00
guide/front/client/store/modules/category.ts
avele 469b5b5790
Front/structure refactor and building reconfigure (#277)
* DefferedPromise refactor

* Moved defferedPromise to utils folder

* Base url moved to app file

* Store modules states rewrittten according to vue doc recommendations

* Deffered promise usage moved

* removed unused packages in entries

* Structure refactor, easier building, prod building configured, tsconfig reconfigure removed useless packages

* Update front/index.html

Co-Authored-By: avele <34437766+avele@users.noreply.github.com>

* Update front/postcss.config.js

Co-Authored-By: avele <34437766+avele@users.noreply.github.com>

* Comment rewritten
2019-04-18 01:52:43 +04:00

76 lines
2.2 KiB
TypeScript

import { ActionTree, GetterTree, MutationTree, ActionContext, Module } from 'vuex'
import { ICategoryInfo, ICategoryFull, CategoryService } from 'client/service/Category'
interface ICategoryState {
categoryList: ICategoryInfo[],
category: ICategoryFull
}
const state = (): ICategoryState => ({
categoryList: [],
category: null
})
const getters: GetterTree<ICategoryState, any> = {}
const actions: ActionTree<ICategoryState, any> = {
async reloadCategory ({ dispatch, state }: ActionContext<ICategoryState, any>) {
const category = state.category
if (!category) {
return
}
await dispatch('loadCategory', category.id)
},
async loadCategory (
{ commit }: ActionContext<ICategoryState, any>,
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()
commit('setCategoryList', data)
},
async createCategory (
{ dispatch }: ActionContext<ICategoryState, any>,
{ title, group }: { title: ICategoryInfo['title'], group: ICategoryInfo['group'] }
): Promise<ICategoryInfo['id']> {
const createdId = await CategoryService.createCategory({
title,
group
})
dispatch('loadCategoryList')
return createdId
},
async updateCategoryInfo (context: ActionContext<ICategoryState, any>, {id, title, group, status, sections}) {
await CategoryService.updateCategoryInfo({id, title, group, status, sections})
},
async deleteCategory (
{ dispatch }: ActionContext<ICategoryState, any>,
id: ICategoryInfo['id']
): Promise<void> {
await CategoryService.deleteCategory(id)
}
}
const mutations: MutationTree<ICategoryState> = {
setCategoryList: (state: ICategoryState, payload: ICategoryInfo[]) => {
state.categoryList = payload
},
setCategory: (state: ICategoryState, payload: ICategoryFull) => {
state.category = payload
}
}
const category: Module<ICategoryState, any> = {
namespaced: true,
state,
getters,
actions,
mutations
}
export default category