2023-04-13 13:39:30 +03:00

121 lines
4.2 KiB
TypeScript

/* eslint-disable sonarjs/cognitive-complexity */
import { debouncedReaction } from '../utils/mobx';
import type { ProcessContext } from './types';
import type { Elements } from '@/Components/Calculation/config/map/values';
import type { Values } from '@/stores/calculation/values/types';
import ValidationHelper from '@/stores/validation/helper';
import { comparer, toJS } from 'mobx';
import { uid } from 'radash';
import type { ZodTypeAny } from 'zod';
export function createValidationReaction<T extends ZodTypeAny>(
createValidationSchema: (context: ProcessContext) => T
) {
const key = uid(7);
return (context: ProcessContext) => {
const validationSchema = createValidationSchema(context);
const shapeValues = Object.keys(validationSchema._def.schema.shape) as string[];
const { store } = context;
const { $calculation, $tables } = store;
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()),
},
},
}),
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,
}
);
} 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);
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,
}
);
}
};
}