2022-02-07 15:01:07 +05:00
|
|
|
|
import { useState, useEffect, memo } from 'react'
|
2021-10-07 17:23:01 +05:00
|
|
|
|
import { Button, Form, Input, Popconfirm, Timeline } from 'antd'
|
2021-11-17 14:08:13 +05:00
|
|
|
|
import {
|
|
|
|
|
CheckSquareOutlined,
|
|
|
|
|
EditOutlined,
|
|
|
|
|
SaveOutlined,
|
|
|
|
|
PlusOutlined,
|
|
|
|
|
CloseCircleOutlined,
|
|
|
|
|
DeleteOutlined
|
|
|
|
|
} from '@ant-design/icons'
|
2021-12-23 18:23:21 +05:00
|
|
|
|
|
2022-02-07 15:01:07 +05:00
|
|
|
|
import LoaderPortal from '@components/LoaderPortal'
|
|
|
|
|
import { invokeWebApiWrapperAsync } from '@components/factory'
|
|
|
|
|
import { MeasureService } from '@api'
|
|
|
|
|
import { formatDate } from '@utils'
|
2021-12-23 18:23:21 +05:00
|
|
|
|
|
|
|
|
|
import { View } from './View'
|
|
|
|
|
|
2022-02-07 15:01:07 +05:00
|
|
|
|
import '@styles/index.css'
|
|
|
|
|
import '@styles/measure.css'
|
2021-12-23 18:23:21 +05:00
|
|
|
|
|
|
|
|
|
const createEditingColumns = (cols, renderDelegate) =>
|
|
|
|
|
cols.map(col => ({ render: renderDelegate, ...col }))
|
2021-10-05 17:55:20 +05:00
|
|
|
|
|
2022-02-07 15:01:07 +05:00
|
|
|
|
export const MeasureTable = memo(({idWell, group, updateMeasuresFunc, additionalButtons}) => {
|
2021-11-17 14:08:13 +05:00
|
|
|
|
const [showLoader, setShowLoader] = useState(false)
|
|
|
|
|
const [displayedValues, setDisplayedValues] = useState({})
|
|
|
|
|
const [editingColumns, setEditingColumns] = useState(group.columns)
|
|
|
|
|
const [isTableEditing, setIsTableEditing] = useState(false)
|
|
|
|
|
const [editingActionName, setEditingActionName] = useState('')
|
|
|
|
|
const [data, setData] = useState([])
|
2021-10-05 17:55:20 +05:00
|
|
|
|
|
2021-11-17 14:08:13 +05:00
|
|
|
|
const [measuresForm] = Form.useForm()
|
2021-10-06 13:57:04 +05:00
|
|
|
|
|
2021-10-06 16:06:07 +05:00
|
|
|
|
useEffect(() => {
|
2021-12-23 18:23:21 +05:00
|
|
|
|
let data = [group.defaultValue]
|
|
|
|
|
if (group?.values?.length > 0)
|
|
|
|
|
data = group.values
|
2021-11-17 14:08:13 +05:00
|
|
|
|
setData(data)
|
|
|
|
|
setDisplayedValues(data.at(-1))
|
|
|
|
|
}, [group.defaultValue, group.values])
|
2021-10-06 16:06:07 +05:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-17 14:08:13 +05:00
|
|
|
|
const switchableColumns = createEditingColumns(
|
|
|
|
|
group.columns,
|
|
|
|
|
isTableEditing ? () => <Input className={'w-100 measure-input'} /> : null
|
|
|
|
|
)
|
2021-10-06 16:06:07 +05:00
|
|
|
|
|
2021-10-07 17:23:01 +05:00
|
|
|
|
if(editingActionName === 'edit')
|
2021-11-17 14:08:13 +05:00
|
|
|
|
measuresForm.setFieldsValue(displayedValues?.data)
|
2021-10-07 17:23:01 +05:00
|
|
|
|
else if(editingActionName === 'add')
|
2021-10-07 10:48:26 +05:00
|
|
|
|
measuresForm.resetFields()
|
|
|
|
|
|
2021-10-06 16:06:07 +05:00
|
|
|
|
setEditingColumns(switchableColumns)
|
2021-11-17 14:08:13 +05:00
|
|
|
|
}, [isTableEditing, group.columns, editingActionName, displayedValues?.data, measuresForm])
|
|
|
|
|
|
|
|
|
|
const markMeasuresAsDeleted = async () => await invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
|
|
|
|
await MeasureService.markAsDelete(idWell, displayedValues.id)
|
|
|
|
|
updateMeasuresFunc()
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
2022-02-07 17:44:46 +05:00
|
|
|
|
`Не удалось удалить запись ${displayedValues.id} для скважины "${idWell}"`,
|
|
|
|
|
'Удаление записи для скважины'
|
2021-11-17 14:08:13 +05:00
|
|
|
|
)
|
2021-10-07 17:23:01 +05:00
|
|
|
|
|
2021-11-17 14:08:13 +05:00
|
|
|
|
const isDataDefault = () => !!displayedValues?.isDefaultData
|
2021-10-06 13:57:04 +05:00
|
|
|
|
|
2021-11-17 14:08:13 +05:00
|
|
|
|
const editTable = (action) => {
|
|
|
|
|
setEditingActionName(action)
|
|
|
|
|
setIsTableEditing(true)
|
2021-10-06 13:57:04 +05:00
|
|
|
|
}
|
2021-08-28 22:32:13 +05:00
|
|
|
|
|
2021-11-17 14:08:13 +05:00
|
|
|
|
const handleSubmitMeasuresForm = async (formData) => await invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
|
|
|
|
measuresForm.validateFields()
|
|
|
|
|
|
|
|
|
|
const measureParams = {
|
|
|
|
|
idWell: idWell,
|
|
|
|
|
idCategory: group.idCategory,
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
data: formData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(editingActionName === 'add') {
|
|
|
|
|
await MeasureService.insert(idWell, measureParams)
|
|
|
|
|
} else if (editingActionName === 'edit') {
|
|
|
|
|
measureParams.id = displayedValues.id
|
|
|
|
|
measureParams.timestamp = displayedValues.timestamp
|
|
|
|
|
await MeasureService.update(idWell, measureParams)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsTableEditing(false)
|
|
|
|
|
updateMeasuresFunc()
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
2022-02-07 17:44:46 +05:00
|
|
|
|
`Не удалось добавить/изменить запись для скаважины "${idWell}"`,
|
|
|
|
|
'Добавление/изменение записи по скважине'
|
2021-11-17 14:08:13 +05:00
|
|
|
|
)
|
2021-10-05 17:55:20 +05:00
|
|
|
|
|
2021-11-17 14:08:13 +05:00
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<h2>{group.title}</h2>
|
|
|
|
|
<div className={'d-flex'}>
|
|
|
|
|
<div className={'flex-direction-column'}>
|
|
|
|
|
<div className={'measure-buttons-container'}>
|
|
|
|
|
<div className={'w-300px mt-8px d-flex'} style={{ alignItems: 'stretch' }}>
|
|
|
|
|
{ isTableEditing ? (
|
|
|
|
|
<>
|
|
|
|
|
<Button key={'confirm'} className={'flex-1'} onClick={() => measuresForm.submit()}>
|
|
|
|
|
<SaveOutlined />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button key={'decline'} className={'flex-1'} onClick={() => setIsTableEditing(false)}>
|
|
|
|
|
<CloseCircleOutlined />
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<Button key={'add'} className={'flex-1'} onClick={() => editTable('add')}>
|
|
|
|
|
<PlusOutlined />
|
|
|
|
|
</Button>
|
|
|
|
|
<Button key={'edit'} className={'flex-1'} onClick={() => editTable('edit')} disabled={isDataDefault()}>
|
|
|
|
|
<EditOutlined />
|
|
|
|
|
</Button>
|
2021-12-27 15:18:20 +05:00
|
|
|
|
<Popconfirm style={{ flex: '1' }} title={'Удалить данные?'} onConfirm={markMeasuresAsDeleted} disabled={isDataDefault()}>
|
|
|
|
|
<Button key={'delete'} onClick={() => setEditingActionName('delete')} disabled={isDataDefault()}>
|
2021-11-17 14:08:13 +05:00
|
|
|
|
<DeleteOutlined style={{ margin:'auto 28px' }}/>
|
|
|
|
|
</Button>
|
|
|
|
|
</Popconfirm>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
{additionalButtons?.(isTableEditing)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className={'measure-dates mt-20px'}>
|
|
|
|
|
<Timeline className={'mt-12px ml-10px'}>
|
|
|
|
|
{data.map((item, index) =>
|
|
|
|
|
<Timeline.Item
|
|
|
|
|
key={index}
|
|
|
|
|
className={'measure-button'}
|
|
|
|
|
onClick={() => setDisplayedValues(item)}
|
2021-12-23 18:23:21 +05:00
|
|
|
|
dot={item?.id === displayedValues?.id &&
|
2021-11-17 14:08:13 +05:00
|
|
|
|
<CheckSquareOutlined className={'timeline-clock-icon'} />
|
|
|
|
|
}
|
|
|
|
|
>
|
2021-12-27 15:18:20 +05:00
|
|
|
|
<span className={item?.id === displayedValues?.id ? 'selected-timeline' : undefined}>
|
|
|
|
|
{formatDate(item.timestamp, true) ?? 'Нет данных'}
|
2021-11-17 14:08:13 +05:00
|
|
|
|
</span>
|
|
|
|
|
</Timeline.Item>
|
|
|
|
|
)}
|
|
|
|
|
</Timeline>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={'w-100'}>
|
|
|
|
|
<LoaderPortal show={showLoader}>
|
|
|
|
|
<Form form={measuresForm} onFinish={handleSubmitMeasuresForm}>
|
|
|
|
|
<View item={displayedValues?.data ?? {}} columns={editingColumns} />
|
|
|
|
|
</Form>
|
|
|
|
|
</LoaderPortal>
|
2021-10-05 17:55:20 +05:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2021-11-17 14:08:13 +05:00
|
|
|
|
</>
|
|
|
|
|
)
|
2022-02-07 15:01:07 +05:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default MeasureTable
|