From 8e6ca39d65fe021a17b497a1703df74e59de9035 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 13 Apr 2023 14:00:18 +0300 Subject: [PATCH] process/tools: refactor createValidationReaction --- apps/web/process/tools.ts | 129 +++++++++++++------------------------- 1 file changed, 44 insertions(+), 85 deletions(-) diff --git a/apps/web/process/tools.ts b/apps/web/process/tools.ts index 5acd553..208efb7 100644 --- a/apps/web/process/tools.ts +++ b/apps/web/process/tools.ts @@ -22,99 +22,58 @@ export function createValidationReaction( 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).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).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).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, + } + ); }; }