From 021363baa7d96c9b93a64c6832695fdafd4e27dc Mon Sep 17 00:00:00 2001 From: Chika Date: Wed, 26 Oct 2022 20:08:44 +0300 Subject: [PATCH] stores/tables: add validate method --- process/fingap/reactions/validation.ts | 11 ++++------- stores/tables/fingap/index.ts | 11 ++++++++++- stores/tables/insurance/index.ts | 12 +++++++++++- stores/tables/payments/index.ts | 13 ++++++++++++- stores/validation/index.ts | 14 +++++--------- stores/validation/types.ts | 5 +++++ 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/process/fingap/reactions/validation.ts b/process/fingap/reactions/validation.ts index b7dfc6d..6060397 100644 --- a/process/fingap/reactions/validation.ts +++ b/process/fingap/reactions/validation.ts @@ -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, diff --git a/stores/tables/fingap/index.ts b/stores/tables/fingap/index.ts index 0c01586..bd10568 100644 --- a/stores/tables/fingap/index.ts +++ b/stores/tables/fingap/index.ts @@ -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([]); - 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(); diff --git a/stores/tables/insurance/index.ts b/stores/tables/insurance/index.ts index 89192dd..3a9c173 100644 --- a/stores/tables/insurance/index.ts +++ b/stores/tables/insurance/index.ts @@ -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 = 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; diff --git a/stores/tables/payments/index.ts b/stores/tables/payments/index.ts index 5ac08d0..81b1abd 100644 --- a/stores/tables/payments/index.ts +++ b/stores/tables/payments/index.ts @@ -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; constructor(rootStore: RootStore) { - this.root = rootStore; this.validation = new Validation({ err_key: 'ERR_PAYMENTS_TABLE', err_title: 'Таблица платежей', }); + this.values = observable([]); this.statuses = observable([]); + 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(); diff --git a/stores/validation/index.ts b/stores/validation/index.ts index 235f17d..69f6a56 100644 --- a/stores/validation/index.ts +++ b/stores/validation/index.ts @@ -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; - 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); }; } diff --git a/stores/validation/types.ts b/stores/validation/types.ts index f19de2a..21e8bc4 100644 --- a/stores/validation/types.ts +++ b/stores/validation/types.ts @@ -1,3 +1,8 @@ +export type ValidationConfig = { + err_key: string; + err_title: string; +}; + export type ValidationParams = { invalid: boolean; message: string;