From 2a4203914b6a4cdffdaaa24b2a2f5ef0280bdf5c Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 2 Feb 2023 20:00:08 +0300 Subject: [PATCH] move options elements validation to process/calculate --- .../process/calculate/reactions/validation.ts | 56 ++++++++++++++++++ apps/web/stores/calculation/helpers.ts | 59 ------------------- apps/web/stores/calculation/index.ts | 3 - 3 files changed, 56 insertions(+), 62 deletions(-) delete mode 100644 apps/web/stores/calculation/helpers.ts diff --git a/apps/web/process/calculate/reactions/validation.ts b/apps/web/process/calculate/reactions/validation.ts index 72b4f10..d58796d 100644 --- a/apps/web/process/calculate/reactions/validation.ts +++ b/apps/web/process/calculate/reactions/validation.ts @@ -1,8 +1,20 @@ +import types from 'Components/Calculation/config/elements-types'; +import type * as Values from 'Components/Calculation/config/map/values'; import { reaction } from 'mobx'; import type { ReactionsContext } from 'process/types'; +import type { BaseOption } from 'ui/elements/types'; + +function hasInvalidValue(value: unknown, options: BaseOption[]) { + if (value === null) { + return false; + } + + return (value && !options?.length) || !options.some((x) => x.value === value); +} export default function validationReactions({ store }: ReactionsContext) { const { $calculation, $tables } = store; + reaction( () => { const hasElementsErrors = Object.values($calculation.$validation).some( @@ -27,4 +39,48 @@ export default function validationReactions({ store }: ReactionsContext) { fireImmediately: true, } ); + + /** + * Проверяем, что выбранное значение элемента есть в списке + */ + (Object.keys(types) as Values.Elements[]).forEach((elementName) => { + const type = types[elementName]; + if (type().typeName !== 'Options') { + return; + } + + const element = $calculation.element(elementName); + + reaction( + () => { + const options = element.getOptions(); + const value = element.getValue(); + + return { + value, + options, + }; + }, + ({ value, options }) => { + element.validate({ + invalid: hasInvalidValue(value, options), + message: 'Выбранное значение отсутствует в списке', + silent: true, + }); + }, + { + delay: 100, + } + ); + + reaction( + () => element.getOptions(), + (options) => { + const value = element.getValue(); + if (hasInvalidValue(value, options)) { + element.resetValue(); + } + } + ); + }); } diff --git a/apps/web/stores/calculation/helpers.ts b/apps/web/stores/calculation/helpers.ts deleted file mode 100644 index d47fd0a..0000000 --- a/apps/web/stores/calculation/helpers.ts +++ /dev/null @@ -1,59 +0,0 @@ -import types from 'Components/Calculation/config/elements-types'; -import type * as Values from 'Components/Calculation/config/map/values'; -import { reaction } from 'mobx'; -import type { BaseOption } from 'ui/elements/types'; -import type CalculationStore from '.'; - -export function hasInvalidValue(value: unknown, options: BaseOption[]) { - if (value === null) { - return false; - } - - return (value && !options?.length) || !options.some((x) => x.value === value); -} - -export function createReactions($calculation: CalculationStore) { - /** - * Проверяем, что выбранное значение элемента есть в списке - */ - (Object.keys(types) as Values.Elements[]).forEach((elementName) => { - const type = types[elementName]; - if (type().typeName !== 'Options') { - return; - } - - const element = $calculation.element(elementName); - - reaction( - () => { - const options = element.getOptions(); - const value = element.getValue(); - - return { - value, - options, - }; - }, - ({ value, options }) => { - element.validate({ - invalid: hasInvalidValue(value, options), - message: 'Выбранное значение отсутствует в списке', - silent: true, - }); - }, - { - delay: 100, - } - ); - - reaction( - () => element.getOptions(), - (options) => { - const value = element.getValue(); - if (hasInvalidValue(value, options)) { - element.resetValue(); - } - } - ); - }); -} diff --git a/apps/web/stores/calculation/index.ts b/apps/web/stores/calculation/index.ts index 36fcbfe..bf62329 100644 --- a/apps/web/stores/calculation/index.ts +++ b/apps/web/stores/calculation/index.ts @@ -7,7 +7,6 @@ import type RootStore from 'stores/root'; import Validation from 'stores/validation'; import type { BaseOption } from 'ui/elements/types'; import type { RemoveError, ValidationParams } from '../validation/types'; -import { createReactions } from './helpers'; import OptionsStore from './options'; import StatusStore from './statuses'; import ValuesStore from './values'; @@ -23,8 +22,6 @@ export default class CalculationStore { this.$status = new StatusStore(rootStore); this.$options = new OptionsStore(rootStore); this.$validation = observable.object({}); - - createReactions(this); } private createElementValidation = (elementName: E) => {