add options/value validation for insurance table
This commit is contained in:
parent
2a4203914b
commit
2884ccf3e0
@ -9,7 +9,7 @@ export type RowValues = z.infer<typeof RowSchema>;
|
||||
export type Values = Exclude<keyof RowValues, 'key'>;
|
||||
|
||||
export type RowOptions = {
|
||||
[ValueName in Values]?: BaseOption<RowValues[ValueName]>[];
|
||||
[ValueName in Values]: BaseOption<RowValues[ValueName]>[];
|
||||
};
|
||||
|
||||
export type RowStatuses = Record<Values, Status>;
|
||||
|
||||
@ -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) => <Alert type="error" showIcon message={message} />);
|
||||
return messages.map((message) => (
|
||||
<Alert type="error" showIcon message={Message(title, message)} />
|
||||
));
|
||||
}
|
||||
|
||||
const Errors = observer(() => {
|
||||
|
||||
@ -23,6 +23,9 @@ export const defaultOptions: Record<Insurance.Keys, Insurance.RowOptions> = {
|
||||
value: 100_000_001,
|
||||
},
|
||||
],
|
||||
insCost: [],
|
||||
insuranceCompany: [],
|
||||
policyType: [],
|
||||
},
|
||||
kasko: {
|
||||
insured: [
|
||||
@ -45,6 +48,9 @@ export const defaultOptions: Record<Insurance.Keys, Insurance.RowOptions> = {
|
||||
value: 100_000_001,
|
||||
},
|
||||
],
|
||||
insCost: [],
|
||||
insuranceCompany: [],
|
||||
policyType: [],
|
||||
},
|
||||
fingap: {
|
||||
insured: [
|
||||
@ -67,6 +73,9 @@ export const defaultOptions: Record<Insurance.Keys, Insurance.RowOptions> = {
|
||||
value: 100_000_001,
|
||||
},
|
||||
],
|
||||
insCost: [],
|
||||
insuranceCompany: [],
|
||||
policyType: [],
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@ -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<unknown>[]) {
|
||||
function hasInvalidValueOrOptions(value: unknown, options: BaseOption<unknown>[]) {
|
||||
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');
|
||||
}
|
||||
|
||||
@ -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: <V extends Insurance.Values>(valueName: V) => this.options[key][valueName],
|
||||
|
||||
setValue: <V extends Insurance.Values>(valueName: V, value: Insurance.RowValues[V]) => {
|
||||
const rowIndex = this.values.findIndex((x) => x.key === key);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user