tables/payments: Легкий старт

This commit is contained in:
Chika 2022-10-03 19:51:23 +03:00
parent 7c40e2cf4f
commit 9bedfe8fab
2 changed files with 97 additions and 5 deletions

View File

@ -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();
}
);
}

4
tools/array.ts Normal file
View File

@ -0,0 +1,4 @@
/* eslint-disable import/prefer-default-export */
export function areEqual(arr1: Array<any>, arr2: Array<any>) {
return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]);
}