diff --git a/src/client/Containers/Calculation/Sections/sectionsList.ts b/src/client/Containers/Calculation/Sections/sectionsList.ts index 77d2b71..e35841d 100644 --- a/src/client/Containers/Calculation/Sections/sectionsList.ts +++ b/src/client/Containers/Calculation/Sections/sectionsList.ts @@ -599,33 +599,40 @@ const sections: ISections[] = [ }, ], }, - // { - // elements: [ - // { - // type: ElementType.Table, - // Component: Table, - // props: { - // name: 'tablePayments', - // features: { - // canDeleteRow: false, - // numerize: { - // columnTitle: 'Номер платежа', - // }, - // }, - // columns: [ - // { - // name: 'paymentRelation', - // title: 'Соотношение платежа', - // Component: Label, - // }, - // ], - // }, - // }, - // ], - // layout: { - // newLine: true, - // }, - // }, + { + elements: [ + { + type: ElementType.Table, + Component: Table, + props: { + name: 'tablePayments', + features: { + canDeleteRow: false, + numerize: { + columnTitle: 'Номер платежа', + }, + }, + columns: [ + { + name: 'paymentRelation', + title: 'Соотношение платежа', + Component: InputNumber, + props: { + min: '0.01', + max: '100.00', + step: '1.00', + // TODO: toFixed + //numeral.js + }, + }, + ], + }, + }, + ], + layout: { + newLine: true, + }, + }, ], }, { diff --git a/src/client/hooks/Calculation/useValue.js b/src/client/hooks/Calculation/useValue.js index a15d414..dc3e869 100644 --- a/src/client/hooks/Calculation/useValue.js +++ b/src/client/hooks/Calculation/useValue.js @@ -67,7 +67,7 @@ export const useTableValue = ({ tableName, rowIndex, propName }) => { useEffect(() => { if (cellCallBack) { - cellCallBack(calculationStore, tableName, rowIndex, propName); + cellCallBack(calculationStore, tableName, rowIndex, debouncedValue); } }, [debouncedValue, cellCallBack]); diff --git a/src/client/stores/CalculationStore/Effects/reaction.ts b/src/client/stores/CalculationStore/Effects/reaction.ts index 70b7dcb..791307d 100644 --- a/src/client/stores/CalculationStore/Effects/reaction.ts +++ b/src/client/stores/CalculationStore/Effects/reaction.ts @@ -1006,6 +1006,9 @@ const reactionEffects: IReactionEffect[] = [ } } }, + options: { + fireImmediately: true, + }, }), calculationStore => ({ @@ -2198,6 +2201,145 @@ const reactionEffects: IReactionEffect[] = [ fireImmediately: true, }, }), + + calculationStore => ({ + expression: () => { + const { + leasingPeriod, + graphType, + parmentsDecreasePercent, + seasonType, + highSeasonStart, + firstPaymentPerc, + lastPaymentPerc, + } = calculationStore.values; + return { + leasingPeriod, + graphType, + parmentsDecreasePercent, + seasonType, + highSeasonStart, + firstPaymentPerc, + lastPaymentPerc, + }; + }, + effect: ({ + leasingPeriod, + graphType, + parmentsDecreasePercent, + seasonType, + highSeasonStart, + firstPaymentPerc, + lastPaymentPerc, + }) => { + calculationStore.cleanTable('tablePayments'); + calculationStore.setTableRow( + 'tablePayments', + 0, + )({ + paymentRelation: { + value: firstPaymentPerc, + status: Status.Disabled, + }, + }); + + switch (graphType) { + case 100000000: { + calculationStore.setTableRows( + 'tablePayments', + 1, + )( + Array.from({ length: leasingPeriod - 2 }, () => ({ + paymentRelation: { + value: 100, + status: Status.Disabled, + }, + })), + ); + break; + } + + case 100000001: { + calculationStore.setTableRow( + 'tablePayments', + 1, + )({ + paymentRelation: { + value: 100, + status: Status.Disabled, + }, + }); + + calculationStore.setTableRows( + 'tablePayments', + 2, + )( + Array.from({ length: leasingPeriod - 3 }, () => ({ + paymentRelation: { + value: 100, + status: Status.Default, + }, + })), + ); + + break; + } + + case 100000002: { + calculationStore.setTableRow( + 'tablePayments', + 1, + )({ + paymentRelation: { + value: 100, + status: Status.Disabled, + }, + }); + + const rows = Array.from({ length: leasingPeriod - 3 }, (v, i) => ({ + paymentRelation: { + value: 100, + status: Status.Disabled, + }, + })); + + for (let i in rows) { + const currRow = rows[parseInt(i)]; + const prevRow = rows[parseInt(i) - 1]; + currRow.paymentRelation.value = parseFloat( + ( + ((prevRow ? prevRow.paymentRelation.value : 100) * + parmentsDecreasePercent) / + 100 + ).toFixed(2), + ); + } + + calculationStore.setTableRows('tablePayments', 2)(rows); + break; + } + + default: { + break; + } + } + + calculationStore.setTableRow( + 'tablePayments', + calculationStore.tables.tablePayments.rows.length, + )({ + paymentRelation: { + value: lastPaymentPerc, + status: Status.Disabled, + }, + }); + + console.log(calculationStore.tables.tablePayments); + }, + options: { + fireImmediately: true, + }, + }), ]; export default reactionEffects; diff --git a/src/core/config/initialTables/tablePayments.ts b/src/core/config/initialTables/tablePayments.ts index 8544f1c..02511b7 100644 --- a/src/core/config/initialTables/tablePayments.ts +++ b/src/core/config/initialTables/tablePayments.ts @@ -1,7 +1,93 @@ import { ITable } from 'core/types/tables'; +import { toJS } from 'mobx'; const tablePayments: ITable = { - rows: [], + rows: [ + // { + // paymentRelation: { value: 5 }, + // }, + ], + callbacks: { + paymentRelation: (calculationStore, tableName, rowIndex, value) => { + const { graphType } = calculationStore.values; + if (graphType === 100000001) { + if ( + rowIndex > 1 && + rowIndex < calculationStore.tables[tableName].rows.length - 1 + ) { + calculationStore.setTableRows( + tableName, + rowIndex, + )( + Array.from( + { + length: + calculationStore.tables[tableName].rows.length - rowIndex - 1, + }, + () => ({ + paymentRelation: { + value, + }, + }), + ), + ); + for (let i in calculationStore.tables[tableName].rows) { + const currRow = + calculationStore.tables[tableName].rows[parseInt(i)]; + const prevRow = + calculationStore.tables[tableName].rows[parseInt(i) - 1]; + if ( + parseInt(i) > 1 && + currRow && + prevRow && + currRow.paymentRelation?.value > prevRow.paymentRelation?.value + ) { + calculationStore.setTableRow( + tableName, + parseInt(i), + )({ + paymentRelation: { + validation: false, + }, + }); + } else { + calculationStore.setTableRow( + tableName, + parseInt(i), + )({ + paymentRelation: { + validation: true, + }, + }); + } + } + const middleRows = toJS(calculationStore.tables[tableName].rows); + console.log('middleRows', middleRows); + middleRows.splice(0, 1); + middleRows.splice(-1, 1); + + if ( + middleRows.every( + x => + x.paymentRelation?.value === + middleRows[0].paymentRelation?.value, + ) + ) { + calculationStore.setTableRows( + tableName, + 2, + )( + Array.from({ length: middleRows.length - 1 }, (v, i) => ({ + paymentRelation: { + validation: false, + }, + })), + ); + } + } + } + }, + }, }; export default tablePayments; diff --git a/src/core/types/tables.ts b/src/core/types/tables.ts index 51a6cb6..526fb0c 100644 --- a/src/core/types/tables.ts +++ b/src/core/types/tables.ts @@ -17,7 +17,7 @@ export type TCellCallback = ( calculationStore: ICalculationStore, tableName: TableNames, rowIndex: number, - propName: TableValuesNames, + value: any, ) => void; export interface ITableCell {