import { useState, useEffect } from 'react' import { Button, Form, Input, Timeline } from 'antd' import moment from 'moment' import { CheckSquareOutlined } from '@ant-design/icons' import { View } from './View' import LoaderPortal from '../../components/LoaderPortal' import { MeasureService } from '../../services/api' import '../../styles/index.css' import '../../styles/measure.css' const format='YYYY.MM.DD HH:mm' export const MeasureTable = ({idWell, idCategory, title, columns, values, updateMeasuresFunc}) => { const [showLoader, setShowLoader] = useState(false); const [selectedTimeLineId, setSelectedTimeLineId] = useState(0) const [displayedValues, setDisplayedValues] = useState([]); const [editingColumns, setEditingColumns] = useState(columns); const [isTableEditing, setIsTableEditing] = useState(false); const [editingActionName, setEditingActionName] = useState(''); const [measuresForm] = Form.useForm(); const createEditingColumns = (cols, renderDelegate) => cols.map(col => ({ render: renderDelegate, ...col }) ) useEffect(() => { let valuesToDisplay = [] if(selectedTimeLineId === 0) { valuesToDisplay = values.length ? values[values.length-1] : [] } else { valuesToDisplay = values.find(el => el.id === selectedTimeLineId) } setDisplayedValues(valuesToDisplay) }, [selectedTimeLineId, values]) useEffect(() => { let switchableColumns = [] isTableEditing && editingActionName !== 'Удалить' ? switchableColumns = createEditingColumns(columns, () => ) : switchableColumns = createEditingColumns(columns, null) if(editingActionName === 'Редактировать') measuresForm.setFieldsValue(displayedValues.data); else if(editingActionName === 'Добавить') measuresForm.resetFields() setEditingColumns(switchableColumns) }, [isTableEditing, columns, editingActionName, displayedValues.data, measuresForm]) const buttonsConfig = [ {name:'Добавить', key:'add'}, {name:'Редактировать', key:'edit'}, {name:'Удалить', key:'delete'} ] const createButtons = (config, onClickDelegate) => { return (
{config.map(conf => { return ( ) })}
) } const crudButtons = createButtons(buttonsConfig, (e) => { setEditingActionName(e.target.innerText) setIsTableEditing(true) }) const confirmButtons =

{editingActionName} данные?

let handleSubmitMeasuresForm = async (formData) => { measuresForm.validateFields() const measureParams = { idWell: idWell, idCategory: idCategory, timestamp: new Date().toISOString(), data: formData } setShowLoader(true) if(editingActionName === 'Добавить') { await MeasureService.insert(idWell, measureParams) } else if (editingActionName === 'Редактировать') { measureParams.id = displayedValues.id measureParams.timestamp = displayedValues.timestamp await MeasureService.update(idWell, measureParams) } setIsTableEditing(false) updateMeasuresFunc() setShowLoader(false) } return <>  

{title}

 
{isTableEditing ? confirmButtons : crudButtons }
{values.map((item, index) => setSelectedTimeLineId(item.id)} dot={item?.id === displayedValues?.id ? : null} > {item.timestamp ? moment.utc(item.timestamp).local().format(format) : 'Нет данных'} )}
}