From eb7dc997e45b9c27af1c4ba074b9fe44105022de Mon Sep 17 00:00:00 2001 From: Chika Date: Mon, 3 Oct 2022 14:03:19 +0300 Subject: [PATCH] =?UTF-8?q?tables/payments:=20=D0=B0=D0=BD=D0=BD=D1=83?= =?UTF-8?q?=D0=B8=D1=82=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- process/payments/reactions.ts | 85 +++++++++++++++++++++------------ stores/tables/payments/index.ts | 6 +-- stores/tables/payments/types.ts | 6 +++ 3 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 stores/tables/payments/types.ts diff --git a/process/payments/reactions.ts b/process/payments/reactions.ts index c950d1f..f1ee5e7 100644 --- a/process/payments/reactions.ts +++ b/process/payments/reactions.ts @@ -1,45 +1,70 @@ import type { ApolloClient } from '@apollo/client'; -import { reaction, toJS } from 'mobx'; +import type { QueryClient } from '@tanstack/react-query'; +import { reaction } from 'mobx'; import type RootStore from 'stores/root'; +import type { Row } from 'stores/tables/payments/types'; -export default function paymentsReactions(store: RootStore, apolloClient: ApolloClient) { +export default function paymentsReactions( + store: RootStore, + apolloClient: ApolloClient, + queryClient: QueryClient +) { const { $calculation, $tables } = store; - /** - * При изменении срока лизинга регулируем длину таблицы платежей - */ reaction( - () => $calculation.$values.getValue('leasingPeriod'), - (leasingPeriod) => { - if (leasingPeriod) { - $tables.payments.setValues( - Array.from( - { - length: leasingPeriod, - }, - () => 0 - ) - ); - } - }, - { - fireImmediately: true, + () => $calculation.getElementValue('tbxFirstPaymentPerc'), + (firstPaymentPerc) => { + $tables.payments.setValue(0, firstPaymentPerc); + } + ); + + reaction( + () => $calculation.getElementValue('tbxLastPaymentPerc'), + (lastPaymentPerc) => { + const paymentsLength = $tables.payments.values.length; + $tables.payments.setValue(paymentsLength - 1, lastPaymentPerc); } ); /** - * Проверяем платежи на 0 + * Аннуитет */ - const errorText = 'Значения должны быть больше 0'; - let removeError: () => void; - reaction( - () => toJS($tables.payments.values), - (values) => { - if (values.includes(0)) { - removeError = $tables.payments.validation.addError(errorText); - } else { - removeError(); + () => { + const graphType = $calculation.getElementValue('radioGraphType'); + const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + + return { + graphType, + leasingPeriod, + }; + }, + ({ graphType, leasingPeriod }) => { + if (graphType === 100_000_000) { + const middleValues: Row[] = Array.from( + { + length: leasingPeriod - 2, + }, + () => ({ + value: 100, + status: 'Disabled', + }) + ); + + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + + $tables.payments.setRows([ + { + value: firstPaymentPerc, + status: 'Disabled', + }, + ...middleValues, + { + value: lastPaymentPerc, + status: 'Disabled', + }, + ]); } }, { diff --git a/stores/tables/payments/index.ts b/stores/tables/payments/index.ts index f6b8898..d3c0eed 100644 --- a/stores/tables/payments/index.ts +++ b/stores/tables/payments/index.ts @@ -4,11 +4,7 @@ import { makeAutoObservable, observable, reaction } from 'mobx'; import type RootStore from 'stores/root'; import Validation from '../validation'; - -type Row = { - value: number; - status: Status; -}; +import type { Row } from './types'; export default class PaymentsTable { root: RootStore; diff --git a/stores/tables/payments/types.ts b/stores/tables/payments/types.ts new file mode 100644 index 0000000..7e086e1 --- /dev/null +++ b/stores/tables/payments/types.ts @@ -0,0 +1,6 @@ +import type { Status } from 'Elements/types'; + +export type Row = { + value: number; + status: Status; +};