stores: refactor validationHelper usage

This commit is contained in:
vchikalkin 2023-02-13 18:20:06 +03:00
parent a4d3e1c34a
commit 0cd80ef783
10 changed files with 77 additions and 77 deletions

View File

@ -19,6 +19,10 @@ export default function validationReactions({ store }: ReactionsContext) {
invalid: finGAPInsuranceCompany !== null && hasPaymentsErrors,
message: 'Неверно заполнены платежи',
});
if (hasPaymentsErrors) {
$tables.fingap.clear();
}
},
{
fireImmediately: true,

View File

@ -103,37 +103,25 @@ export default function validationReactions({ store, apolloClient }: ReactionsCo
},
});
$calculation
.element('tbxCountSeats')
.validate({
invalid: evo_leasingobject_type?.evo_id === '1' && countSeats >= 9,
message: 'Количество мест должно быть меньше 9',
})
.err((removeError) => {
validationHelper.add(removeError);
});
$calculation.element('tbxCountSeats').validate({
helper: validationHelper,
invalid: evo_leasingobject_type?.evo_id === '1' && countSeats >= 9,
message: 'Количество мест должно быть меньше 9',
});
$calculation
.element('tbxCountSeats')
.validate({
invalid:
(evo_leasingobject_type?.evo_id === '4' || evo_leasingobject_type?.evo_id === '5') &&
countSeats <= 8,
message: 'Количество мест должно быть больше 8',
})
.err((removeError) => {
validationHelper.add(removeError);
});
$calculation.element('tbxCountSeats').validate({
helper: validationHelper,
invalid:
(evo_leasingobject_type?.evo_id === '4' || evo_leasingobject_type?.evo_id === '5') &&
countSeats <= 8,
message: 'Количество мест должно быть больше 8',
});
$calculation
.element('tbxMaxMass')
.validate({
invalid: evo_leasingobject_type?.evo_id === '2' && maxMass <= 0,
message: 'Не заполнено поле',
})
.err((removeError) => {
validationHelper.add(removeError);
});
$calculation.element('tbxMaxMass').validate({
helper: validationHelper,
invalid: evo_leasingobject_type?.evo_id === '2' && maxMass <= 0,
message: 'Не заполнено поле',
});
}
);
}

View File

@ -566,11 +566,15 @@ export function validationReactions({ store }: ReactionsContext) {
},
() => {
validationHelper.removeErrors();
const errorText = validatePaymentsTable(store);
if (errorText) {
const removeError = $tables.payments.validation.addError(errorText);
validationHelper.add(removeError);
$tables.payments.validate({
helper: validationHelper,
invalid: errorText !== null,
message: errorText,
});
}
},
{

View File

@ -147,44 +147,32 @@ export function validateAgentRewardSumm(
}
if (evo_reward_condition.evo_reward_summ) {
$calculation
.element(rewardSummField)
.validate({
invalid: rewardSumm > evo_reward_condition?.evo_reward_summ,
message: 'Вознаграждение указано больше условия по агентскому договору!',
})
.err((removeError) => {
validationHelper.add(removeError);
});
$calculation.element(rewardSummField).validate({
helper: validationHelper,
invalid: rewardSumm > evo_reward_condition?.evo_reward_summ,
message: 'Вознаграждение указано больше условия по агентскому договору!',
});
}
if (
evo_reward_condition?.evo_reduce_reward !== null &&
evo_reward_condition.evo_reward_summ
) {
$calculation
.element(rewardSummField)
.validate({
invalid:
evo_reward_condition.evo_reduce_reward === false &&
rewardSumm < evo_reward_condition.evo_reward_summ,
message: 'Вознаграждение указано меньше условия по агентскому договору!',
})
.err((removeError) => {
validationHelper.add(removeError);
});
$calculation.element(rewardSummField).validate({
helper: validationHelper,
invalid:
evo_reward_condition.evo_reduce_reward === false &&
rewardSumm < evo_reward_condition.evo_reward_summ,
message: 'Вознаграждение указано меньше условия по агентскому договору!',
});
}
if (evo_reward_condition?.evo_min_reward_summ !== null) {
$calculation
.element(rewardSummField)
.validate({
invalid: rewardSumm < evo_reward_condition?.evo_min_reward_summ,
message: 'Вознаграждение указано меньше условия по агентскому договору!',
})
.err((removeError) => {
validationHelper.add(removeError);
});
$calculation.element(rewardSummField).validate({
helper: validationHelper,
invalid: rewardSumm < evo_reward_condition?.evo_min_reward_summ,
message: 'Вознаграждение указано меньше условия по агентскому договору!',
});
}
}
);

