From ede4f678ddf6563670e5cd445a4d8e7a415c37c6 Mon Sep 17 00:00:00 2001 From: avele Date: Mon, 4 Nov 2019 02:28:47 +0400 Subject: [PATCH] Helper set function for vuex --- front/client/store/helpers.ts | 6 ++++ front/client/store/modules/category.ts | 9 ++--- front/client/store/modules/categoryItem.ts | 40 +++++++++------------- 3 files changed, 26 insertions(+), 29 deletions(-) create mode 100644 front/client/store/helpers.ts diff --git a/front/client/store/helpers.ts b/front/client/store/helpers.ts new file mode 100644 index 0000000..4d25265 --- /dev/null +++ b/front/client/store/helpers.ts @@ -0,0 +1,6 @@ +// Set property on state +// setUser: set('user') +// commit('setUser', { name: 'foo' }) +export const set = key => (state, val) => { state[key] = val } + +// https://gist.github.com/rhythnic/6521495650a215ccab8bf7120949fb7d more helpers \ No newline at end of file diff --git a/front/client/store/modules/category.ts b/front/client/store/modules/category.ts index 7b3f7a9..8e3b5e0 100644 --- a/front/client/store/modules/category.ts +++ b/front/client/store/modules/category.ts @@ -1,6 +1,7 @@ import { ActionTree, GetterTree, MutationTree, ActionContext, Module } from 'vuex' import { ICategoryInfo, ICategoryFull, CategoryService } from 'client/service/Category' import { ICategoryItem } from 'client/service/CategoryItem' +import { set } from '../helpers' interface ICategoryState { categoryList: ICategoryInfo[], @@ -97,12 +98,8 @@ const actions: ActionTree = { } const mutations: MutationTree = { - setCategoryList: (state: ICategoryState, payload: ICategoryInfo[]) => { - state.categoryList = payload - }, - setCategory: (state: ICategoryState, payload: ICategoryFull) => { - state.category = payload - }, + setCategoryList: set('categoryList'), + setCategory: set('category'), toggleSectionEditState (state, { sectionName, itemId }) { const sectionEditState = state.itemsSectionsInEdit[sectionName] diff --git a/front/client/store/modules/categoryItem.ts b/front/client/store/modules/categoryItem.ts index 87cc0ea..1c17884 100644 --- a/front/client/store/modules/categoryItem.ts +++ b/front/client/store/modules/categoryItem.ts @@ -7,19 +7,14 @@ import { ITrait } from 'client/service/CategoryItem' -interface ICategoryItemState { - categoryItemList: ICategoryItem[] -} - -const state = (): ICategoryItemState => ({ - categoryItemList: [] +const state = (): {} => ({ }) -const getters: GetterTree = {} +const getters: GetterTree<{}, any> = {} -const actions: ActionTree = { +const actions: ActionTree<{}, any> = { async createItem ( - { dispatch }: ActionContext, + { dispatch }: ActionContext<{}, any>, { category, name, @@ -39,37 +34,37 @@ const actions: ActionTree = { await CategoryItemService.deleteItemById(id) }, async updateItemInfo ( - context: ActionContext, + context: ActionContext<{}, any>, { id, body }: { id: ICategoryItem['id'], body: ICategoryItemInfo } ): Promise { await CategoryItemService.updateItemInfo(id, body) }, async moveItem ( - context: ActionContext, + context: ActionContext<{}, any>, { id, direction }: { id: ICategoryItem['id'], direction: string } ): Promise { await CategoryItemService.moveItem(id, direction) }, async updateItemSummary ( - context: ActionContext, + context: ActionContext<{}, any>, { id, original, modified }: { id: ICategoryItem['id'], original: ICategoryItem['summary'], modified: ICategoryItem['summary'] } ): Promise { await CategoryItemService.updateItemSummary(id, original, modified) }, async updateItemEcosystem ( - context: ActionContext, + context: ActionContext<{}, any>, { id, original, modified }: { id: ICategoryItem['id'], original: ICategoryItem['ecosystem'], modified: ICategoryItem['ecosystem'] } ): Promise { await CategoryItemService.updateItemEcosystem(id, original, modified) }, async updateItemNotes ( - context: ActionContext, + context: ActionContext<{}, any>, { id, original, modified }: { id: ICategoryItem['id'], original: ICategoryItem['notes'], modified: ICategoryItem['notes'] } ): Promise { await CategoryItemService.updateItemNotes(id, original, modified) }, async updateItemTrait ( - context: ActionContext, + context: ActionContext<{}, any>, { itemId, traitId, @@ -85,7 +80,7 @@ const actions: ActionTree = { await CategoryItemService.updateItemTrait(itemId, traitId, original, modified) }, async moveItemTrait ( - context: ActionContext, + context: ActionContext<{}, any>, { itemId, traitId, @@ -99,7 +94,7 @@ const actions: ActionTree = { await CategoryItemService.moveItemTrait(itemId, traitId, direction) }, async deleteItemTrait ( - context: ActionContext, + context: ActionContext<{}, any>, { itemId, traitId, @@ -111,7 +106,7 @@ const actions: ActionTree = { await CategoryItemService.deleteItemTrait(itemId, traitId) }, async createItemTrait ( - context: ActionContext, + context: ActionContext<{}, any>, { itemId, type, content @@ -125,13 +120,12 @@ const actions: ActionTree = { } } -const mutations: MutationTree = { - setCategoryItem: (state: ICategoryItemState, payload: ICategoryItem[]) => { - state.categoryItemList = payload - } + +const mutations: MutationTree<{}> = { } -const categoryItem: Module = { + +const categoryItem: Module<{}, any> = { namespaced: true, state, getters,