process/tools: refactor createValidationReaction

This commit is contained in:
vchikalkin 2023-04-13 14:00:18 +03:00
parent a286813d4d
commit 8e6ca39d65

View File

@ -22,99 +22,58 @@ export function createValidationReaction<T extends ZodTypeAny>(
const helper = new ValidationHelper();
if (shapeValues.includes('insurance')) {
debouncedReaction(
() => ({
...$calculation.$values.getValues(shapeValues as Values[]),
insurance: {
values: {
fingap: toJS($tables.insurance.row('fingap').getValues()),
kasko: toJS($tables.insurance.row('kasko').getValues()),
osago: toJS($tables.insurance.row('osago').getValues()),
debouncedReaction(
() => {
const values = $calculation.$values.getValues(shapeValues as Values[]);
if (shapeValues.includes('insurance'))
return {
...values,
insurance: {
values: {
fingap: toJS($tables.insurance.row('fingap').getValues()),
kasko: toJS($tables.insurance.row('kasko').getValues()),
osago: toJS($tables.insurance.row('osago').getValues()),
},
},
},
}),
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
};
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Array<Elements & 'insurance'>).forEach((elementName) => {
if (elementName === 'insurance') {
const removeError = $tables.insurance.setError({ key, message });
if (removeError) helper.add(removeError);
} else {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
}
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
equals: comparer.structural,
wait: 100,
if (shapeValues.includes('payments')) {
return {
...values,
payments: { values: toJS($tables.payments.values) },
};
}
);
} else if (shapeValues.includes('payments')) {
debouncedReaction(
() => ({
...$calculation.$values.getValues(shapeValues as Values[]),
payments: { values: toJS($tables.payments.values) },
}),
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Array<Elements & 'payments'>).forEach((elementName) => {
if (elementName === 'payments') {
const removeError = $tables.payments.setError({ key, message });
if (removeError) helper.add(removeError);
} else {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
}
});
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
equals: comparer.structural,
wait: 100,
}
);
} else {
debouncedReaction(
() => $calculation.$values.getValues(shapeValues as Values[]),
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
return values;
},
async (values) => {
helper.removeErrors();
const validationResult = await validationSchema.safeParseAsync(values);
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Elements[]).forEach((elementName) => {
if (validationResult.success === false) {
validationResult.error.errors.forEach(({ path, message }) => {
(path as Array<Elements & ('insurance' | 'insurance')>).forEach((elementName) => {
if (elementName === 'insurance') {
const removeError = $tables.insurance.setError({ key, message });
if (removeError) helper.add(removeError);
} else if (elementName === 'payments') {
const removeError = $tables.payments.setError({ key, message });
if (removeError) helper.add(removeError);
} else {
const removeError = $calculation.element(elementName).setError({ key, message });
if (removeError) helper.add(removeError);
});
}
});
} else {
helper.removeErrors();
}
},
{
delay: 1,
wait: 100,
});
} else {
helper.removeErrors();
}
);
}
},
{
delay: 1,
equals: comparer.structural,
wait: 100,
}
);
};
}