stores/tables: add validate method

This commit is contained in:
Chika 2022-10-26 20:08:44 +03:00
parent 4d5fff51b1
commit 021363baa7
6 changed files with 47 additions and 19 deletions

View File

@ -2,7 +2,6 @@ import type { ApolloClient } from '@apollo/client';
import type { QueryClient } from '@tanstack/react-query';
import { reaction } from 'mobx';
import type RootStore from 'stores/root';
import type { RemoveError } from 'stores/validation/types';
export default function validationReactions(
store: RootStore,
@ -11,9 +10,6 @@ export default function validationReactions(
) {
const { $tables } = store;
const errorText = 'Неверно заполнены платежи';
let removeError: RemoveError | undefined;
reaction(
() => {
const hasPaymentsErrors = $tables.payments.validation.hasErrors;
@ -25,9 +21,10 @@ export default function validationReactions(
};
},
({ hasPaymentsErrors, finGAPInsuranceCompany }) => {
if (finGAPInsuranceCompany && hasPaymentsErrors) {
removeError = $tables.fingap.validation.addError(errorText);
} else if (removeError) removeError();
$tables.fingap.validate({
invalid: finGAPInsuranceCompany !== null && hasPaymentsErrors,
message: 'Неверно заполнены платежи',
});
},
{
fireImmediately: true,

View File

@ -3,6 +3,7 @@ import type { IObservableArray } from 'mobx';
import { makeAutoObservable, observable } from 'mobx';
import type RootStore from 'stores/root';
import Validation from '../../validation';
import type { ValidationParams } from '../../validation/types';
export default class FinGAPTable {
root: RootStore;
@ -13,13 +14,13 @@ export default class FinGAPTable {
constructor(rootStore: RootStore) {
this.selectedKeys = new Set();
this.risks = observable<FinGAP.Risk>([]);
makeAutoObservable(this);
this.validation = new Validation({
err_key: 'ERR_FINGAP_TABLE',
err_title: 'Таблица рисков Safe Finance',
});
makeAutoObservable(this);
this.root = rootStore;
}
@ -37,6 +38,14 @@ export default class FinGAPTable {
.reduce((sum, risk) => sum + risk.premium, 0);
}
validate = ({ invalid, message }: ValidationParams) => {
if (invalid) {
this.validation?.addError(message);
} else {
this.validation?.removeError(message);
}
};
clear = () => {
this.risks.clear();
this.selectedKeys.clear();

View File

@ -4,6 +4,7 @@ import * as insuranceTableConfig from 'config/tables/insurance-table';
import { makeAutoObservable } from 'mobx';
import type RootStore from 'stores/root';
import Validation from '../../validation';
import type { ValidationParams } from '../../validation/types';
export interface InsuranceTableData {
values?: Insurance.RowValues[];
@ -19,12 +20,13 @@ export default class InsuranceTable {
statuses: Record<Insurance.Keys, Insurance.RowStatuses> = insuranceTableConfig.defaultStatuses;
constructor(rootStore: RootStore) {
this.root = rootStore;
this.validation = new Validation({
err_key: 'ERR_INSURANCE_TABLE',
err_title: 'Таблица страхования',
});
makeAutoObservable(this);
this.root = rootStore;
}
hydrate = ({
@ -67,6 +69,14 @@ export default class InsuranceTable {
this.statuses[key] = { ...this.statuses[key], ...rowStatuses };
};
validate = ({ invalid, message }: ValidationParams) => {
if (invalid) {
this.validation?.addError(message);
} else {
this.validation?.removeError(message);
}
};
reset = () => {
this.values = insuranceTableConfig.defaultValues;
this.options = insuranceTableConfig.defaultOptions;

View File

@ -4,6 +4,7 @@ import { makeAutoObservable, observable, reaction } from 'mobx';
import type RootStore from 'stores/root';
import Validation from '../../validation';
import type { ValidationParams } from '../../validation/types';
import type { Row } from './types';
export default class PaymentsTable {
@ -13,14 +14,16 @@ export default class PaymentsTable {
statuses: IObservableArray<Status>;
constructor(rootStore: RootStore) {
this.root = rootStore;
this.validation = new Validation({
err_key: 'ERR_PAYMENTS_TABLE',
err_title: 'Таблица платежей',
});
this.values = observable<number>([]);
this.statuses = observable<Status>([]);
makeAutoObservable(this);
this.root = rootStore;
/**
* Синхронизируем длину массива значений и статусов
@ -70,6 +73,14 @@ export default class PaymentsTable {
this.setStatuses(statuses);
};
validate = ({ invalid, message }: ValidationParams) => {
if (invalid) {
this.validation?.addError(message);
} else {
this.validation?.removeError(message);
}
};
reset = () => {
this.values.clear();
this.statuses.clear();

View File

@ -1,17 +1,13 @@
import notification from 'Elements/notification';
import { makeAutoObservable } from 'mobx';
type Params = {
err_key: string;
err_title: string;
};
import type { ValidationConfig } from './types';
export default class Validation {
params: Params;
params: ValidationConfig;
messages: Set<string>;
constructor(params: Params) {
this.params = params;
constructor(config: ValidationConfig) {
this.params = config;
this.messages = new Set();
makeAutoObservable(this);
}
@ -42,7 +38,7 @@ export default class Validation {
};
clearErrors = () => {
notification.close(this.params.err_key);
this.messages.clear();
notification.close(this.params.err_key);
};
}

View File

@ -1,3 +1,8 @@
export type ValidationConfig = {
err_key: string;
err_title: string;
};
export type ValidationParams = {
invalid: boolean;
message: string;