From 9cf409df2c7d97dbe5a5b6b1ed2de1a6eea975d1 Mon Sep 17 00:00:00 2001 From: Chika Date: Tue, 27 Apr 2021 13:19:40 +0300 Subject: [PATCH] refactor(calculationstore): import actions dynamically remove actions from calculationStore and import in component dynamically --- src/client/hooks/Calculation/useAction.js | 11 ++++++++--- .../Effects/actions/createKP.js | 5 +++-- .../CalculationStore/Effects/actions/index.js | 11 ----------- src/client/stores/CalculationStore/index.ts | 17 ++++------------- src/core/tools/resolve.js | 3 +++ 5 files changed, 18 insertions(+), 29 deletions(-) delete mode 100644 src/client/stores/CalculationStore/Effects/actions/index.js diff --git a/src/client/hooks/Calculation/useAction.js b/src/client/hooks/Calculation/useAction.js index fa5fa77..e71a8f6 100644 --- a/src/client/hooks/Calculation/useAction.js +++ b/src/client/hooks/Calculation/useAction.js @@ -1,7 +1,12 @@ -import { useStores } from '../useStores'; +import { action as resolveAction } from 'core/tools/resolve'; export const useAction = ({ actionName }) => { - const { calculationStore } = useStores(); - const action = calculationStore.actions[actionName]; + const action = () => { + resolveAction({ storeName: 'CalculationStore', actionName }).then( + ({ default: resolvedAction }) => { + resolvedAction(); + }, + ); + }; return { action }; }; diff --git a/src/client/stores/CalculationStore/Effects/actions/createKP.js b/src/client/stores/CalculationStore/Effects/actions/createKP.js index 4d88b12..58bac7c 100644 --- a/src/client/stores/CalculationStore/Effects/actions/createKP.js +++ b/src/client/stores/CalculationStore/Effects/actions/createKP.js @@ -8,10 +8,11 @@ import { toJS } from 'mobx'; import CalculationStore, { calculationUrls } from '../..'; import customValues from '../lib/customValues'; import { quoteFields } from '../lib/queries'; +import calculate from './calculate'; export default async () => { - const { values, tables, actions } = CalculationStore; - const calculationRes = await actions.calculate(); + const { values, tables } = CalculationStore; + const calculationRes = await calculate(); if (!calculationRes) { return; diff --git a/src/client/stores/CalculationStore/Effects/actions/index.js b/src/client/stores/CalculationStore/Effects/actions/index.js deleted file mode 100644 index bac71f5..0000000 --- a/src/client/stores/CalculationStore/Effects/actions/index.js +++ /dev/null @@ -1,11 +0,0 @@ -import calculate from './calculate'; -import createKP from './createKP'; -import createLead from './createLead'; - -export default { - actions: { - calculate, - createKP, - createLead, - }, -}; diff --git a/src/client/stores/CalculationStore/index.ts b/src/client/stores/CalculationStore/index.ts index 3d6b753..59547c7 100644 --- a/src/client/stores/CalculationStore/index.ts +++ b/src/client/stores/CalculationStore/index.ts @@ -9,7 +9,6 @@ import { autorun, makeAutoObservable, reaction } from 'mobx'; import staticData from './Data/static'; import tables from './Data/tables'; import values from './Data/values'; -import actionsEffects from './Effects/actions'; import autorunEffects from './Effects/autorun'; import computedEffects from './Effects/computed'; import reactionEffects from './Effects/reactions'; @@ -47,19 +46,11 @@ export const calculationUrls = makeAutoObservable({ }); const CalculationStore: ICalculationStore = makeAutoObservable( - Object.assign( - {}, - staticData, - values, - tables, - computedEffects, - actionsEffects, - { - stores: { - calculationProcess, - }, + Object.assign({}, staticData, values, tables, computedEffects, { + stores: { + calculationProcess, }, - ), + }), ); autorunEffects.map(autorunEffect => autorun(autorunEffect(CalculationStore))); diff --git a/src/core/tools/resolve.js b/src/core/tools/resolve.js index 3810376..4bd1ef2 100644 --- a/src/core/tools/resolve.js +++ b/src/core/tools/resolve.js @@ -2,3 +2,6 @@ import { lazy } from 'react'; export const container = name => lazy(() => import(`client/Containers/${name}`)); + +export const action = ({ storeName, actionName }) => + import(`client/stores/${storeName}/Effects/actions/${actionName}`);