View File

@ -99,21 +99,19 @@ export default class CalculationStore {
return this.element(elementName);
},
validate: ({ invalid, message, silent }: ValidationParams) => {
validate: ({ invalid, message, silent, helper }: ValidationParams) => {
if (!this.$validation[elementName]) this.createElementValidation(elementName);
let removeError: RemoveError | undefined;
if (invalid) {
removeError = this.$validation[elementName]?.addError(message, silent);
if (helper && removeError) helper.add(removeError);
} else {
this.$validation[elementName]?.removeError(message);
}
return {
err(callback: (removeError: RemoveError) => void) {
if (removeError) callback(removeError);
},
};
return removeError;
},
});
}

View File

@ -1,5 +1,5 @@
import Validation from '../../validation';
import type { ValidationParams } from '../../validation/types';
import type { RemoveError, ValidationParams } from '../../validation/types';
import type * as FinGAP from '@/Components/Calculation/Form/Insurance/FinGAPTable/types';
import type RootStore from '@/stores/root';
import type { IObservableArray } from 'mobx';
@ -38,12 +38,17 @@ export default class FinGAPTable {
.reduce((sum, risk) => sum + risk.premium, 0);
}
public validate = ({ invalid, message }: ValidationParams) => {
public validate = ({ invalid, message, helper }: ValidationParams) => {
let removeError: RemoveError | undefined;
if (invalid) {
this.validation?.addError(message);
removeError = this.validation?.addError(message);
if (helper && removeError) helper.add(removeError);
} else {
this.validation?.removeError(message);
}
return removeError;
};
public clear = () => {

View File

@ -1,5 +1,5 @@
import Validation from '../../validation';
import type { ValidationParams } from '../../validation/types';
import type { RemoveError, ValidationParams } from '../../validation/types';
import type * as Insurance from '@/Components/Calculation/Form/Insurance/InsuranceTable/types';
import * as insuranceTableConfig from '@/config/tables/insurance-table';
import type RootStore from '@/stores/root';
@ -40,12 +40,17 @@ export default class InsuranceTable {
if (initialStatuses) this.statuses = initialStatuses;
};
public validate = ({ invalid, message }: ValidationParams) => {
public validate = ({ invalid, message, helper }: ValidationParams) => {
let removeError: RemoveError | undefined;
if (invalid) {
this.validation?.addError(message);
removeError = this.validation?.addError(message);
if (helper && removeError) helper.add(removeError);
} else {
this.validation?.removeError(message);
}
return removeError;
};
public reset = () => {

View File

@ -1,5 +1,5 @@
import Validation from '../../validation';
import type { ValidationParams } from '../../validation/types';
import type { RemoveError, ValidationParams } from '../../validation/types';
import type { Row } from './types';
import type RootStore from '@/stores/root';
import type { IObservableArray } from 'mobx';
@ -72,12 +72,17 @@ export default class PaymentsTable {
this.setStatuses(statuses);
};
public validate = ({ invalid, message }: ValidationParams) => {
public validate = ({ invalid, message, helper }: ValidationParams) => {
let removeError: RemoveError | undefined;
if (invalid) {
this.validation?.addError(message);
removeError = this.validation?.addError(message);
if (helper && removeError) helper.add(removeError);
} else {
this.validation?.removeError(message);
}
return removeError;
};
public reset = () => {

View File

@ -1,4 +1,4 @@
import type { ValidationConfig } from './types';
import type { RemoveError, ValidationConfig } from './types';
import { makeAutoObservable } from 'mobx';
import notification from 'ui/elements/notification';
@ -36,7 +36,7 @@ export default class Validation {
this.messages.add(message);
return () => this.removeError(message);
return (() => this.removeError(message)) as RemoveError;
};
public clearErrors = () => {

View File

@ -1,9 +1,12 @@
import type ValidationHelper from './helper';
export type ValidationConfig = {
err_key: string;
err_title: string;
};
export type ValidationParams = {
helper?: ValidationHelper;
invalid: boolean;
message: string;
silent?: boolean;