diff --git a/src/components/Table/Columns/numeric.tsx b/src/components/Table/Columns/numeric.tsx index a205283..d1a2aef 100755 --- a/src/components/Table/Columns/numeric.tsx +++ b/src/components/Table/Columns/numeric.tsx @@ -1,38 +1,20 @@ import { InputNumber } from 'antd' import { ReactNode } from 'react' -import { makeNumericSorter, makeObjNumericSorter } from '../sorters' +import { makeNumericSorter } from '../sorters' import { columnPropsOther, makeGroupColumn, RenderMethod } from '.' export const RegExpIsFloat = /^[-+]?\d+\.?\d*$/ -export const makeNumericRender = (fixed?: number): RenderMethod => (value) => { +export const makeNumericRender = (fixed?: number, columnKey?: string): RenderMethod => (value, obj) => { let val = '-' + const isSelected = obj && columnKey ? obj[columnKey] : false if ((value ?? null) !== null && Number.isFinite(+value)) { val = (fixed ?? null) !== null ? (+value).toFixed(fixed) : (+value).toPrecision(5) } - return ( -
- {val} -
- ) -} - -type ObjRenderType = { - max: number - avg: number - min: number - isMax: boolean - isMin: boolean -} - -export const makeObjRender = (key: keyof ObjRenderType, boolKey?: keyof ObjRenderType): RenderMethod => (value: ObjRenderType) => { - const val = key ? value[key]: '-' - const isSelected = boolKey ? value[boolKey] : false - return (
{val} @@ -40,7 +22,7 @@ export const makeObjRender = (key: keyof ObjRenderType, boolKey?: keyof ObjRende ) } -export const makeNumericColumnOptions = (fixed?: number, sorterKey?: string): columnPropsOther => ({ +export const makeNumericColumnOptions = (fixed?: number, sorterKey?: string, columnKey?: string): columnPropsOther => ({ editable: true, initialValue: 0, width: 100, @@ -50,20 +32,7 @@ export const makeNumericColumnOptions = (fixed?: number, sorterKey?: string): co message: 'Введите число', pattern: RegExpIsFloat, }], - render: makeNumericRender(fixed), -}) - -export const makeObjColumnOptions = (key: keyof ObjRenderType, sortedObj?: string, boolKey?: keyof ObjRenderType): columnPropsOther => ({ - editable: true, - initialValue: 0, - width: 100, - sorter: sortedObj && key ? makeObjNumericSorter(sortedObj, key) : undefined, - formItemRules: [{ - required: true, - message: 'Введите число', - pattern: RegExpIsFloat, - }], - render: makeObjRender(key, boolKey), + render: makeNumericRender(fixed, columnKey), }) export const makeNumericColumn = ( @@ -135,9 +104,9 @@ export const makeNumericAvgRange = ( renderDelegate: (_: any, row: object) => any, width: string ) => makeGroupColumn(title, [ - makeNumericColumn('мин', dataIndex, filters, filterDelegate, renderDelegate, width, makeObjColumnOptions('min', dataIndex, 'isMin')), - makeNumericColumn('сред', dataIndex, filters, filterDelegate, renderDelegate, width, makeObjColumnOptions('avg', dataIndex)), - makeNumericColumn('макс', dataIndex, filters, filterDelegate, renderDelegate, width, makeObjColumnOptions('max', dataIndex, 'isMax')) + makeNumericColumn('мин', dataIndex + 'Min', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Min', dataIndex + 'IsMin')), + makeNumericColumn('сред', dataIndex + 'Avg', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Avg')), + makeNumericColumn('макс', dataIndex + 'Max', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Max', dataIndex + 'IsMax')) ]) export default makeNumericColumn diff --git a/src/components/Table/sorters.ts b/src/components/Table/sorters.ts index f0a1ac9..11fb7e2 100755 --- a/src/components/Table/sorters.ts +++ b/src/components/Table/sorters.ts @@ -7,9 +7,6 @@ import { DataType } from './Columns' export const makeNumericSorter = (key: keyof DataType) => (a: DataType, b: DataType) => Number(a[key]) - Number(b[key]) -export const makeObjNumericSorter = (obj: keyof DataType, key: keyof DataType) => - (a: DataType>, b: DataType>) => Number(a[obj][key]) - Number(b[obj][key]) - export const makeStringSorter = (key: keyof DataType) => (a?: DataType | null, b?: DataType | null) => { if (!a && !b) return 0 if (!a) return 1 diff --git a/src/pages/Analytics/WellCompositeEditor/NewParamsTable.jsx b/src/pages/Analytics/WellCompositeEditor/NewParamsTable.jsx index bf504df..817b617 100755 --- a/src/pages/Analytics/WellCompositeEditor/NewParamsTable.jsx +++ b/src/pages/Analytics/WellCompositeEditor/NewParamsTable.jsx @@ -6,6 +6,7 @@ import { Table } from '@components/Table' import LoaderPortal from '@components/LoaderPortal' import { invokeWebApiWrapperAsync } from '@components/factory' import { DrillParamsService } from '@api' +import { getArrayConvertedObjectsIntoObjectsWithPrimitiveValues } from '@utils' import { getColumns } from '@pages/WellOperations/WellDrillParams' @@ -25,7 +26,7 @@ export const NewParamsTable = memo(({ selectedWellsKeys }) => { async () => { setIsParamsModalVisible(true) const params = await DrillParamsService.getCompositeAll(well.id) - setParams(params) + setParams(getArrayConvertedObjectsIntoObjectsWithPrimitiveValues(params)) }, setShowParamsLoader, `Не удалось загрузить список режимов`, diff --git a/src/pages/WellOperations/WellDrillParams.jsx b/src/pages/WellOperations/WellDrillParams.jsx index aab2d2d..236c7c6 100755 --- a/src/pages/WellOperations/WellDrillParams.jsx +++ b/src/pages/WellOperations/WellDrillParams.jsx @@ -2,7 +2,7 @@ import { useState, useEffect, useCallback, memo, useMemo } from 'react' import { useWell } from '@asb/context' import { - Table, + EditableTable, makeSelectColumn, makeNumericAvgRange, makeNumericSorter, @@ -10,7 +10,7 @@ import { import LoaderPortal from '@components/LoaderPortal' import { invokeWebApiWrapperAsync } from '@components/factory' import { DrillParamsService, WellOperationService } from '@api' -import { arrayOrDefault } from '@utils' +import { arrayOrDefault, getArrayConvertedObjectsIntoObjectsWithPrimitiveValues } from '@utils' export const getColumns = async (idWell) => { @@ -44,9 +44,7 @@ export const WellDrillParams = memo(() => { const updateParams = useCallback(async () => await invokeWebApiWrapperAsync( async () => { const params = arrayOrDefault(await DrillParamsService.getAll(well.id)) - // Typescript против использования числа в качестве типа значения select - params.forEach((param) => param.idWellSectionType = `${param.idWellSectionType}`) - setParams(params) + setParams(getArrayConvertedObjectsIntoObjectsWithPrimitiveValues(params)) }, setShowLoader, `Не удалось загрузить список режимов бурения`, @@ -81,12 +79,15 @@ export const WellDrillParams = memo(() => { return ( - diff --git a/src/utils/functions/getArrayConvertedObjectsIntoObjectsWithPrimitiveValues.ts b/src/utils/functions/getArrayConvertedObjectsIntoObjectsWithPrimitiveValues.ts new file mode 100644 index 0000000..c8a72f4 --- /dev/null +++ b/src/utils/functions/getArrayConvertedObjectsIntoObjectsWithPrimitiveValues.ts @@ -0,0 +1,26 @@ +export type ObjType = { + [key:string]: Object | any +} + +export function getArrayConvertedObjectsIntoObjectsWithPrimitiveValues(arr: Array) { + return arr.map(param => { + const newObj:ObjType = {} + for (const paramElement in param) { + if (typeof param[paramElement] === 'object') { + for (const item in param[paramElement]) { + newObj[paramElement + item[0].toUpperCase() + item.slice(1)] = param[paramElement][item] + } + } else { + // Typescript против использования числа в качестве типа значения select + if (paramElement === 'idWellSectionType') { + newObj[paramElement] = String(param[paramElement]) + } else { + newObj[paramElement] = param[paramElement] + } + } + } + return newObj + }) +} + +export default getArrayConvertedObjectsIntoObjectsWithPrimitiveValues \ No newline at end of file diff --git a/src/utils/functions/index.ts b/src/utils/functions/index.ts index 69fcf3c..87e4506 100644 --- a/src/utils/functions/index.ts +++ b/src/utils/functions/index.ts @@ -27,3 +27,6 @@ export * from './svg' export * from './table_settings' export type { TableColumnSettings, TableSettings, TableSettingsStore } from './table_settings' + +export * from './getArrayConvertedObjectsIntoObjectsWithPrimitiveValues' +export type { ObjType } from './getArrayConvertedObjectsIntoObjectsWithPrimitiveValues'