66 lines
2.1 KiB
TypeScript
66 lines
2.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 { 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,
|
||
}
|
||
);
|
||
}
|