2023-03-16 15:35:44 +03:00

66 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 { autorun, reaction } 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();
reaction(
() =>
$calculation.$values.getValues([
'leaseObjectType',
'engineVolume',
'engineType',
'leaseObjectMotorPower',
'countSeats',
'maxMass',
'leaseObjectCategory',
]),
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,
}
);
}