From 9bedfe8fab8e175681ac2e0b491bf276c5d766de Mon Sep 17 00:00:00 2001 From: Chika Date: Mon, 3 Oct 2022 19:51:23 +0300 Subject: [PATCH] =?UTF-8?q?tables/payments:=20=D0=9B=D0=B5=D0=B3=D0=BA?= =?UTF-8?q?=D0=B8=D0=B9=20=D1=81=D1=82=D0=B0=D1=80=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- process/payments/reactions.ts | 98 +++++++++++++++++++++++++++++++++-- tools/array.ts | 4 ++ 2 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 tools/array.ts diff --git a/process/payments/reactions.ts b/process/payments/reactions.ts index 42c4e26..5febcf2 100644 --- a/process/payments/reactions.ts +++ b/process/payments/reactions.ts @@ -1,8 +1,10 @@ import type { ApolloClient } from '@apollo/client'; import type { QueryClient } from '@tanstack/react-query'; -import { reaction } from 'mobx'; +import { reaction, toJS } from 'mobx'; +import { sort } from 'radash/dist/array'; import type RootStore from 'stores/root'; import type { Row } from 'stores/tables/payments/types'; +import { areEqual } from 'tools/array'; export default function paymentsReactions( store: RootStore, @@ -41,7 +43,7 @@ export default function paymentsReactions( }, ({ graphType, leasingPeriod }) => { if (graphType === 100_000_000) { - const middleValues: Row[] = Array.from( + const middlePayments: Row[] = Array.from( { length: leasingPeriod - 2, }, @@ -59,7 +61,7 @@ export default function paymentsReactions( value: firstPaymentPerc, status: 'Disabled', }, - ...middleValues, + ...middlePayments, { value: lastPaymentPerc, status: 'Disabled', @@ -89,7 +91,7 @@ export default function paymentsReactions( }, ({ graphType, leasingPeriod, parmentsDecreasePercent }) => { if (graphType === 100_000_002) { - const middleValues: Row[] = Array.from( + const middlePayments: Row[] = Array.from( { length: leasingPeriod - 2, }, @@ -111,7 +113,7 @@ export default function paymentsReactions( value: firstPaymentPerc, status: 'Disabled', }, - ...middleValues, + ...middlePayments, { value: lastPaymentPerc, status: 'Disabled', @@ -120,4 +122,90 @@ export default function paymentsReactions( } } ); + + /** + * Легкий старт + */ + reaction( + () => { + const graphType = $calculation.getElementValue('radioGraphType'); + const leasingPeriod = $calculation.getElementValue('tbxLeasingPeriod'); + + return { + graphType, + leasingPeriod, + }; + }, + ({ graphType, leasingPeriod }) => { + if (graphType === 100_000_004) { + const editablePayments: Row[] = [ + { + value: 25, + status: 'Default', + }, + { + value: 50, + status: 'Default', + }, + { + value: 75, + status: 'Default', + }, + ]; + + const payments: Row[] = Array.from( + { + length: leasingPeriod - 5, + }, + () => ({ + value: 100, + status: 'Disabled', + }) + ); + + const firstPaymentPerc = $calculation.getElementValue('tbxFirstPaymentPerc'); + const lastPaymentPerc = $calculation.getElementValue('tbxLastPaymentPerc'); + + $tables.payments.setRows([ + { + value: firstPaymentPerc, + status: 'Disabled', + }, + ...editablePayments, + ...payments, + { + value: lastPaymentPerc, + status: 'Disabled', + }, + ]); + } + } + ); + + let removeError: () => void; + + reaction( + () => { + const graphType = $calculation.getElementValue('radioGraphType'); + const payments = $tables.payments.values; + + return { + payments: toJS(payments), + graphType, + }; + }, + ({ payments, graphType }) => { + if (graphType === 100_000_004) { + const targetPayments = payments.slice(1, 4); + const sortedPayments = sort(targetPayments, (x) => x); + const areEqualPayments = new Set(targetPayments).size === 1; + + if (!areEqual(targetPayments, sortedPayments) || areEqualPayments) { + removeError = $tables.payments.validation.addError( + "При виде графика 'Легкий старт', 2, 3, 4 платежи должны возрастать" + ); + } else if (removeError) removeError(); + } else if (removeError) removeError(); + } + ); } diff --git a/tools/array.ts b/tools/array.ts new file mode 100644 index 0000000..10ea29b --- /dev/null +++ b/tools/array.ts @@ -0,0 +1,4 @@ +/* eslint-disable import/prefer-default-export */ +export function areEqual(arr1: Array, arr2: Array) { + return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]); +}