46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
/* eslint-disable @typescript-eslint/naming-convention */
|
|
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 { reaction } from '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();
|
|
reaction(
|
|
() =>
|
|
$calculation.$values.getValues([
|
|
'VATInLeaseObjectPrice',
|
|
'leaseObjectPriceWthtVAT',
|
|
'product',
|
|
'supplierDiscountRub',
|
|
'plPriceRub',
|
|
'firstPaymentRub',
|
|
'subsidySum',
|
|
]),
|
|
async (values) => {
|
|
helper.removeErrors();
|
|
const validationResult = await validationSchema.safeParseAsync(values);
|
|
|
|
if (!validationResult.success) {
|
|
validationResult.error.errors.forEach(({ path, message }) => {
|
|
(path as Elements[]).forEach((elementName) => {
|
|
const removeError = $calculation.element(elementName).setError({ key, message });
|
|
if (removeError) helper.add(removeError);
|
|
});
|
|
});
|
|
}
|
|
},
|
|
{
|
|
delay: 100,
|
|
}
|
|
);
|
|
}
|