33 lines
1.1 KiB
TypeScript
33 lines
1.1 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 { 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(['parmentsDecreasePercent', 'tarif']),
|
|
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);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
);
|
|
}
|