From f591f613103f2d3c6bd76edaa7100d9ff1195826 Mon Sep 17 00:00:00 2001 From: Chika Date: Mon, 26 Apr 2021 18:20:37 +0300 Subject: [PATCH] refactor tables --- .../Calculation/lib/buildElement.js | 5 +- .../Calculation/lib/elements/tables.ts | 119 ------ .../Calculation/lib/elements/tables/index.js | 9 + .../lib/elements/tables/insurance.ts | 364 ++++++++++++++++++ .../lib/elements/tables/payments.ts | 137 +++++++ .../lib/elements/tables/results.ts | 48 +++ src/client/Elements/Table.jsx | 31 +- src/client/hocs/Calculation/withTable.jsx | 8 +- src/client/hooks/Calculation/useValue.js | 18 +- .../stores/CalculationStore/Data/tables.js | 2 +- .../CalculationStore/config/initialTables.ts | 7 + .../config/initialTables/index.ts | 9 - .../config/initialTables/tableInsurance.ts | 306 --------------- .../config/initialTables/tablePayments.ts | 112 ------ .../config/initialTables/tableResults.ts | 7 - src/core/types/Calculation/Store/tables.ts | 39 +- src/core/types/Calculation/components.ts | 19 +- 17 files changed, 627 insertions(+), 613 deletions(-) delete mode 100644 src/client/Containers/Calculation/lib/elements/tables.ts create mode 100644 src/client/Containers/Calculation/lib/elements/tables/index.js create mode 100644 src/client/Containers/Calculation/lib/elements/tables/insurance.ts create mode 100644 src/client/Containers/Calculation/lib/elements/tables/payments.ts create mode 100644 src/client/Containers/Calculation/lib/elements/tables/results.ts create mode 100644 src/client/stores/CalculationStore/config/initialTables.ts delete mode 100644 src/client/stores/CalculationStore/config/initialTables/index.ts delete mode 100644 src/client/stores/CalculationStore/config/initialTables/tableInsurance.ts delete mode 100644 src/client/stores/CalculationStore/config/initialTables/tablePayments.ts delete mode 100644 src/client/stores/CalculationStore/config/initialTables/tableResults.ts diff --git a/src/client/Containers/Calculation/lib/buildElement.js b/src/client/Containers/Calculation/lib/buildElement.js index 2cd7bf1..f7ebfa8 100644 --- a/src/client/Containers/Calculation/lib/buildElement.js +++ b/src/client/Containers/Calculation/lib/buildElement.js @@ -3,9 +3,10 @@ import { withComputedValue, withLink, withTable, - withValue + withValue, } from 'client/hocs/Calculation'; import { ElementType } from 'core/types/Calculation/Store/elements'; +import { omit } from 'lodash'; import elementsActions from './elements/actions'; import elementsComponents from './elements/components'; import elementsComputedValues from './elements/computedValues'; @@ -24,7 +25,7 @@ export function buildElement(elementName) { case ElementType.Table: { return withTable(Component)({ name: elementName, - ...tables[elementName].props, + ...omit(tables[elementName], ['columns', 'rows', 'options']), }); } case ElementType.Action: { diff --git a/src/client/Containers/Calculation/lib/elements/tables.ts b/src/client/Containers/Calculation/lib/elements/tables.ts deleted file mode 100644 index 544ae1f..0000000 --- a/src/client/Containers/Calculation/lib/elements/tables.ts +++ /dev/null @@ -1,119 +0,0 @@ -import InputNumber from 'client/Elements/InputNumber'; -import { ITableElement } from 'core/types/Calculation/components'; -import { StoreTables } from 'core/types/Calculation/Store/tables'; -import Label from 'client/Elements/Label'; -import Select from 'client/Elements/Select'; -import { round } from 'lodash'; - -const elementsTables: StoreTables = { - tablePayments: { - props: { - features: { - canDeleteRow: false, - numerize: { - columnTitle: 'Номер платежа', - }, - }, - columns: [ - { - name: 'paymentRelation', - title: 'Соотношение платежа', - Component: InputNumber, - props: { - min: '0.01', - max: '100.00', - step: '1.00', - precision: 2, - // TODO: toFixed - //numeral.js - }, - }, - ], - }, - }, - tableInsurance: { - props: { - columns: [ - { - name: 'policyType', - title: 'Тип полиса', - Component: Label, - }, - { - name: 'insuranceCompany', - title: 'Страховая компания', - Component: Select, - props: { - nameMiddleware: name => { - const targetName = name.match(/"(.+)"/); - if (targetName) { - return targetName[1] - .replaceAll('"', String.fromCharCode(160)) - .trim(); - } - return name; - }, - }, - }, - { - name: 'insured', - title: 'Плательщик', - Component: Select, - }, - { - name: 'insCost', - title: 'Стоимость за первый период', - Component: InputNumber, - props: { - min: '0.00', - max: '1500000.00', - step: '1000.00', - }, - }, - { - name: 'insTerm', - title: 'Срок страхования', - Component: Select, - }, - ], - }, - }, - tableResults: { - props: { - features: { - numerize: { - columnTitle: 'Номер', - }, - }, - - columns: [ - { - name: 'paymentSum', - title: 'Сумма платежа', - Component: Label, - props: { - middleware: value => value && round(value, 2).toFixed(2), - }, - }, - { - name: 'ndsCompensation', - title: 'НДС к возмещению', - Component: Label, - props: { - middleware: value => value && round(value, 2).toFixed(2), - }, - }, - { - name: 'redemptionAmount', - title: 'Сумма досрочного выкупа', - Component: Label, - props: { - middleware: value => value && round(value, 2).toFixed(2), - }, - }, - ], - }, - }, -}; - -export default elementsTables; diff --git a/src/client/Containers/Calculation/lib/elements/tables/index.js b/src/client/Containers/Calculation/lib/elements/tables/index.js new file mode 100644 index 0000000..84df9b0 --- /dev/null +++ b/src/client/Containers/Calculation/lib/elements/tables/index.js @@ -0,0 +1,9 @@ +import insurance from './insurance'; +import payments from './payments'; +import results from './results'; + +export default { + tableInsurance: insurance, + tablePayments: payments, + tableResults: results, +}; diff --git a/src/client/Containers/Calculation/lib/elements/tables/insurance.ts b/src/client/Containers/Calculation/lib/elements/tables/insurance.ts new file mode 100644 index 0000000..37a73ee --- /dev/null +++ b/src/client/Containers/Calculation/lib/elements/tables/insurance.ts @@ -0,0 +1,364 @@ +import InputNumber from 'client/Elements/InputNumber'; +import Label from 'client/Elements/Label'; +import { openNotification } from 'client/Elements/Notification'; +import Select from 'client/Elements/Select'; +import { + insuranceKaskoDefaultFilter, + insuranceOsagoDefaultFilter, +} from 'core/constants/stores/Calculation/filters'; +import { + ITable, + TableColumn, + TableColumnCallbacks, + TableColumnOptions, + TableRow, +} from 'core/types/Calculation/Store/tables'; +import { ElementStatus } from 'core/types/statuses'; + +const columns: TableColumn[] = [ + { + name: 'policyType', + title: 'Тип полиса', + Component: Label, + }, + { + name: 'insuranceCompany', + title: 'Страховая компания', + Component: Select, + props: { + nameMiddleware: name => { + const targetName = name.match(/"(.+)"/); + if (targetName) { + return targetName[1].replaceAll('"', String.fromCharCode(160)).trim(); + } + return name; + }, + }, + }, + { + name: 'insured', + title: 'Плательщик', + Component: Select, + }, + { + name: 'insCost', + title: 'Стоимость за первый период', + Component: InputNumber, + props: { + min: '0.00', + max: '1500000.00', + step: '1000.00', + }, + }, + { + name: 'insTerm', + title: 'Срок страхования', + Component: Select, + }, +]; + +const rows: TableRow[] = [ + { + policyType: { + value: 'ОСАГО', + }, + insuranceCompany: { + value: null, + filter: insuranceOsagoDefaultFilter, + }, + insured: { + value: 100000000, + }, + insCost: { + value: 0, + }, + insTerm: { + value: 100000000, + status: ElementStatus.Disabled, + }, + }, + { + policyType: { + value: 'КАСКО', + }, + insuranceCompany: { + value: null, + filter: insuranceKaskoDefaultFilter, + }, + insured: { + value: 100000000, + }, + insCost: { + value: 0, + }, + insTerm: { + value: null, + status: ElementStatus.Disabled, + }, + }, + { + policyType: { + value: 'ДГО', + }, + insuranceCompany: { + value: null, + status: ElementStatus.Disabled, + filter: insuranceKaskoDefaultFilter, + }, + insured: { + value: null, + status: ElementStatus.Disabled, + }, + insCost: { + value: 0, + }, + insTerm: { + value: null, + status: ElementStatus.Disabled, + }, + }, + { + policyType: { + value: 'НС', + }, + insuranceCompany: { + value: null, + status: ElementStatus.Disabled, + filter: insuranceKaskoDefaultFilter, + }, + insured: { + value: null, + status: ElementStatus.Disabled, + }, + insCost: { + value: 0, + }, + insTerm: { + value: null, + status: ElementStatus.Disabled, + }, + }, +]; + +const callbacks: TableColumnCallbacks = { + insCost: ({ calculationStore, tableName, rowIndex }) => { + if ( + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > + 0 && + !calculationStore.tables[tableName]?.rows[rowIndex]['insuranceCompany'] + ?.value + ) { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insuranceCompany: { + validation: false, + }, + }); + openNotification({ + type: 'error', + title: 'Ошибка', + description: 'Не указана страховая компания по полису', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insuranceCompany: { + validation: true, + }, + }); + } + + if ( + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > + 0 && + !calculationStore.tables[tableName]?.rows[rowIndex]['insTerm']?.value + ) { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insTerm: { + validation: false, + }, + }); + openNotification({ + type: 'error', + title: 'Ошибка', + description: 'Не указан срок страхования', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insTerm: { + validation: true, + }, + }); + } + + if ( + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > + 0 && + !calculationStore.tables[tableName]?.rows[rowIndex]['insured']?.value + ) { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insured: { + validation: false, + }, + }); + openNotification({ + type: 'error', + title: 'Ошибка', + description: 'Не указан плательщик', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insured: { + validation: true, + }, + }); + } + }, + + insured: ({ calculationStore, tableName, rowIndex }) => { + if ( + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > + 0 && + !calculationStore.tables[tableName]?.rows[rowIndex]['insured']?.value + ) { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insured: { + validation: false, + }, + }); + openNotification({ + type: 'error', + title: 'Ошибка', + description: 'Не указан плательщик', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insured: { + validation: true, + }, + }); + } + }, + + insTerm: ({ calculationStore, tableName, rowIndex }) => { + if ( + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > + 0 && + !calculationStore.tables[tableName]?.rows[rowIndex]['insTerm']?.value + ) { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insTerm: { + validation: false, + }, + }); + openNotification({ + type: 'error', + title: 'Ошибка', + description: 'Не указан срок страхования', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insTerm: { + validation: true, + }, + }); + } + }, + insuranceCompany: ({ calculationStore, tableName, rowIndex }) => { + if ( + calculationStore.tables[tableName]?.rows[rowIndex] && + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && + calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > + 0 && + !calculationStore.tables[tableName]?.rows[rowIndex]['insuranceCompany'] + ?.value + ) { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insuranceCompany: { + validation: false, + }, + }); + openNotification({ + type: 'error', + title: 'Ошибка', + description: 'Не указана страховая компания по полису', + })(); + } else { + calculationStore.setTableRow( + 'tableInsurance', + rowIndex, + )({ + insuranceCompany: { + validation: true, + }, + }); + } + }, +}; + +const options: TableColumnOptions = { + insured: [ + { + name: 'ЛП', + value: 100000000, + }, + { + name: 'ЛД', + value: 100000001, + }, + ], + insTerm: [ + { + name: '12 месяцев', + value: 100000000, + }, + { + name: 'Срок ДЛ', + value: 100000001, + }, + ], +}; + +export default { + columns, + rows, + options, + callbacks, + params: {}, +} as ITable; diff --git a/src/client/Containers/Calculation/lib/elements/tables/payments.ts b/src/client/Containers/Calculation/lib/elements/tables/payments.ts new file mode 100644 index 0000000..83d2fdb --- /dev/null +++ b/src/client/Containers/Calculation/lib/elements/tables/payments.ts @@ -0,0 +1,137 @@ +import InputNumber from 'client/Elements/InputNumber'; +import valuesConstants from 'core/constants/values'; +import { rotateArrays } from 'core/tools/array'; +import { + ITable, + TableColumn, + TableColumnCallbacks, + TableColumnFeatures, +} from 'core/types/Calculation/Store/tables'; +import { inRange } from 'lodash'; +const { PERIODS_NUMBER } = valuesConstants; + +const columns: TableColumn[] = [ + { + name: 'paymentRelation', + title: 'Соотношение платежа', + Component: InputNumber, + props: { + min: '0.01', + max: '100.00', + step: '1.00', + precision: 2, + }, + }, +]; + +const callbacks: TableColumnCallbacks = { + paymentRelation: ({ calculationStore, tableName, rowIndex, value }) => { + const rowLength = calculationStore.tables[tableName].rows.length; + const { graphType, seasonType } = calculationStore.values; + + if (graphType === 100000001 && !seasonType) { + if (rowIndex > 1 && rowIndex < rowLength - 1) { + if ( + calculationStore.tables[tableName].rows[rowIndex].paymentRelation + ?.value !== + calculationStore.tables[tableName].rows[rowIndex + 1].paymentRelation + ?.value + ) + calculationStore.setTableRows( + tableName, + rowIndex, + )( + Array.from( + { + length: rowLength - rowIndex - 1, + }, + (_v, i) => ({ + paymentRelation: { + value, + }, + }), + ), + ); + } + } + + if (graphType === 100000003) { + const { leasingPeriod } = calculationStore.values; + if (rowIndex >= 1 && rowIndex <= PERIODS_NUMBER) { + const highSeasonStartValue = parseInt( + calculationStore.getOption('selectHighSeasonStart')?.name || '2', + ), + shiftNumber = highSeasonStartValue - 2; + + const seasonTypeOptions = calculationStore.getOption( + 'selectSeasonType', + ); + const startPositions = + seasonTypeOptions && seasonTypeOptions.startPositions, + endPositions = + seasonTypeOptions && + seasonTypeOptions.paymentsInStep.map( + (x, i) => startPositions[i] + x, + ); + + const allBoundaries = rotateArrays(startPositions, endPositions); + + const withUnshift = n => n - shiftNumber; + const withShift = n => n + shiftNumber; + const withFirstPayment = n => n + 1; + let index; + for (let i = 0; i < allBoundaries.length; i++) { + const [min, max] = allBoundaries[i].map(x => withFirstPayment(x)); + let unshiftedRowIndex = withUnshift(rowIndex); + if (unshiftedRowIndex <= 0) { + unshiftedRowIndex += PERIODS_NUMBER; + } + + if (inRange(unshiftedRowIndex, min, max)) { + index = i; + break; + } + } + + const paymentsInStep = + seasonTypeOptions && seasonTypeOptions.paymentsInStep; + + for ( + let i = + withFirstPayment(withShift(allBoundaries[index][0])) - + PERIODS_NUMBER; + i < leasingPeriod - 1; + i += PERIODS_NUMBER + ) { + const targetStartPosition = i; + for ( + let j = targetStartPosition; + j < paymentsInStep[index] + targetStartPosition; + j++ + ) { + if (j > 0 && j < leasingPeriod - 1) + calculationStore.setTableRow( + tableName, + j, + )({ paymentRelation: { value } }); + } + } + } + } + }, +}; + +const features: TableColumnFeatures = { + numerize: { + columnTitle: 'Номер платежа', + }, +}; + +const params = { features }; + +export default { + columns, + rows: [], + params, + callbacks, +} as ITable; diff --git a/src/client/Containers/Calculation/lib/elements/tables/results.ts b/src/client/Containers/Calculation/lib/elements/tables/results.ts new file mode 100644 index 0000000..4d7b459 --- /dev/null +++ b/src/client/Containers/Calculation/lib/elements/tables/results.ts @@ -0,0 +1,48 @@ +import Label from 'client/Elements/Label'; +import { + ITable, + TableColumn, + TableColumnFeatures, +} from 'core/types/Calculation/Store/tables'; +import { round } from 'lodash'; + +const columns: TableColumn[] = [ + { + name: 'paymentSum', + title: 'Сумма платежа', + Component: Label, + props: { + middleware: value => value && round(value, 2).toFixed(2), + }, + }, + { + name: 'ndsCompensation', + title: 'НДС к возмещению', + Component: Label, + props: { + middleware: value => value && round(value, 2).toFixed(2), + }, + }, + { + name: 'redemptionAmount', + title: 'Сумма досрочного выкупа', + Component: Label, + props: { + middleware: value => value && round(value, 2).toFixed(2), + }, + }, +]; + +const features: TableColumnFeatures = { + numerize: { + columnTitle: 'Номер', + }, +}; + +const params = { features }; + +export default { + columns, + rows: [], + params, +} as ITable; diff --git a/src/client/Elements/Table.jsx b/src/client/Elements/Table.jsx index 386ea3b..91d64ec 100644 --- a/src/client/Elements/Table.jsx +++ b/src/client/Elements/Table.jsx @@ -1,11 +1,8 @@ -import { withTableValue } from 'client/hocs/Calculation/withTable'; import colors from 'client/UIKit/colors'; import { Box } from 'client/UIKit/grid'; import mq from 'client/UIKit/mq'; import styled from 'styled-components'; -// import { Button as AntButton } from 'antd'; - const TableWrapper = styled(Box)` overflow-x: auto; table { @@ -62,15 +59,14 @@ const TableWrapper = styled(Box)` } `; -// const DeleteRowButton = ({ onClick }) => ( -// -// -// Удалить -// -// -// ); - -const Table = ({ name: tableName, columns, rows, features, actions }) => { +const Table = ({ + name: tableName, + columns, + rows, + params: { features }, + withTableValue, + callbacks, +}) => { return ( @@ -82,7 +78,6 @@ const Table = ({ name: tableName, columns, rows, features, actions }) => { {columns.map(({ title }, ci) => { return ; })} - {/* {features && features.canDeleteRow && @@ -100,6 +95,7 @@ const Table = ({ name: tableName, columns, rows, features, actions }) => { tableName, rowIndex: ri, propName: rowPropName, + columnCallback: callbacks[rowPropName], ...columns[columnIndex].props, }); return ( @@ -108,15 +104,6 @@ const Table = ({ name: tableName, columns, rows, features, actions }) => { ); })} - {/* {features && features.canDeleteRow && ( - - )} */} ); })} diff --git a/src/client/hocs/Calculation/withTable.jsx b/src/client/hocs/Calculation/withTable.jsx index 19c3738..2648754 100644 --- a/src/client/hocs/Calculation/withTable.jsx +++ b/src/client/hocs/Calculation/withTable.jsx @@ -10,14 +10,17 @@ export default Table => props => { const ObservedTable = observer(Table); const { calculationStore } = useStores(); const tableData = calculationStore.tables[tableName]; - return () => ; + return () => ( + + ); }; -export const withTableValue = Component => ({ +const withTableValue = Component => ({ tableName, rowIndex, propName, validation, + columnCallback, ...props }) => observer(() => { @@ -25,6 +28,7 @@ export const withTableValue = Component => ({ tableName, rowIndex, propName, + columnCallback, }); const { status } = useTableStatus({ tableName, rowIndex, propName }); const { validateStatus, message } = useTableValidation({ diff --git a/src/client/hooks/Calculation/useValue.js b/src/client/hooks/Calculation/useValue.js index a488695..65ec092 100644 --- a/src/client/hooks/Calculation/useValue.js +++ b/src/client/hooks/Calculation/useValue.js @@ -34,13 +34,18 @@ export const useStoreValue = ({ valueName }) => { return { value: currentValue, setCurrentValue }; }; -export const useTableValue = ({ tableName, rowIndex, propName }) => { +export const useTableValue = ({ + tableName, + rowIndex, + propName, + columnCallback, +}) => { const { calculationStore, calculationProcess } = useStores(); const [currentValue, setCurrentValue] = useState(undefined); //get row value from store - const sourceValue = - calculationStore.tables[tableName].rows[rowIndex][propName].value; + const targetTable = calculationStore.tables[tableName]; + const sourceValue = targetTable.rows[rowIndex][propName].value; useEffect(() => { if (sourceValue !== undefined) { setCurrentValue(sourceValue); @@ -84,14 +89,11 @@ export const useTableValue = ({ tableName, rowIndex, propName }) => { }, DEFAULT_DEBOUNCE_DELAY, ); - const cellCallBack = - calculationStore.tables[tableName].callbacks && - calculationStore.tables[tableName].callbacks[propName]; useEffect(() => { - if (cellCallBack && calculationProcess.process === Process.Default) + if (columnCallback && calculationProcess.process === Process.Default) debouncedCellCallback( - cellCallBack, + columnCallback, calculationStore, calculationProcess, tableName, diff --git a/src/client/stores/CalculationStore/Data/tables.js b/src/client/stores/CalculationStore/Data/tables.js index f1d028f..b17bc74 100644 --- a/src/client/stores/CalculationStore/Data/tables.js +++ b/src/client/stores/CalculationStore/Data/tables.js @@ -1,5 +1,5 @@ -import initialTables from 'client/stores/CalculationStore/config/initialTables'; import { merge, mergeWith } from 'lodash'; +import initialTables from '../config/initialTables'; const tablesData = { tables: initialTables, diff --git a/src/client/stores/CalculationStore/config/initialTables.ts b/src/client/stores/CalculationStore/config/initialTables.ts new file mode 100644 index 0000000..0212a40 --- /dev/null +++ b/src/client/stores/CalculationStore/config/initialTables.ts @@ -0,0 +1,7 @@ +import tables from 'client/Containers/Calculation/lib/elements/tables'; +import { pick } from 'lodash'; + +export default Object.keys(tables).reduce((o, tableName) => { + o[tableName] = pick(tables[tableName], ['columns', 'rows', 'options']); + return o; +}, {}); diff --git a/src/client/stores/CalculationStore/config/initialTables/index.ts b/src/client/stores/CalculationStore/config/initialTables/index.ts deleted file mode 100644 index 1634580..0000000 --- a/src/client/stores/CalculationStore/config/initialTables/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import tableInsurance from './tableInsurance'; -import tablePayments from './tablePayments'; -import tableResults from './tableResults'; - -export default { - tableInsurance, - tablePayments, - tableResults, -}; diff --git a/src/client/stores/CalculationStore/config/initialTables/tableInsurance.ts b/src/client/stores/CalculationStore/config/initialTables/tableInsurance.ts deleted file mode 100644 index 41ed3f9..0000000 --- a/src/client/stores/CalculationStore/config/initialTables/tableInsurance.ts +++ /dev/null @@ -1,306 +0,0 @@ -import { openNotification } from 'client/Elements/Notification'; -import { - insuranceKaskoDefaultFilter, - insuranceOsagoDefaultFilter, -} from 'core/constants/stores/Calculation/filters'; -import { ITable } from 'core/types/Calculation/Store/tables'; -import { ElementStatus } from 'core/types/statuses'; - -const tableInsurance: ITable = { - rows: [ - { - policyType: { - value: 'ОСАГО', - }, - insuranceCompany: { - value: null, - filter: insuranceOsagoDefaultFilter, - }, - insured: { - value: 100000000, - }, - insCost: { - value: 0, - }, - insTerm: { - value: 100000000, - status: ElementStatus.Disabled, - }, - }, - { - policyType: { - value: 'КАСКО', - }, - insuranceCompany: { - value: null, - filter: insuranceKaskoDefaultFilter, - }, - insured: { - value: 100000000, - }, - insCost: { - value: 0, - }, - insTerm: { - value: null, - status: ElementStatus.Disabled, - }, - }, - { - policyType: { - value: 'ДГО', - }, - insuranceCompany: { - value: null, - status: ElementStatus.Disabled, - filter: insuranceKaskoDefaultFilter, - }, - insured: { - value: null, - status: ElementStatus.Disabled, - }, - insCost: { - value: 0, - }, - insTerm: { - value: null, - status: ElementStatus.Disabled, - }, - }, - { - policyType: { - value: 'НС', - }, - insuranceCompany: { - value: null, - status: ElementStatus.Disabled, - filter: insuranceKaskoDefaultFilter, - }, - insured: { - value: null, - status: ElementStatus.Disabled, - }, - insCost: { - value: 0, - }, - insTerm: { - value: null, - status: ElementStatus.Disabled, - }, - }, - ], - options: { - insured: [ - { - name: 'ЛП', - value: 100000000, - }, - { - name: 'ЛД', - value: 100000001, - }, - ], - insTerm: [ - { - name: '12 месяцев', - value: 100000000, - }, - { - name: 'Срок ДЛ', - value: 100000001, - }, - ], - }, - callbacks: { - insCost: ({ calculationStore, tableName, rowIndex }) => { - if ( - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > - 0 && - !calculationStore.tables[tableName]?.rows[rowIndex]['insuranceCompany'] - ?.value - ) { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insuranceCompany: { - validation: false, - }, - }); - openNotification({ - type: 'error', - title: 'Ошибка', - description: 'Не указана страховая компания по полису', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insuranceCompany: { - validation: true, - }, - }); - } - - if ( - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > - 0 && - !calculationStore.tables[tableName]?.rows[rowIndex]['insTerm']?.value - ) { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insTerm: { - validation: false, - }, - }); - openNotification({ - type: 'error', - title: 'Ошибка', - description: 'Не указан срок страхования', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insTerm: { - validation: true, - }, - }); - } - - if ( - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > - 0 && - !calculationStore.tables[tableName]?.rows[rowIndex]['insured']?.value - ) { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insured: { - validation: false, - }, - }); - openNotification({ - type: 'error', - title: 'Ошибка', - description: 'Не указан плательщик', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insured: { - validation: true, - }, - }); - } - }, - - insured: ({ calculationStore, tableName, rowIndex }) => { - if ( - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > - 0 && - !calculationStore.tables[tableName]?.rows[rowIndex]['insured']?.value - ) { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insured: { - validation: false, - }, - }); - openNotification({ - type: 'error', - title: 'Ошибка', - description: 'Не указан плательщик', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insured: { - validation: true, - }, - }); - } - }, - - insTerm: ({ calculationStore, tableName, rowIndex }) => { - if ( - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > - 0 && - !calculationStore.tables[tableName]?.rows[rowIndex]['insTerm']?.value - ) { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insTerm: { - validation: false, - }, - }); - openNotification({ - type: 'error', - title: 'Ошибка', - description: 'Не указан срок страхования', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insTerm: { - validation: true, - }, - }); - } - }, - insuranceCompany: ({ calculationStore, tableName, rowIndex }) => { - if ( - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value && - calculationStore.tables[tableName]?.rows[rowIndex]['insCost']?.value > - 0 && - !calculationStore.tables[tableName]?.rows[rowIndex]['insuranceCompany'] - ?.value - ) { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insuranceCompany: { - validation: false, - }, - }); - openNotification({ - type: 'error', - title: 'Ошибка', - description: 'Не указана страховая компания по полису', - })(); - } else { - calculationStore.setTableRow( - 'tableInsurance', - rowIndex, - )({ - insuranceCompany: { - validation: true, - }, - }); - } - }, - }, -}; - -export default tableInsurance; diff --git a/src/client/stores/CalculationStore/config/initialTables/tablePayments.ts b/src/client/stores/CalculationStore/config/initialTables/tablePayments.ts deleted file mode 100644 index b687bde..0000000 --- a/src/client/stores/CalculationStore/config/initialTables/tablePayments.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { rotateArrays } from 'core/tools/array'; -import { ITable } from 'core/types/Calculation/Store/tables'; -import { inRange } from 'lodash'; -import valuesConstants from 'core/constants/values'; -const { PERIODS_NUMBER } = valuesConstants; - -const tablePayments: ITable = { - rows: [ - // { - // paymentRelation: { value: 5 }, - // }, - ], - - callbacks: { - paymentRelation: ({ calculationStore, tableName, rowIndex, value }) => { - const rowLength = calculationStore.tables[tableName].rows.length; - const { graphType, seasonType } = calculationStore.values; - - if (graphType === 100000001 && !seasonType) { - if (rowIndex > 1 && rowIndex < rowLength - 1) { - if ( - calculationStore.tables[tableName].rows[rowIndex].paymentRelation - ?.value !== - calculationStore.tables[tableName].rows[rowIndex + 1] - .paymentRelation?.value - ) - calculationStore.setTableRows( - tableName, - rowIndex, - )( - Array.from( - { - length: rowLength - rowIndex - 1, - }, - (_v, i) => ({ - paymentRelation: { - value, - }, - }), - ), - ); - } - } - - if (graphType === 100000003) { - const { leasingPeriod } = calculationStore.values; - if (rowIndex >= 1 && rowIndex <= PERIODS_NUMBER) { - const highSeasonStartValue = parseInt( - calculationStore.getOption('selectHighSeasonStart')?.name || '2', - ), - shiftNumber = highSeasonStartValue - 2; - - const seasonTypeOptions = calculationStore.getOption( - 'selectSeasonType', - ); - const startPositions = - seasonTypeOptions && seasonTypeOptions.startPositions, - endPositions = - seasonTypeOptions && - seasonTypeOptions.paymentsInStep.map( - (x, i) => startPositions[i] + x, - ); - - const allBoundaries = rotateArrays(startPositions, endPositions); - - const withUnshift = n => n - shiftNumber; - const withShift = n => n + shiftNumber; - const withFirstPayment = n => n + 1; - let index; - for (let i = 0; i < allBoundaries.length; i++) { - const [min, max] = allBoundaries[i].map(x => withFirstPayment(x)); - let unshiftedRowIndex = withUnshift(rowIndex); - if (unshiftedRowIndex <= 0) { - unshiftedRowIndex += PERIODS_NUMBER; - } - - if (inRange(unshiftedRowIndex, min, max)) { - index = i; - break; - } - } - - const paymentsInStep = - seasonTypeOptions && seasonTypeOptions.paymentsInStep; - - for ( - let i = - withFirstPayment(withShift(allBoundaries[index][0])) - - PERIODS_NUMBER; - i < leasingPeriod - 1; - i += PERIODS_NUMBER - ) { - const targetStartPosition = i; - for ( - let j = targetStartPosition; - j < paymentsInStep[index] + targetStartPosition; - j++ - ) { - if (j > 0 && j < leasingPeriod - 1) - calculationStore.setTableRow( - tableName, - j, - )({ paymentRelation: { value } }); - } - } - } - } - }, - }, -}; - -export default tablePayments; diff --git a/src/client/stores/CalculationStore/config/initialTables/tableResults.ts b/src/client/stores/CalculationStore/config/initialTables/tableResults.ts deleted file mode 100644 index 976a084..0000000 --- a/src/client/stores/CalculationStore/config/initialTables/tableResults.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ITable } from 'core/types/Calculation/Store/tables'; - -const tableResults: ITable = { - rows: [], -}; - -export default tableResults; diff --git a/src/core/types/Calculation/Store/tables.ts b/src/core/types/Calculation/Store/tables.ts index 541f7fe..f9c95e6 100644 --- a/src/core/types/Calculation/Store/tables.ts +++ b/src/core/types/Calculation/Store/tables.ts @@ -1,4 +1,5 @@ import { ElementStatus } from '../../statuses'; +import { ElementProps } from '../components'; import { ICalculationStore } from './'; import { TCRMEntity } from './../../Entities/crmEntities'; import { TCalculationProcess } from './effect'; @@ -26,22 +27,46 @@ export type TCellCallback = (props: { value: any; }) => void; -export interface ITableCell { +export type ITableCell = { value?: any; status?: ElementStatus; validation?: boolean; filter?: TElementFilter; -} +}; export type TableProps = { [propName in TableValuesNames]?: T; }; -export interface ITable { - rows: TableProps[]; - options?: TableProps<(IBaseOption & TCRMEntity)[]>; - callbacks?: TableProps; -} +export type TableRow = TableProps; +export type TableColumnOptions = TableProps<(IBaseOption & TCRMEntity)[]>; +export type TableColumnCallbacks = TableProps; + +export type TableColumn = { + name: TableValuesNames; + title: string; + Component: () => JSX.Element; + props?: ElementProps; +}; + +export type TableColumnFeatures = { + numerize?: { + columnTitle?: string; + }; +}; + +export type TableParams = { + features?: TableColumnFeatures; + [param: string]: any; +}; + +export type ITable = { + columns: TableColumn[]; + rows: TableRow[]; + options?: TableColumnOptions; + params?: TableParams; + callbacks?: TableColumnCallbacks; +}; export type StoreTables = { [table in TableNames]: T; diff --git a/src/core/types/Calculation/components.ts b/src/core/types/Calculation/components.ts index 4a80912..0617091 100644 --- a/src/core/types/Calculation/components.ts +++ b/src/core/types/Calculation/components.ts @@ -3,27 +3,10 @@ import { LinkElementsNames, ResultElementsNames, } from './Store/elements'; -import { TableNames, TableValuesNames } from './Store/tables'; +import { TableNames } from './Store/tables'; export type ElementProps = { [key: string]: any }; export type Component = () => JSX.Element; -export interface ITableElement { - title?: string; - props: { - features?: { - canDeleteRow?: boolean; - numerize: { - columnTitle?: string; - }; - }; - columns: { - name: TableValuesNames; - title: string; - Component: () => JSX.Element; - props?: ElementProps; - }[]; - }; -} interface IBlock { title?: string;
{title}} */}
- { - actions.deleteRow(ri); - }} - /> -