From a286813d4d530c52a0e2d1d011ef72d718a60a06 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 13 Apr 2023 13:39:30 +0300 Subject: [PATCH] process: add createValidationReaction --- apps/web/process/add-product/reactions.ts | 39 +----- apps/web/process/bonuses/reactions/index.ts | 2 +- .../process/bonuses/reactions/validation.ts | 37 +----- .../process/configurator/reactions/index.ts | 2 +- .../configurator/reactions/validation.ts | 37 +----- apps/web/process/gibdd/reactions.ts | 53 +------- apps/web/process/insurance/reactions.ts | 63 +-------- .../process/leasing-object/reactions/index.ts | 2 +- .../leasing-object/reactions/validation.ts | 68 +--------- apps/web/process/payments/reactions/index.ts | 2 +- .../process/payments/reactions/validation.ts | 58 +-------- apps/web/process/price/reactions/index.ts | 2 +- .../web/process/price/reactions/validation.ts | 46 +------ apps/web/process/recalc/reactions.ts | 38 +----- .../process/supplier-agent/reactions/index.ts | 2 +- .../supplier-agent/reactions/validation.ts | 72 +---------- apps/web/process/tools.ts | 120 ++++++++++++++++++ apps/web/process/used-pl/reactions.ts | 44 +------ 18 files changed, 151 insertions(+), 536 deletions(-) create mode 100644 apps/web/process/tools.ts diff --git a/apps/web/process/add-product/reactions.ts b/apps/web/process/add-product/reactions.ts index c51e731..c48e2b9 100644 --- a/apps/web/process/add-product/reactions.ts +++ b/apps/web/process/add-product/reactions.ts @@ -1,16 +1,12 @@ +import { createValidationReaction } from '../tools'; import type { ProcessContext } from '../types'; import { createValidationSchema } from './validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; import { selectRequirementTelematic } from '@/config/default-options'; import * as CRMTypes from '@/graphql/crm.types'; -import type { Values } from '@/stores/calculation/values/types'; -import ValidationHelper from '@/stores/validation/helper'; import { normalizeOptions } from '@/utils/entity'; -import { debouncedReaction } from '@/utils/mobx'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; -import { uid } from 'radash'; dayjs.extend(utc); @@ -202,35 +198,4 @@ export default function reactions({ store, apolloClient }: ProcessContext) { ); } -const key = uid(7); - -export function validation(context: ProcessContext) { - const { store } = context; - const { $calculation } = store; - const validationSchema = createValidationSchema(context); - - const helper = new ValidationHelper(); - debouncedReaction( - () => - $calculation.$values.getValues(Object.keys(validationSchema._def.schema.shape) as Values[]), - async (values) => { - helper.removeErrors(); - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/bonuses/reactions/index.ts b/apps/web/process/bonuses/reactions/index.ts index cc8d68a..4082721 100644 --- a/apps/web/process/bonuses/reactions/index.ts +++ b/apps/web/process/bonuses/reactions/index.ts @@ -1,2 +1,2 @@ export { default as common } from './common'; -export { default as validation } from './validation'; +export * from './validation'; diff --git a/apps/web/process/bonuses/reactions/validation.ts b/apps/web/process/bonuses/reactions/validation.ts index eff9a3b..5d51679 100644 --- a/apps/web/process/bonuses/reactions/validation.ts +++ b/apps/web/process/bonuses/reactions/validation.ts @@ -1,37 +1,4 @@ import { createValidationSchema } from '../validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; -import type { ProcessContext } from '@/process/types'; -import ValidationHelper from '@/stores/validation/helper'; -import { debouncedReaction } from '@/utils/mobx'; -import { uid } from 'radash'; +import { createValidationReaction } from '@/process/tools'; -const key = uid(7); - -export default function reactions(context: ProcessContext) { - const { store } = context; - const { $calculation } = store; - const validationSchema = createValidationSchema(context); - - const helper = new ValidationHelper(); - debouncedReaction( - () => $calculation.$values.getValues(['product', 'saleBonus']), - async (values) => { - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/configurator/reactions/index.ts b/apps/web/process/configurator/reactions/index.ts index 1b87c74..82b539f 100644 --- a/apps/web/process/configurator/reactions/index.ts +++ b/apps/web/process/configurator/reactions/index.ts @@ -1,4 +1,4 @@ export { default as filters } from './filters'; export { default as unlimited } from './unlimited'; -export { default as validation } from './validation'; +export * from './validation'; export { default as values } from './values'; diff --git a/apps/web/process/configurator/reactions/validation.ts b/apps/web/process/configurator/reactions/validation.ts index 6a523fd..5d51679 100644 --- a/apps/web/process/configurator/reactions/validation.ts +++ b/apps/web/process/configurator/reactions/validation.ts @@ -1,37 +1,4 @@ import { createValidationSchema } from '../validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; -import type { ProcessContext } from '@/process/types'; -import ValidationHelper from '@/stores/validation/helper'; -import { debouncedReaction } from '@/utils/mobx'; -import { uid } from 'radash'; +import { createValidationReaction } from '@/process/tools'; -const key = uid(7); - -export default function reactions(context: ProcessContext) { - const { store } = context; - const { $calculation } = store; - const validationSchema = createValidationSchema(context); - - const helper = new ValidationHelper(); - debouncedReaction( - () => $calculation.$values.getValues(['parmentsDecreasePercent', 'tarif']), - async (values) => { - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/gibdd/reactions.ts b/apps/web/process/gibdd/reactions.ts index 23b3e03..3541dae 100644 --- a/apps/web/process/gibdd/reactions.ts +++ b/apps/web/process/gibdd/reactions.ts @@ -1,19 +1,17 @@ +import { createValidationReaction } from '../tools'; import type { ProcessContext } from '../types'; import helper from './lib/helper'; import { createValidationSchema } from './validation'; import { getTransTax } from '@/api/1c/query'; -import type { Elements } from '@/Components/Calculation/config/map/values'; import { selectObjectCategoryTax } from '@/config/default-options'; import { STALE_TIME } from '@/constants/request'; import * as CRMTypes from '@/graphql/crm.types'; -import ValidationHelper from '@/stores/validation/helper'; import { normalizeOptions } from '@/utils/entity'; -import { debouncedReaction, disposableReaction } from '@/utils/mobx'; +import { disposableReaction } from '@/utils/mobx'; import type { QueryFunctionContext } from '@tanstack/react-query'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; -import { uid } from 'radash'; dayjs.extend(utc); @@ -509,49 +507,4 @@ export function common({ store, apolloClient, queryClient }: ProcessContext) { ); } -const key = uid(7); - -export function validation(context: ProcessContext) { - const { store } = context; - const { $calculation } = store; - const validationSchema = createValidationSchema(context); - - const validationHelper = new ValidationHelper(); - debouncedReaction( - () => - $calculation.$values.getValues([ - 'leaseObjectCategory', - 'maxMass', - 'leaseObjectType', - 'typePTS', - 'objectRegistration', - 'objectCategoryTax', - 'insNSIB', - 'vehicleTaxInYear', - 'vehicleTaxInLeasingPeriod', - 'objectRegionRegistration', - 'regionRegistration', - 'townRegistration', - 'registration', - ]), - async (values) => { - validationHelper.removeErrors(); - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) validationHelper.add(removeError); - }); - }); - } else { - validationHelper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/insurance/reactions.ts b/apps/web/process/insurance/reactions.ts index 2750ee0..d1afbde 100644 --- a/apps/web/process/insurance/reactions.ts +++ b/apps/web/process/insurance/reactions.ts @@ -1,14 +1,12 @@ /* eslint-disable sonarjs/cognitive-complexity */ +import { createValidationReaction } from '../tools'; import type { ProcessContext } from '../types'; import { createValidationSchema } from './validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; import { selectLeaseObjectUseFor } from '@/config/default-options'; import * as CRMTypes from '@/graphql/crm.types'; -import ValidationHelper from '@/stores/validation/helper'; import { normalizeOptions } from '@/utils/entity'; import { debouncedReaction } from '@/utils/mobx'; -import { comparer, reaction, toJS } from 'mobx'; -import { uid } from 'radash'; +import { reaction } from 'mobx'; export function common({ store, apolloClient }: ProcessContext) { const { $calculation, $tables } = store; @@ -273,59 +271,4 @@ export function common({ store, apolloClient }: ProcessContext) { ); } -const key = uid(7); - -export function validation(context: ProcessContext) { - const { $calculation, $tables } = context.store; - - const validationSchema = createValidationSchema(context); - const helper = new ValidationHelper(); - - debouncedReaction( - () => { - const values = $calculation.$values.getValues([ - 'leasingPeriod', - 'quote', - 'recalcWithRevision', - 'leasingWithoutKasko', - 'insDecentral', - ]); - - return { - insurance: { - values: { - fingap: toJS($tables.insurance.row('fingap').getValues()), - kasko: toJS($tables.insurance.row('kasko').getValues()), - osago: toJS($tables.insurance.row('osago').getValues()), - }, - }, - ...values, - }; - }, - async (values) => { - helper.removeErrors(); - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Array).forEach((elementName) => { - if (elementName === 'insurance') { - const removeError = $tables.insurance.setError({ key, message }); - if (removeError) helper.add(removeError); - } else { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - } - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - equals: comparer.structural, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/leasing-object/reactions/index.ts b/apps/web/process/leasing-object/reactions/index.ts index cc8d68a..4082721 100644 --- a/apps/web/process/leasing-object/reactions/index.ts +++ b/apps/web/process/leasing-object/reactions/index.ts @@ -1,2 +1,2 @@ export { default as common } from './common'; -export { default as validation } from './validation'; +export * from './validation'; diff --git a/apps/web/process/leasing-object/reactions/validation.ts b/apps/web/process/leasing-object/reactions/validation.ts index 4def11c..e47f83f 100644 --- a/apps/web/process/leasing-object/reactions/validation.ts +++ b/apps/web/process/leasing-object/reactions/validation.ts @@ -1,68 +1,4 @@ +import { createValidationReaction } from '../../tools'; import { createValidationSchema } from '../validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; -import type { ProcessContext } from '@/process/types'; -import ValidationHelper from '@/stores/validation/helper'; -import { debouncedReaction } from '@/utils/mobx'; -import { autorun } from 'mobx'; -import { uid } from 'radash'; -const key = uid(7); - -export default function reactions(context: ProcessContext) { - const { $calculation } = context.store; - - /** - * Если model содержит данные и по связи Модель-Комплектация в CRM у данной модели есть связанные активные записи Комплектаций, - * то configuration становится обязательным для заполнения, иначе configuration не обязателен для заполнения - */ - autorun( - () => { - const selectConfiguration = $calculation.element('selectConfiguration'); - if (selectConfiguration.getOptions()?.length > 0 && !selectConfiguration.getValue()) { - selectConfiguration.setError({ - key, - message: 'Не заполнено поле', - }); - } else { - selectConfiguration.removeError({ key }); - } - }, - { - delay: 10, - } - ); - - const validationSchema = createValidationSchema(context); - const helper = new ValidationHelper(); - - debouncedReaction( - () => - $calculation.$values.getValues([ - 'leaseObjectType', - 'engineVolume', - 'engineType', - 'leaseObjectMotorPower', - 'countSeats', - 'maxMass', - 'leaseObjectCategory', - ]), - async (values) => { - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/payments/reactions/index.ts b/apps/web/process/payments/reactions/index.ts index cc8d68a..4082721 100644 --- a/apps/web/process/payments/reactions/index.ts +++ b/apps/web/process/payments/reactions/index.ts @@ -1,2 +1,2 @@ export { default as common } from './common'; -export { default as validation } from './validation'; +export * from './validation'; diff --git a/apps/web/process/payments/reactions/validation.ts b/apps/web/process/payments/reactions/validation.ts index 87154e2..e47f83f 100644 --- a/apps/web/process/payments/reactions/validation.ts +++ b/apps/web/process/payments/reactions/validation.ts @@ -1,58 +1,4 @@ -/* eslint-disable sonarjs/cognitive-complexity */ +import { createValidationReaction } from '../../tools'; import { createValidationSchema } from '../validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; -import type { ProcessContext } from '@/process/types'; -import ValidationHelper from '@/stores/validation/helper'; -import { comparer, reaction, toJS } from 'mobx'; -import { uid } from 'radash'; -const key = uid(7); - -export default function reactions(context: ProcessContext) { - const { $calculation, $tables } = context.store; - - const validationSchema = createValidationSchema(); - const helper = new ValidationHelper(); - - reaction( - () => { - const payments = toJS($tables.payments.values); - const values = $calculation.$values.getValues([ - 'graphType', - 'highSeasonStart', - 'leasingPeriod', - 'seasonType', - 'insNSIB', - 'lastPaymentRub', - ]); - - return { - payments: { values: payments }, - ...values, - }; - }, - async (values) => { - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Array).forEach((elementName) => { - if (elementName === 'payments') { - const removeError = $tables.payments.setError({ key, message }); - if (removeError) helper.add(removeError); - } else { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - } - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 100, - equals: comparer.structural, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/price/reactions/index.ts b/apps/web/process/price/reactions/index.ts index 04e67d6..c0a451a 100644 --- a/apps/web/process/price/reactions/index.ts +++ b/apps/web/process/price/reactions/index.ts @@ -1,3 +1,3 @@ export { default as common } from './common'; export { default as computed } from './computed'; -export { default as validation } from './validation'; +export * from './validation'; diff --git a/apps/web/process/price/reactions/validation.ts b/apps/web/process/price/reactions/validation.ts index 329ae31..e47f83f 100644 --- a/apps/web/process/price/reactions/validation.ts +++ b/apps/web/process/price/reactions/validation.ts @@ -1,46 +1,4 @@ +import { createValidationReaction } from '../../tools'; import { createValidationSchema } from '../validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; -import type { ProcessContext } from '@/process/types'; -import ValidationHelper from '@/stores/validation/helper'; -import { debouncedReaction } from '@/utils/mobx'; -import { uid } from 'radash'; -const key = uid(7); - -export default function reactions(context: ProcessContext) { - const { store } = context; - const { $calculation } = store; - const validationSchema = createValidationSchema(context); - - const helper = new ValidationHelper(); - debouncedReaction( - () => - $calculation.$values.getValues([ - 'VATInLeaseObjectPrice', - 'leaseObjectPriceWthtVAT', - 'product', - 'supplierDiscountRub', - 'plPriceRub', - 'firstPaymentRub', - 'subsidySum', - ]), - async (values) => { - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/recalc/reactions.ts b/apps/web/process/recalc/reactions.ts index f6c9f93..0b6fde6 100644 --- a/apps/web/process/recalc/reactions.ts +++ b/apps/web/process/recalc/reactions.ts @@ -1,13 +1,11 @@ import type { ProcessContext } from '../types'; import { createValidationSchema } from './validation'; import type { Elements } from '@/Components/Calculation/config/map/values'; -import type { Values } from '@/stores/calculation/values/types'; -import ValidationHelper from '@/stores/validation/helper'; -import { debouncedReaction, disposableReaction } from '@/utils/mobx'; +import { createValidationReaction } from '@/process/tools'; +import { disposableReaction } from '@/utils/mobx'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; -import { uid } from 'radash'; dayjs.extend(utc); @@ -142,34 +140,4 @@ export function common({ store }: ProcessContext) { } } -const key = uid(7); - -export function validation(context: ProcessContext) { - const { store } = context; - const { $calculation } = store; - const validationSchema = createValidationSchema(context); - - const helper = new ValidationHelper(); - debouncedReaction( - () => - $calculation.$values.getValues(Object.keys(validationSchema._def.schema.shape) as Values[]), - async (values) => { - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/supplier-agent/reactions/index.ts b/apps/web/process/supplier-agent/reactions/index.ts index 9a46f6f..1912f70 100644 --- a/apps/web/process/supplier-agent/reactions/index.ts +++ b/apps/web/process/supplier-agent/reactions/index.ts @@ -11,4 +11,4 @@ function common(context: ProcessContext) { export { common }; -export { default as validation } from './validation'; +export * from './validation'; diff --git a/apps/web/process/supplier-agent/reactions/validation.ts b/apps/web/process/supplier-agent/reactions/validation.ts index 38524bb..e47f83f 100644 --- a/apps/web/process/supplier-agent/reactions/validation.ts +++ b/apps/web/process/supplier-agent/reactions/validation.ts @@ -1,72 +1,4 @@ +import { createValidationReaction } from '../../tools'; import { createValidationSchema } from '../validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; -import type { ProcessContext } from '@/process/types'; -import ValidationHelper from '@/stores/validation/helper'; -import { debouncedReaction } from '@/utils/mobx'; -import { uid } from 'radash'; -const key = uid(7); - -export default function reactions(context: ProcessContext) { - const { store } = context; - const { $calculation } = store; - const validationSchema = createValidationSchema(context); - - const helper = new ValidationHelper(); - debouncedReaction( - () => { - const values = $calculation.$values.getValues([ - 'calcBrokerRewardCondition', - 'calcBrokerRewardSum', - 'calcDoubleAgentRewardCondition', - 'calcDoubleAgentRewardSumm', - 'dealer', - 'dealerBroker', - 'calcFinDepartment', - 'dealerBrokerRewardCondition', - 'dealerBrokerRewardSumm', - 'dealerPerson', - 'dealerRewardCondition', - 'dealerRewardSumm', - 'finDepartmentRewardCondtion', - 'finDepartmentRewardSumm', - 'indAgent', - 'indAgentRewardCondition', - 'indAgentRewardSumm', - 'calcDoubleAgent', - 'calcBroker', - ]); - - const options = ( - [ - 'selectCalcBrokerRewardCondition', - 'selectCalcDoubleAgentRewardCondition', - 'selectDealerBrokerRewardCondition', - 'selectDealerRewardCondition', - 'selectFinDepartmentRewardCondtion', - 'selectIndAgentRewardCondition', - ] as Elements[] - ).map((elementName) => $calculation.element(elementName).getOptions()); - - return { options, values }; - }, - async ({ values }) => { - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema); diff --git a/apps/web/process/tools.ts b/apps/web/process/tools.ts new file mode 100644 index 0000000..5acd553 --- /dev/null +++ b/apps/web/process/tools.ts @@ -0,0 +1,120 @@ +/* eslint-disable sonarjs/cognitive-complexity */ +import { debouncedReaction } from '../utils/mobx'; +import type { ProcessContext } from './types'; +import type { Elements } from '@/Components/Calculation/config/map/values'; +import type { Values } from '@/stores/calculation/values/types'; +import ValidationHelper from '@/stores/validation/helper'; +import { comparer, toJS } from 'mobx'; +import { uid } from 'radash'; +import type { ZodTypeAny } from 'zod'; + +export function createValidationReaction( + createValidationSchema: (context: ProcessContext) => T +) { + const key = uid(7); + + return (context: ProcessContext) => { + const validationSchema = createValidationSchema(context); + const shapeValues = Object.keys(validationSchema._def.schema.shape) as string[]; + + const { store } = context; + const { $calculation, $tables } = store; + + const helper = new ValidationHelper(); + + if (shapeValues.includes('insurance')) { + debouncedReaction( + () => ({ + ...$calculation.$values.getValues(shapeValues as Values[]), + insurance: { + values: { + fingap: toJS($tables.insurance.row('fingap').getValues()), + kasko: toJS($tables.insurance.row('kasko').getValues()), + osago: toJS($tables.insurance.row('osago').getValues()), + }, + }, + }), + async (values) => { + helper.removeErrors(); + const validationResult = await validationSchema.safeParseAsync(values); + + if (validationResult.success === false) { + validationResult.error.errors.forEach(({ path, message }) => { + (path as Array).forEach((elementName) => { + if (elementName === 'insurance') { + const removeError = $tables.insurance.setError({ key, message }); + if (removeError) helper.add(removeError); + } else { + const removeError = $calculation.element(elementName).setError({ key, message }); + if (removeError) helper.add(removeError); + } + }); + }); + } else { + helper.removeErrors(); + } + }, + { + delay: 1, + equals: comparer.structural, + wait: 100, + } + ); + } else if (shapeValues.includes('payments')) { + debouncedReaction( + () => ({ + ...$calculation.$values.getValues(shapeValues as Values[]), + payments: { values: toJS($tables.payments.values) }, + }), + async (values) => { + helper.removeErrors(); + const validationResult = await validationSchema.safeParseAsync(values); + + if (validationResult.success === false) { + validationResult.error.errors.forEach(({ path, message }) => { + (path as Array).forEach((elementName) => { + if (elementName === 'payments') { + const removeError = $tables.payments.setError({ key, message }); + if (removeError) helper.add(removeError); + } else { + const removeError = $calculation.element(elementName).setError({ key, message }); + if (removeError) helper.add(removeError); + } + }); + }); + } else { + helper.removeErrors(); + } + }, + { + delay: 1, + equals: comparer.structural, + wait: 100, + } + ); + } else { + debouncedReaction( + () => $calculation.$values.getValues(shapeValues as Values[]), + async (values) => { + helper.removeErrors(); + const validationResult = await validationSchema.safeParseAsync(values); + + if (validationResult.success === false) { + validationResult.error.errors.forEach(({ path, message }) => { + (path as Elements[]).forEach((elementName) => { + const removeError = $calculation.element(elementName).setError({ key, message }); + if (removeError) helper.add(removeError); + }); + }); + } else { + helper.removeErrors(); + } + }, + { + delay: 1, + wait: 100, + } + ); + } + }; +} diff --git a/apps/web/process/used-pl/reactions.ts b/apps/web/process/used-pl/reactions.ts index fe07f27..c17e34d 100644 --- a/apps/web/process/used-pl/reactions.ts +++ b/apps/web/process/used-pl/reactions.ts @@ -1,11 +1,8 @@ import type { ProcessContext } from '../types'; import { createValidationSchema } from './validation'; -import type { Elements } from '@/Components/Calculation/config/map/values'; import * as CRMTypes from '@/graphql/crm.types'; -import ValidationHelper from '@/stores/validation/helper'; -import { debouncedReaction } from '@/utils/mobx'; +import { createValidationReaction } from '@/process/tools'; import { reaction } from 'mobx'; -import { uid } from 'radash'; export function common({ store, apolloClient }: ProcessContext) { const { $calculation } = store; @@ -241,41 +238,4 @@ export function common({ store, apolloClient }: ProcessContext) { ); } -const key = uid(7); - -export function validation(context: ProcessContext) { - const { store } = context; - const { $calculation } = store; - const validationSchema = createValidationSchema(context); - - const helper = new ValidationHelper(); - debouncedReaction( - () => - $calculation.$values.getValues([ - 'engineHours', - 'leaseObjectCategory', - 'leaseObjectType', - 'leaseObjectUsed', - 'mileage', - 'vin', - ]), - async (values) => { - const validationResult = await validationSchema.safeParseAsync(values); - - if (validationResult.success === false) { - validationResult.error.errors.forEach(({ path, message }) => { - (path as Elements[]).forEach((elementName) => { - const removeError = $calculation.element(elementName).setError({ key, message }); - if (removeError) helper.add(removeError); - }); - }); - } else { - helper.removeErrors(); - } - }, - { - delay: 1, - wait: 100, - } - ); -} +export const validation = createValidationReaction(createValidationSchema);