2021-10-06 13:57:04 +05:00
|
|
|
import { useState, useEffect } from 'react'
|
2021-10-07 17:23:01 +05:00
|
|
|
import { Button, Form, Input, Popconfirm, Timeline } from 'antd'
|
2021-10-05 17:55:20 +05:00
|
|
|
import moment from 'moment'
|
2021-10-07 17:23:01 +05:00
|
|
|
import { CheckSquareOutlined,
|
|
|
|
EditOutlined,
|
|
|
|
SaveOutlined,
|
|
|
|
PlusOutlined,
|
|
|
|
CloseCircleOutlined,
|
|
|
|
DeleteOutlined } from '@ant-design/icons'
|
2021-10-05 17:55:20 +05:00
|
|
|
import { View } from './View'
|
2021-10-06 13:57:04 +05:00
|
|
|
import LoaderPortal from '../../components/LoaderPortal'
|
2021-10-06 15:31:55 +05:00
|
|
|
import { MeasureService } from '../../services/api'
|
2021-10-05 17:55:20 +05:00
|
|
|
import '../../styles/index.css'
|
|
|
|
import '../../styles/measure.css'
|
|
|
|
|
|
|
|
const format='YYYY.MM.DD HH:mm'
|
|
|
|
|
2021-10-07 11:52:52 +05:00
|
|
|
export const MeasureTable = ({idWell, idCategory, title, columns, values, updateMeasuresFunc}) => {
|
2021-10-05 17:55:20 +05:00
|
|
|
|
2021-10-06 13:57:04 +05:00
|
|
|
const [showLoader, setShowLoader] = useState(false);
|
2021-10-07 17:23:01 +05:00
|
|
|
const [selectedTimeLineId, setSelectedTimeLineId] = useState(undefined)
|
2021-10-05 17:55:20 +05:00
|
|
|
const [displayedValues, setDisplayedValues] = useState([]);
|
|
|
|
const [editingColumns, setEditingColumns] = useState(columns);
|
|
|
|
const [isTableEditing, setIsTableEditing] = useState(false);
|
2021-10-06 13:57:04 +05:00
|
|
|
const [editingActionName, setEditingActionName] = useState('');
|
|
|
|
|
|
|
|
const [measuresForm] = Form.useForm();
|
|
|
|
|
2021-10-06 16:06:07 +05:00
|
|
|
const createEditingColumns = (cols, renderDelegate) =>
|
2021-10-07 15:05:59 +05:00
|
|
|
cols.map(col =>
|
|
|
|
({ render: renderDelegate,
|
|
|
|
...col
|
|
|
|
})
|
2021-10-06 16:06:07 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let valuesToDisplay = []
|
|
|
|
|
2021-10-07 17:23:01 +05:00
|
|
|
if(!selectedTimeLineId)
|
2021-10-07 17:23:01 +05:00
|
|
|
valuesToDisplay = values && values.length
|
2021-10-06 16:06:07 +05:00
|
|
|
? values[values.length-1]
|
|
|
|
: []
|
2021-10-07 17:23:01 +05:00
|
|
|
else
|
2021-10-06 16:06:07 +05:00
|
|
|
valuesToDisplay = values.find(el => el.id === selectedTimeLineId)
|
|
|
|
|
|
|
|
setDisplayedValues(valuesToDisplay)
|
2021-10-07 17:23:01 +05:00
|
|
|
setSelectedTimeLineId(valuesToDisplay?.id)
|
2021-10-06 16:06:07 +05:00
|
|
|
}, [selectedTimeLineId, values])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let switchableColumns = []
|
2021-10-07 10:48:26 +05:00
|
|
|
|
2021-10-07 17:23:01 +05:00
|
|
|
isTableEditing && editingActionName !== 'delete'
|
|
|
|
? switchableColumns = createEditingColumns(columns, () => <Input className='w-100 measure-input' />)
|
2021-10-06 16:06:07 +05:00
|
|
|
: switchableColumns = createEditingColumns(columns, null)
|
|
|
|
|
2021-10-07 17:23:01 +05:00
|
|
|
if(editingActionName === 'edit')
|
2021-10-07 10:48:26 +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-10-07 17:23:01 +05:00
|
|
|
}, [isTableEditing, columns, editingActionName, displayedValues?.data, measuresForm])
|
|
|
|
|
|
|
|
const markMeasuresAsDeleted = async () => {
|
|
|
|
setShowLoader(true)
|
|
|
|
await MeasureService.markAsDelete(idWell, displayedValues.id)
|
|
|
|
updateMeasuresFunc()
|
|
|
|
setShowLoader(false)
|
|
|
|
setIsTableEditing(false)
|
2021-10-06 13:57:04 +05:00
|
|
|
}
|
|
|
|
|
2021-10-07 17:23:01 +05:00
|
|
|
const checkIsDataDefault = () =>
|
|
|
|
displayedValues?.isDefaultData ? true : false
|
|
|
|
|
|
|
|
const crudButtons =
|
|
|
|
<div className='w-300px mt-8px'>
|
|
|
|
<Button
|
|
|
|
key='add'
|
|
|
|
className='w-33'
|
|
|
|
onClick={() => {
|
|
|
|
setEditingActionName('add')
|
|
|
|
setIsTableEditing(true)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<PlusOutlined />
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
key='edit'
|
|
|
|
className='w-33'
|
|
|
|
onClick={() => {
|
|
|
|
setEditingActionName('edit')
|
|
|
|
setIsTableEditing(true)
|
|
|
|
}}
|
|
|
|
disabled={checkIsDataDefault()}
|
|
|
|
>
|
|
|
|
<EditOutlined />
|
|
|
|
</Button>
|
|
|
|
<Popconfirm
|
|
|
|
title="Удалить данные?"
|
|
|
|
onConfirm={() => markMeasuresAsDeleted()}
|
|
|
|
disabled={checkIsDataDefault()}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
key='delete'
|
|
|
|
onClick={() => {
|
|
|
|
setEditingActionName('delete')
|
|
|
|
}}
|
|
|
|
disabled={checkIsDataDefault()}
|
|
|
|
>
|
|
|
|
<DeleteOutlined style={{margin:'auto 28px'}}/>
|
|
|
|
</Button>
|
|
|
|
</Popconfirm>
|
|
|
|
</div>
|
2021-10-06 13:57:04 +05:00
|
|
|
|
|
|
|
const confirmButtons =
|
2021-10-07 17:23:01 +05:00
|
|
|
<div className='w-300px mt-8px'>
|
|
|
|
<div className='d-flex'>
|
2021-10-07 15:05:59 +05:00
|
|
|
<Button
|
|
|
|
key='confirm'
|
2021-10-07 17:23:01 +05:00
|
|
|
className='w-50'
|
|
|
|
onClick={() => { measuresForm.submit() }}
|
2021-10-06 15:31:55 +05:00
|
|
|
>
|
2021-10-07 17:23:01 +05:00
|
|
|
<SaveOutlined />
|
2021-10-06 15:31:55 +05:00
|
|
|
</Button>
|
2021-10-07 15:05:59 +05:00
|
|
|
<Button
|
|
|
|
key='decline'
|
2021-10-07 17:23:01 +05:00
|
|
|
className='w-50'
|
2021-10-07 15:05:59 +05:00
|
|
|
onClick={()=> setIsTableEditing(false)}
|
|
|
|
>
|
2021-10-07 17:23:01 +05:00
|
|
|
<CloseCircleOutlined />
|
2021-10-07 15:05:59 +05:00
|
|
|
</Button>
|
2021-10-06 13:57:04 +05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
let handleSubmitMeasuresForm = async (formData) => {
|
|
|
|
measuresForm.validateFields()
|
2021-08-30 16:40:56 +05:00
|
|
|
|
2021-10-06 15:31:55 +05:00
|
|
|
const measureParams = {
|
|
|
|
idWell: idWell,
|
|
|
|
idCategory: idCategory,
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
data: formData
|
|
|
|
}
|
|
|
|
|
2021-10-06 13:57:04 +05:00
|
|
|
setShowLoader(true)
|
2021-10-07 15:05:59 +05:00
|
|
|
|
2021-10-07 17:23:01 +05:00
|
|
|
if(editingActionName === 'add') {
|
2021-10-07 15:05:59 +05:00
|
|
|
await MeasureService.insert(idWell, measureParams)
|
2021-10-07 17:23:01 +05:00
|
|
|
} else if (editingActionName === 'edit') {
|
2021-10-07 15:05:59 +05:00
|
|
|
measureParams.id = displayedValues.id
|
|
|
|
measureParams.timestamp = displayedValues.timestamp
|
|
|
|
await MeasureService.update(idWell, measureParams)
|
|
|
|
}
|
|
|
|
|
2021-10-06 15:31:55 +05:00
|
|
|
setIsTableEditing(false)
|
2021-10-07 11:52:52 +05:00
|
|
|
updateMeasuresFunc()
|
2021-10-06 13:57:04 +05:00
|
|
|
setShowLoader(false)
|
|
|
|
}
|
2021-08-28 22:32:13 +05:00
|
|
|
|
2021-10-05 17:55:20 +05:00
|
|
|
return <>
|
|
|
|
|
2021-10-07 17:23:01 +05:00
|
|
|
<h2>{title}</h2>
|
2021-08-30 16:40:56 +05:00
|
|
|
|
2021-10-05 17:55:20 +05:00
|
|
|
<div className='d-flex'>
|
|
|
|
<div className='flex-direction-column'>
|
2021-10-07 17:23:01 +05:00
|
|
|
<div className='measure-buttons-container'>
|
2021-10-05 17:55:20 +05:00
|
|
|
{isTableEditing
|
|
|
|
? confirmButtons
|
|
|
|
: crudButtons
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='measure-dates mt-20px'>
|
|
|
|
<Timeline className='mt-12px ml-10px'>
|
|
|
|
{values.map((item, index) =>
|
|
|
|
<Timeline.Item
|
2021-10-07 15:05:59 +05:00
|
|
|
key={index}
|
|
|
|
className='measure-button'
|
2021-10-06 16:06:07 +05:00
|
|
|
onClick={() => setSelectedTimeLineId(item.id)}
|
|
|
|
dot={item?.id === displayedValues?.id
|
2021-10-07 15:05:59 +05:00
|
|
|
? <CheckSquareOutlined className="timeline-clock-icon" />
|
2021-10-06 13:57:04 +05:00
|
|
|
: null}
|
2021-10-05 17:55:20 +05:00
|
|
|
>
|
2021-10-07 15:05:59 +05:00
|
|
|
<span className={item?.id === displayedValues?.id ? 'selected-timeline' : ''}>
|
2021-10-07 15:34:59 +05:00
|
|
|
{item.timestamp ? moment.utc(item.timestamp).local().format(format) : 'Нет данных'}
|
2021-10-07 15:05:59 +05:00
|
|
|
</span>
|
2021-10-05 17:55:20 +05:00
|
|
|
</Timeline.Item>
|
|
|
|
)}
|
|
|
|
</Timeline>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className='w-100'>
|
2021-10-06 13:57:04 +05:00
|
|
|
<LoaderPortal show={showLoader}>
|
|
|
|
<Form
|
|
|
|
form={measuresForm}
|
|
|
|
onFinish={handleSubmitMeasuresForm}
|
|
|
|
>
|
|
|
|
<View
|
2021-10-07 17:23:01 +05:00
|
|
|
item={displayedValues?.data ?? []}
|
2021-10-06 13:57:04 +05:00
|
|
|
columns={editingColumns}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
</LoaderPortal>
|
2021-10-05 17:55:20 +05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
2021-08-28 22:32:13 +05:00
|
|
|
}
|