import { useState, useEffect } from 'react' import { Button, Form, Timeline } from 'antd' import moment from 'moment' import { ExclamationCircleOutlined } 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, updateMeasuresDelegate}) => { var defaultDisplay = values && values.length ? values[values.length-1] : [] const [showLoader, setShowLoader] = useState(false); const [displayedValues, setDisplayedValues] = useState([]); const [editingColumns, setEditingColumns] = useState(columns); const [isTableEditing, setIsTableEditing] = useState(false); const [editingActionName, setEditingActionName] = useState(''); const [measuresForm] = Form.useForm(); const buttonsConfig = [ {name:'Добавить', key:'add'}, {name:'Редактировать', key:'edit'}, {name:'Удалить', key:'delete'} ] const createButtons = (config, onClickDelegate) => { return (
{config.map(c => { return ( ) })}
) } const crudButtons = createButtons(buttonsConfig, (e)=> { setEditingActionName(e.target.innerText) setIsTableEditing(true) }) const confirmButtons =

{editingActionName} данные?

const createEditingColumns = (cols, renderDelegate) => cols.map(col => ({ render: renderDelegate, ...col }) ) useEffect(() => { setDisplayedValues(defaultDisplay) }, [defaultDisplay]) useEffect(() => { let switchableColumns = [] isTableEditing && editingActionName !== 'Удалить' ? switchableColumns = createEditingColumns(columns, () => ) : switchableColumns = createEditingColumns(columns, null) setEditingColumns(switchableColumns) }, [isTableEditing, columns, editingActionName]) let handleSubmitMeasuresForm = async (formData) => { // if(isTableEditing) // measuresForm.setFieldsValue({ // ...displayedValues // }); measuresForm.validateFields() const measureParams = { idWell: idWell, idCategory: idCategory, timestamp: new Date().toISOString(), data: formData } setShowLoader(true) await MeasureService.insert(idWell, measureParams) setIsTableEditing(false) updateMeasuresDelegate() setShowLoader(false) } return <>  

{title}

 
{isTableEditing ? confirmButtons : crudButtons }
{values.map((item, index) => setDisplayedValues(values.find(el => el.id === item.id))} dot={item.id === displayedValues.id ? : null} > {moment.utc(item.timestamp).local().format(format)} )}
}