From be071545a6352a049f86854714d1e2a59beb12fd Mon Sep 17 00:00:00 2001 From: Chika Date: Wed, 9 Sep 2020 14:43:03 +0300 Subject: [PATCH] fix calculationstore type --- .../CalculationStore/Effects/autorun.ts | 10 +-- .../CalculationStore/Effects/reaction.ts | 85 ++++++++++++++----- .../stores/CalculationStore/Effects/when.ts | 8 +- src/client/stores/CalculationStore/index.ts | 3 +- src/core/types/effect.ts | 9 +- src/core/types/stores.ts | 16 ++++ 6 files changed, 96 insertions(+), 35 deletions(-) create mode 100644 src/core/types/stores.ts diff --git a/src/client/stores/CalculationStore/Effects/autorun.ts b/src/client/stores/CalculationStore/Effects/autorun.ts index 3dcb669..2fba7d5 100644 --- a/src/client/stores/CalculationStore/Effects/autorun.ts +++ b/src/client/stores/CalculationStore/Effects/autorun.ts @@ -1,11 +1,11 @@ import { IAutorunEffect } from 'core/types/effect'; const autorunEffects: IAutorunEffect[] = [ - calculationStore => () => { - if (parseInt(calculationStore.values.price) > 25) { - calculationStore.setValue('one', 100500); - } - } + // calculationStore => () => { + // if (parseInt(calculationStore.values.price) > 25) { + // calculationStore.setValue('one', 100500); + // } + // } ]; export default autorunEffects; diff --git a/src/client/stores/CalculationStore/Effects/reaction.ts b/src/client/stores/CalculationStore/Effects/reaction.ts index ccdcea7..ed4b823 100644 --- a/src/client/stores/CalculationStore/Effects/reaction.ts +++ b/src/client/stores/CalculationStore/Effects/reaction.ts @@ -1,29 +1,74 @@ import { IReactionEffect } from 'core/types/effect'; -import { Status } from 'core/types/elements'; +import { Status } from 'core/types/statuses'; const reactionEffects: IReactionEffect[] = [ calculationStore => ({ - expression: () => calculationStore.values.one, - effect: one => console.log('one ', one) - }), - calculationStore => ({ - expression: () => calculationStore.values.price, - effect: price => console.log('price: ', price) - }), - calculationStore => ({ - expression: () => calculationStore.values.cbx, - effect: cbx => { - if (cbx === true) { - calculationStore.statuses.cars = Status.Disabled; - } else { - calculationStore.statuses.cars = Status.Default; + expression: () => { + const { channel } = calculationStore.values; + return channel; + }, + effect: channel => { + switch (channel) { + case 100000000: + calculationStore.setStatus('selectSupplier', Status.Default); + calculationStore.setStatus('selectAgent', Status.Default); + + calculationStore.setStatus('selectFinDepartment', Status.Disabled); + calculationStore.setValue('finDepartment', undefined); + + calculationStore.setStatus('selectBroker', Status.Disabled); + calculationStore.setValue('broker', undefined); + break; + + case 100000001: + calculationStore.setStatus('selectSupplier', Status.Default); + calculationStore.setStatus('selectAgent', Status.Default); + + calculationStore.setStatus('selectFinDepartment', Status.Default); + + calculationStore.setStatus('selectBroker', Status.Disabled); + calculationStore.setValue('broker', undefined); + break; + case 100000002: + calculationStore.setStatus('selectSupplier', Status.Disabled); + calculationStore.setValue('supplier', undefined); + + calculationStore.setStatus('selectAgent', Status.Default); + + calculationStore.setStatus('selectFinDepartment', Status.Disabled); + calculationStore.setValue('finDepartment', undefined); + + calculationStore.setStatus('selectBroker', Status.Disabled); + calculationStore.setValue('broker', undefined); + break; + case 100000003: + calculationStore.setStatus('selectSupplier', Status.Disabled); + calculationStore.setValue('supplier', undefined); + + calculationStore.setStatus('selectAgent', Status.Default); + calculationStore.setValue('agent', undefined); + + calculationStore.setStatus('selectFinDepartment', Status.Disabled); + calculationStore.setValue('finDepartment', undefined); + + calculationStore.setStatus('selectBroker', Status.Default); + break; + case 100000004: + default: + calculationStore.setStatus('selectSupplier', Status.Disabled); + calculationStore.setValue('supplier', undefined); + + calculationStore.setStatus('selectAgent', Status.Disabled); + calculationStore.setValue('agent', undefined); + + calculationStore.setStatus('selectFinDepartment', Status.Disabled); + calculationStore.setValue('finDepartment', undefined); + + calculationStore.setStatus('selectBroker', Status.Disabled); + calculationStore.setValue('broker', undefined); + break; } - console.log('cbx: ', cbx); } - }), - calculationStore => ({ - expression: () => calculationStore.values.cars, - effect: cars => console.log('cars: ', cars) }) ]; diff --git a/src/client/stores/CalculationStore/Effects/when.ts b/src/client/stores/CalculationStore/Effects/when.ts index ff4f613..9a7045b 100644 --- a/src/client/stores/CalculationStore/Effects/when.ts +++ b/src/client/stores/CalculationStore/Effects/when.ts @@ -1,10 +1,10 @@ import { IWhenEffect } from 'core/types/effect'; const whenEffects: IWhenEffect[] = [ - calculationStore => ({ - predicate: () => parseInt(calculationStore.values.one) === 5, - effect: () => console.log(calculationStore.values.one) - }) + // calculationStore => ({ + // predicate: () => parseInt(calculationStore.values.one) === 5, + // effect: () => console.log(calculationStore.values.one) + // }) ]; export default whenEffects; diff --git a/src/client/stores/CalculationStore/index.ts b/src/client/stores/CalculationStore/index.ts index 8d23156..9d6eebd 100644 --- a/src/client/stores/CalculationStore/index.ts +++ b/src/client/stores/CalculationStore/index.ts @@ -11,8 +11,9 @@ import autorunEffects from './Effects/autorun'; import computedEffects from './Effects/computed'; import reactionEffects from './Effects/reaction'; import whenEffects from './Effects/when'; +import { ICalculationStore } from 'core/types/stores'; -const CalculationStore = observable( +const CalculationStore: ICalculationStore = observable( assignProperties( { values: initialValues, diff --git a/src/core/types/effect.ts b/src/core/types/effect.ts index d2585e9..0384c69 100644 --- a/src/core/types/effect.ts +++ b/src/core/types/effect.ts @@ -1,23 +1,22 @@ -import CalculationStore from 'client/stores/CalculationStore'; import CommonStore from 'client/stores/CommonStore'; import { IReactionPublic, Lambda } from 'mobx'; +import { ICalculationStore } from './stores'; -type TCalculationStore = typeof CalculationStore; type TCommonStore = typeof CommonStore; export interface IAutorunEffect { - (CalculationStore: TCalculationStore, CommonStore?: TCommonStore): () => void; + (CalculationStore: ICalculationStore, CommonStore?: TCommonStore): () => void; } export interface IReactionEffect { - (CalculationStore: TCalculationStore): { + (CalculationStore: ICalculationStore): { expression: (r: IReactionPublic) => any; effect: (arg: any, r: IReactionPublic) => void; }; } export interface IWhenEffect { - (CalculationStore: TCalculationStore): { + (CalculationStore: ICalculationStore): { predicate: () => boolean; effect: Lambda; }; diff --git a/src/core/types/stores.ts b/src/core/types/stores.ts new file mode 100644 index 0000000..ea4135b --- /dev/null +++ b/src/core/types/stores.ts @@ -0,0 +1,16 @@ +import { TValues, ValuesNames } from './values'; +import { TElements, ElementsNames } from './elements'; +import { TStatuses, Status } from './statuses'; + +export interface ICalculationStore { + values: TValues; + options: TElements; + statuses: TStatuses; + filters: any; + + getValue: (sourceValueName: ValuesNames) => any; + getStatus: (elementName: ElementsNames) => Status; + + setValue: (sourceValueName: ValuesNames, newValue: any) => void; + setStatus: (elementName: ElementsNames, status: Status) => void; +}