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 '../../styles/index.css'
import '../../styles/measure.css'
const format='YYYY.MM.DD HH:mm'
export const MeasureTable = ({title, columns, values}) => {
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])
let handleSubmitMeasuresForm = async (formData) => {
// if(isTableEditing)
// measuresForm.setFieldsValue({
// ...displayedValues
// });
measuresForm.validateFields()
setShowLoader(true)
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)}
)}
>
}