2023-04-12 12:50:54 +03:00

38 lines
1.2 KiB
TypeScript

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(['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,
}
);
}