2021-10-06 13:57:04 +05:00
|
|
|
import { useState, useEffect } from 'react'
|
|
|
|
import { Button, Form, Timeline } from 'antd'
|
2021-10-05 17:55:20 +05:00
|
|
|
import moment from 'moment'
|
|
|
|
import { ExclamationCircleOutlined } from '@ant-design/icons'
|
|
|
|
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-06 15:31:55 +05:00
|
|
|
export const MeasureTable = ({idWell, idCategory, title, columns, values, updateMeasuresDelegate}) => {
|
2021-10-05 17:55:20 +05:00
|
|
|
var defaultDisplay = values && values.length
|
|
|
|
? values[values.length-1]
|
|
|
|
: []
|
|
|
|
|
2021-10-06 13:57:04 +05:00
|
|
|
const [showLoader, setShowLoader] = useState(false);
|
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();
|
|
|
|
|
|
|
|
const buttonsConfig = [
|
|
|
|
{name:'Добавить', key:'add'},
|
|
|
|
{name:'Редактировать', key:'edit'},
|
|
|
|
{name:'Удалить', key:'delete'}
|
|
|
|
]
|
|
|
|
|
|
|
|
const createButtons = (config, onClickDelegate) => {
|
|
|
|
return (
|
|
|
|
<div className='w-300px mt-12px'>
|
|
|
|
{config.map(c => {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={c.key}
|
|
|
|
className='w-100'
|
|
|
|
onClick={onClickDelegate}
|
|
|
|
>
|
|
|
|
{c.name}
|
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const crudButtons = createButtons(buttonsConfig, (e)=> {
|
|
|
|
setEditingActionName(e.target.innerText)
|
|
|
|
setIsTableEditing(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
const confirmButtons =
|
|
|
|
<div className='w-300px'>
|
|
|
|
<h3 style={{textAlign: 'center'}}>{editingActionName} данные?</h3>
|
|
|
|
<div className='d-flex mt-20px'>
|
2021-10-06 15:31:55 +05:00
|
|
|
<Button key='confirm' className='w-100' onClick={async () => {
|
|
|
|
if(editingActionName === 'Удалить'){
|
|
|
|
setShowLoader(true)
|
|
|
|
await MeasureService.markAsDelete(idWell, displayedValues.id)
|
|
|
|
updateMeasuresDelegate()
|
|
|
|
setShowLoader(false)
|
|
|
|
setIsTableEditing(false)
|
|
|
|
} else {
|
|
|
|
measuresForm.submit()
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Да
|
|
|
|
</Button>
|
2021-10-06 13:57:04 +05:00
|
|
|
<Button key='decline' className='w-100' onClick={()=> setIsTableEditing(false)}>Нет</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
const createEditingColumns = (cols, renderDelegate) =>
|
|
|
|
cols.map(col =>
|
|
|
|
({ render: renderDelegate,
|
|
|
|
...col
|
|
|
|
})
|
|
|
|
)
|
2021-10-05 17:55:20 +05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setDisplayedValues(defaultDisplay)
|
|
|
|
}, [defaultDisplay])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let switchableColumns = []
|
2021-10-06 13:57:04 +05:00
|
|
|
isTableEditing && editingActionName !== 'Удалить'
|
|
|
|
? switchableColumns = createEditingColumns(columns, () => <input className='w-100'></input>)
|
|
|
|
: switchableColumns = createEditingColumns(columns, null)
|
2021-10-05 17:55:20 +05:00
|
|
|
|
|
|
|
setEditingColumns(switchableColumns)
|
2021-10-06 15:31:55 +05:00
|
|
|
}, [isTableEditing, columns, editingActionName])
|
2021-10-05 17:55:20 +05:00
|
|
|
|
2021-10-06 13:57:04 +05:00
|
|
|
|
|
|
|
let handleSubmitMeasuresForm = async (formData) => {
|
|
|
|
// if(isTableEditing)
|
|
|
|
// measuresForm.setFieldsValue({
|
|
|
|
// ...displayedValues
|
|
|
|
// });
|
|
|
|
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-06 15:31:55 +05:00
|
|
|
await MeasureService.insert(idWell, measureParams)
|
|
|
|
setIsTableEditing(false)
|
|
|
|
updateMeasuresDelegate()
|
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-08-28 22:32:13 +05:00
|
|
|
<h3>{title}</h3>
|
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'>
|
|
|
|
<div className='d-flex button-container'>
|
|
|
|
{isTableEditing
|
|
|
|
? confirmButtons
|
|
|
|
: crudButtons
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='measure-dates mt-20px'>
|
|
|
|
<Timeline className='mt-12px ml-10px'>
|
|
|
|
{values.map((item, index) =>
|
|
|
|
<Timeline.Item
|
|
|
|
key={item.id}
|
|
|
|
className={index === values.length-1
|
|
|
|
? 'last-measure-button'
|
|
|
|
: 'measure-button'}
|
|
|
|
onClick={() => setDisplayedValues(values.find(el => el.id === item.id))}
|
|
|
|
dot={item.id === displayedValues.id
|
|
|
|
? <ExclamationCircleOutlined className="timeline-clock-icon" />
|
2021-10-06 13:57:04 +05:00
|
|
|
: null}
|
2021-10-05 17:55:20 +05:00
|
|
|
>
|
|
|
|
{moment.utc(item.timestamp).local().format(format)}
|
|
|
|
</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
|
|
|
|
item={displayedValues.data}
|
|
|
|
columns={editingColumns}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
</LoaderPortal>
|
2021-10-05 17:55:20 +05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
2021-08-28 22:32:13 +05:00
|
|
|
}
|