From 2884ccf3e06928bd4c5d2503a29affc5c6bb4274 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 2 Feb 2023 20:38:58 +0300 Subject: [PATCH] add options/value validation for insurance table --- .../Form/Insurance/InsuranceTable/types.ts | 2 +- apps/web/Components/Output/Validation.jsx | 5 +- apps/web/config/tables/insurance-table.ts | 9 +++ .../process/calculate/reactions/validation.ts | 61 +++++++++++++++++-- apps/web/stores/tables/insurance/index.ts | 2 +- 5 files changed, 71 insertions(+), 8 deletions(-) diff --git a/apps/web/Components/Calculation/Form/Insurance/InsuranceTable/types.ts b/apps/web/Components/Calculation/Form/Insurance/InsuranceTable/types.ts index 1d84a36..744cf3c 100644 --- a/apps/web/Components/Calculation/Form/Insurance/InsuranceTable/types.ts +++ b/apps/web/Components/Calculation/Form/Insurance/InsuranceTable/types.ts @@ -9,7 +9,7 @@ export type RowValues = z.infer; export type Values = Exclude; export type RowOptions = { - [ValueName in Values]?: BaseOption[]; + [ValueName in Values]: BaseOption[]; }; export type RowStatuses = Record; diff --git a/apps/web/Components/Output/Validation.jsx b/apps/web/Components/Output/Validation.jsx index 2f6ea68..1746d51 100644 --- a/apps/web/Components/Output/Validation.jsx +++ b/apps/web/Components/Output/Validation.jsx @@ -48,8 +48,11 @@ function getPaymentsTableErrors($tables) { function getInsuranceTableErrors($tables) { const { insurance } = $tables; const messages = insurance.validation.getMessages(); + const title = insurance.validation.params.err_title; - return messages.map((message) => ); + return messages.map((message) => ( + + )); } const Errors = observer(() => { diff --git a/apps/web/config/tables/insurance-table.ts b/apps/web/config/tables/insurance-table.ts index 3c8efd8..11bdbbe 100644 --- a/apps/web/config/tables/insurance-table.ts +++ b/apps/web/config/tables/insurance-table.ts @@ -23,6 +23,9 @@ export const defaultOptions: Record = { value: 100_000_001, }, ], + insCost: [], + insuranceCompany: [], + policyType: [], }, kasko: { insured: [ @@ -45,6 +48,9 @@ export const defaultOptions: Record = { value: 100_000_001, }, ], + insCost: [], + insuranceCompany: [], + policyType: [], }, fingap: { insured: [ @@ -67,6 +73,9 @@ export const defaultOptions: Record = { value: 100_000_001, }, ], + insCost: [], + insuranceCompany: [], + policyType: [], }, }; diff --git a/apps/web/process/calculate/reactions/validation.ts b/apps/web/process/calculate/reactions/validation.ts index d58796d..0c83488 100644 --- a/apps/web/process/calculate/reactions/validation.ts +++ b/apps/web/process/calculate/reactions/validation.ts @@ -1,10 +1,11 @@ import types from 'Components/Calculation/config/elements-types'; import type * as Values from 'Components/Calculation/config/map/values'; +import type * as Insurance from 'Components/Calculation/Form/Insurance/InsuranceTable/types'; import { reaction } from 'mobx'; import type { ReactionsContext } from 'process/types'; import type { BaseOption } from 'ui/elements/types'; -function hasInvalidValue(value: unknown, options: BaseOption[]) { +function hasInvalidValueOrOptions(value: unknown, options: BaseOption[]) { if (value === null) { return false; } @@ -43,7 +44,7 @@ export default function validationReactions({ store }: ReactionsContext) { /** * Проверяем, что выбранное значение элемента есть в списке */ - (Object.keys(types) as Values.Elements[]).forEach((elementName) => { + function validateOptionsElement(elementName: Values.Elements) { const type = types[elementName]; if (type().typeName !== 'Options') { return; @@ -63,7 +64,7 @@ export default function validationReactions({ store }: ReactionsContext) { }, ({ value, options }) => { element.validate({ - invalid: hasInvalidValue(value, options), + invalid: hasInvalidValueOrOptions(value, options), message: 'Выбранное значение отсутствует в списке', silent: true, }); @@ -77,10 +78,60 @@ export default function validationReactions({ store }: ReactionsContext) { () => element.getOptions(), (options) => { const value = element.getValue(); - if (hasInvalidValue(value, options)) { + if (hasInvalidValueOrOptions(value, options)) { element.resetValue(); } + }, + { + delay: 100, } ); - }); + } + + (Object.keys(types) as Values.Elements[]).forEach((elementName) => + validateOptionsElement(elementName) + ); + + function validateInsuranceCompany(rowKey: Insurance.Keys) { + const row = $tables.insurance.row(rowKey); + + reaction( + () => { + const options = row.getOptions('insuranceCompany'); + const value = row.getValue('insuranceCompany'); + + return { + value, + options, + }; + }, + ({ value, options }) => { + $tables.insurance.validate({ + invalid: hasInvalidValueOrOptions(value, options), + message: 'Выбранное значение отсутствует в списке', + silent: true, + }); + }, + { + delay: 100, + } + ); + + reaction( + () => row.getOptions('insuranceCompany'), + (options) => { + const value = row.getValue('insuranceCompany'); + if (hasInvalidValueOrOptions(value, options)) { + row.resetValue('insuranceCompany'); + } + }, + { + delay: 100, + } + ); + } + + validateInsuranceCompany('kasko'); + validateInsuranceCompany('osago'); + validateInsuranceCompany('fingap'); } diff --git a/apps/web/stores/tables/insurance/index.ts b/apps/web/stores/tables/insurance/index.ts index bdd8d16..683ce6c 100644 --- a/apps/web/stores/tables/insurance/index.ts +++ b/apps/web/stores/tables/insurance/index.ts @@ -63,7 +63,7 @@ export default class InsuranceTable { getStatus: (valueName: Insurance.Values) => this.statuses[key][valueName], - getOptions: (valueName: Insurance.Values) => this.options[key][valueName], + getOptions: (valueName: V) => this.options[key][valueName], setValue: (valueName: V, value: Insurance.RowValues[V]) => { const rowIndex = this.values.findIndex((x) => x.key === key);