diff --git a/process/payments/reactions.ts b/process/payments/reactions.ts index c63826f..e5dca45 100644 --- a/process/payments/reactions.ts +++ b/process/payments/reactions.ts @@ -8,7 +8,7 @@ import { shift } from 'radash'; import type { CalculationOptions } from 'stores/calculation/options/types'; import type RootStore from 'stores/root'; import type { Row } from 'stores/tables/payments/types'; -import type { RemoveError } from 'stores/validation/types'; +import ValidationHelper from 'stores/validation/helper'; import { difference } from 'tools/array'; import { makeDisposable } from 'tools/mobx'; import * as seasonsConstants from './lib/seasons-constants'; @@ -558,7 +558,7 @@ export default function paymentsReactions( /** * Валидация */ - let removeError: RemoveError | undefined; + const validationHelper = new ValidationHelper(); reaction( () => { @@ -577,11 +577,12 @@ export default function paymentsReactions( }; }, () => { - if (removeError) removeError(); + validationHelper.removeErrors(); const errorText = validatePaymentsTable(store); if (errorText) { - removeError = $tables.payments.validation.addError(errorText); + const removeError = $tables.payments.validation.addError(errorText); + validationHelper.add(removeError); } }, { diff --git a/process/supplier-agent/lib/create-reactions.ts b/process/supplier-agent/lib/create-reactions.ts index d7326ff..2a51c0d 100644 --- a/process/supplier-agent/lib/create-reactions.ts +++ b/process/supplier-agent/lib/create-reactions.ts @@ -8,7 +8,7 @@ import utc from 'dayjs/plugin/utc'; import type * as CRMTypes from 'graphql/crm.types'; import { reaction } from 'mobx'; import type RootStore from 'stores/root'; -import type { RemoveError } from 'stores/validation/types'; +import ValidationHelper from 'stores/validation/helper'; import { normalizeOptions } from 'tools/entity'; import { makeDisposable } from 'tools/mobx'; @@ -149,15 +149,14 @@ export function validateAgentRewardSumm( const rewardSummTitle = titles[rewardSummField]; - const errors: Array = []; + const validationHelper = new ValidationHelper(); reaction( () => $calculation.element(rewardSummField).getValue() as number, async (rewardSumm) => { const conditionId = $calculation.element(rewardConditionField).getValue(); if (!conditionId) { - errors.forEach((removeError) => removeError()); - errors.length = 0; + validationHelper.removeErrors(); return; } @@ -183,7 +182,7 @@ export function validateAgentRewardSumm( message: `${rewardSummTitle} указан больше условия по агентскому договору!`, }) .err((removeError) => { - errors.push(removeError); + validationHelper.add(removeError); }); $calculation @@ -196,7 +195,7 @@ export function validateAgentRewardSumm( message: `${rewardSummTitle} указан меньше условия по агентскому договору!`, }) .err((removeError) => { - errors.push(removeError); + validationHelper.add(removeError); }); $calculation @@ -208,7 +207,7 @@ export function validateAgentRewardSumm( message: `${rewardSummTitle} указан меньше условия по агентскому договору!`, }) .err((removeError) => { - errors.push(removeError); + validationHelper.add(removeError); }); } ); diff --git a/stores/validation/helper.ts b/stores/validation/helper.ts new file mode 100644 index 0000000..c31f5d5 --- /dev/null +++ b/stores/validation/helper.ts @@ -0,0 +1,20 @@ +import type { ObservableSet } from 'mobx'; +import { observable } from 'mobx'; +import type { RemoveError } from './types'; + +export default class ValidationHelper { + errors: ObservableSet; + + constructor() { + this.errors = observable.set(); + } + + add = (removeError: RemoveError) => { + this.errors.add(removeError); + }; + + removeErrors = () => { + this.errors.forEach((removeError) => removeError()); + this.errors.clear(); + }; +}