2021-12-13 10:28:10 +05:00
|
|
|
|
import { Form, Table, Button, Popconfirm } from 'antd'
|
2021-08-02 11:09:55 +05:00
|
|
|
|
import { EditOutlined, SaveOutlined, PlusOutlined, CloseCircleOutlined, DeleteOutlined } from '@ant-design/icons'
|
2021-12-13 10:28:10 +05:00
|
|
|
|
import { useState, useEffect } from 'react'
|
2021-08-20 10:49:20 +05:00
|
|
|
|
import { EditableCell } from './EditableCell'
|
2021-12-13 10:28:10 +05:00
|
|
|
|
import { invokeWebApiWrapperAsync } from '../factory'
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const newRowKeyValue = 'newRow'
|
|
|
|
|
|
2021-12-27 15:18:20 +05:00
|
|
|
|
export const makeActionHandler = (action, { idWell, service, setLoader, errorMsg, onComplete }, recordParser) => service && action && (
|
2021-12-13 10:28:10 +05:00
|
|
|
|
(record) => invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
2021-12-27 15:18:20 +05:00
|
|
|
|
const addIdWell = (...params) => idWell ? [idWell, ...params] : params
|
|
|
|
|
if (typeof recordParser === 'function')
|
|
|
|
|
record = recordParser(record)
|
|
|
|
|
|
2021-12-13 10:28:10 +05:00
|
|
|
|
if (action === 'insert') {
|
|
|
|
|
record.key = Date.now()
|
2021-12-27 15:18:20 +05:00
|
|
|
|
await service.insert(...addIdWell(record))
|
|
|
|
|
} else if (action === 'insertRange') {
|
|
|
|
|
if (!Array.isArray(record))
|
|
|
|
|
record = [record]
|
|
|
|
|
await service.insertRange(...addIdWell(record))
|
2021-12-13 10:28:10 +05:00
|
|
|
|
} else if (record.id) {
|
2021-12-07 19:36:04 +05:00
|
|
|
|
if (action === 'update')
|
2021-12-27 15:18:20 +05:00
|
|
|
|
await service.put(...addIdWell(record.id, record))
|
2021-12-07 19:36:04 +05:00
|
|
|
|
else if (action === 'delete')
|
2021-12-27 15:18:20 +05:00
|
|
|
|
await service.delete(...addIdWell(record.id))
|
2021-12-13 10:28:10 +05:00
|
|
|
|
}
|
2021-12-21 20:35:45 +05:00
|
|
|
|
if (onComplete)
|
|
|
|
|
await onComplete()
|
2021-12-13 10:28:10 +05:00
|
|
|
|
},
|
|
|
|
|
setLoader,
|
|
|
|
|
errorMsg
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-12-07 19:36:04 +05:00
|
|
|
|
|
2021-10-15 16:03:09 +05:00
|
|
|
|
export const tryAddKeys = (items) => {
|
2021-12-13 10:28:10 +05:00
|
|
|
|
if (!items?.length || !items[0])
|
2021-08-19 11:40:26 +05:00
|
|
|
|
return []
|
2021-12-13 10:28:10 +05:00
|
|
|
|
if (items[0].key)
|
2021-08-19 11:40:26 +05:00
|
|
|
|
return items
|
|
|
|
|
return items.map((item, index) => ({...item, key: item.key ?? item.id ?? index }))
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
export const EditableTable = ({
|
2021-11-29 16:49:22 +05:00
|
|
|
|
columns,
|
|
|
|
|
dataSource,
|
|
|
|
|
onChange, // Метод вызывается со всем dataSource с измененными элементами после любого действия
|
|
|
|
|
onRowAdd, // Метод вызывается с новой добавленной записью. Если метод не определен, то кнопка добавления строки не показывается
|
2021-12-15 11:01:18 +05:00
|
|
|
|
onRowEdit, // Метод вызывается с новой отредактированной записью. Если метод не поределен, то кнопка редактирования строки не показывается
|
|
|
|
|
onRowDelete, // Метод вызывается с удаленной записью. Если метод не поределен, то кнопка удаления строки не показывается
|
2021-11-29 16:49:22 +05:00
|
|
|
|
...otherTableProps
|
|
|
|
|
}) => {
|
2021-08-02 11:09:55 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const [form] = Form.useForm()
|
2021-08-19 11:40:26 +05:00
|
|
|
|
const [data, setData] = useState(tryAddKeys(dataSource))
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const [editingKey, setEditingKey] = useState('')
|
|
|
|
|
|
2021-12-13 10:28:10 +05:00
|
|
|
|
useEffect(() => {
|
2021-08-19 11:40:26 +05:00
|
|
|
|
setData(tryAddKeys(dataSource))
|
2021-12-13 10:28:10 +05:00
|
|
|
|
}, [dataSource])
|
2021-08-11 11:44:20 +05:00
|
|
|
|
|
2021-08-30 15:11:21 +05:00
|
|
|
|
const isEditing = (record) => record?.key === editingKey
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
|
|
|
|
const edit = (record) => {
|
|
|
|
|
form.setFieldsValue({...record})
|
|
|
|
|
setEditingKey(record.key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cancel = () => {
|
2021-12-13 10:28:10 +05:00
|
|
|
|
if (editingKey === newRowKeyValue) {
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const newData = [...data]
|
|
|
|
|
const index = newData.findIndex((item) => newRowKeyValue === item.key)
|
|
|
|
|
newData.splice(index, 1)
|
|
|
|
|
setData(newData)
|
|
|
|
|
}
|
2021-07-30 16:14:07 +05:00
|
|
|
|
setEditingKey('')
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 15:41:11 +05:00
|
|
|
|
const addNewRow = async () => {
|
2021-08-02 11:09:55 +05:00
|
|
|
|
let newRow = {
|
2021-08-12 15:41:11 +05:00
|
|
|
|
...form.initialValues,
|
2021-08-02 11:09:55 +05:00
|
|
|
|
key:newRowKeyValue
|
|
|
|
|
}
|
2021-12-13 10:28:10 +05:00
|
|
|
|
|
2021-08-12 17:47:16 +05:00
|
|
|
|
const newData = [newRow, ...data]
|
2021-08-02 11:09:55 +05:00
|
|
|
|
setData(newData)
|
|
|
|
|
edit(newRow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const save = async (record) => {
|
2021-07-30 16:14:07 +05:00
|
|
|
|
try {
|
|
|
|
|
const row = await form.validateFields()
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const newData = [...data]
|
|
|
|
|
const index = newData.findIndex((item) => record.key === item.key)
|
2021-08-12 15:41:11 +05:00
|
|
|
|
const item = newData[index]
|
|
|
|
|
const newItem = { ...item, ...row }
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2021-08-12 15:41:11 +05:00
|
|
|
|
newData.splice(index, 1, newItem)
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2021-12-13 10:28:10 +05:00
|
|
|
|
if (item.key === newRowKeyValue)
|
2021-08-02 11:09:55 +05:00
|
|
|
|
item.key = newRowKeyValue + newData.length
|
2021-11-29 16:49:22 +05:00
|
|
|
|
|
2021-09-30 18:00:46 +05:00
|
|
|
|
const isAdding = editingKey === newRowKeyValue
|
2021-07-30 16:14:07 +05:00
|
|
|
|
setEditingKey('')
|
2021-08-02 11:09:55 +05:00
|
|
|
|
setData(newData)
|
|
|
|
|
|
2021-09-30 18:00:46 +05:00
|
|
|
|
if (isAdding)
|
2021-12-13 10:28:10 +05:00
|
|
|
|
try {
|
2021-11-29 16:49:22 +05:00
|
|
|
|
onRowAdd(newItem)
|
2021-12-13 10:28:10 +05:00
|
|
|
|
} catch (err) {
|
2021-09-30 18:00:46 +05:00
|
|
|
|
console.log('callback onRowAdd fault:', err)
|
|
|
|
|
}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
else
|
2021-12-13 10:28:10 +05:00
|
|
|
|
try {
|
2021-11-29 16:49:22 +05:00
|
|
|
|
onRowEdit(newItem)
|
2021-12-13 10:28:10 +05:00
|
|
|
|
} catch (err) {
|
2021-09-30 18:00:46 +05:00
|
|
|
|
console.log('callback onRowEdit fault:', err)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 10:28:10 +05:00
|
|
|
|
try {
|
|
|
|
|
if (onChange)
|
2021-09-30 18:00:46 +05:00
|
|
|
|
onChange(newData)
|
2021-12-13 10:28:10 +05:00
|
|
|
|
} catch (err) {
|
2021-09-30 18:00:46 +05:00
|
|
|
|
console.log('callback onChange fault:', err)
|
|
|
|
|
}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
} catch (errInfo) {
|
|
|
|
|
console.log('Validate Failed:', errInfo)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const deleteRow = (record) =>{
|
|
|
|
|
const newData = [...data]
|
|
|
|
|
const index = newData.findIndex((item) => record.key === item.key)
|
2021-11-29 16:49:22 +05:00
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
newData.splice(index, 1)
|
|
|
|
|
setData(newData)
|
2021-11-29 16:49:22 +05:00
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
onRowDelete(record)
|
|
|
|
|
|
2021-12-13 10:28:10 +05:00
|
|
|
|
if (onChange)
|
2021-08-02 11:09:55 +05:00
|
|
|
|
onChange(newData)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const operationColumn = {
|
2021-08-20 09:20:33 +05:00
|
|
|
|
width: 82,
|
2021-12-13 10:28:10 +05:00
|
|
|
|
title: !!onRowAdd && (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={addNewRow}
|
|
|
|
|
disabled={editingKey !== ''}
|
|
|
|
|
icon={<PlusOutlined/>}
|
|
|
|
|
/>
|
|
|
|
|
),
|
2021-07-30 16:14:07 +05:00
|
|
|
|
dataIndex: 'operation',
|
2021-12-13 10:28:10 +05:00
|
|
|
|
render: (_, record) => isEditing(record) ? (
|
|
|
|
|
<span>
|
|
|
|
|
<Button onClick={() => save(record)} icon={<SaveOutlined/>}/>
|
|
|
|
|
<Button onClick={cancel} icon={<CloseCircleOutlined/>}/>
|
|
|
|
|
</span>
|
|
|
|
|
) : (
|
|
|
|
|
<span>
|
|
|
|
|
{onRowEdit && (
|
2021-11-29 16:49:22 +05:00
|
|
|
|
<Button
|
|
|
|
|
disabled={editingKey !== ''}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
onClick={() => edit(record)}
|
2021-12-13 10:28:10 +05:00
|
|
|
|
icon={<EditOutlined/>}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{onRowDelete && (
|
|
|
|
|
<Popconfirm title={'Удалить?'} onConfirm={() => deleteRow(record)}>
|
2021-08-02 11:09:55 +05:00
|
|
|
|
<Button icon={<DeleteOutlined/>}/>
|
2021-12-13 10:28:10 +05:00
|
|
|
|
</Popconfirm>
|
|
|
|
|
)}
|
|
|
|
|
</span>
|
|
|
|
|
),
|
2021-07-30 16:14:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleColumn = (col) => {
|
|
|
|
|
if (col.children)
|
|
|
|
|
col.children = col.children.map(handleColumn)
|
|
|
|
|
|
2021-11-29 16:49:22 +05:00
|
|
|
|
if (!col.editable)
|
|
|
|
|
return col
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...col,
|
|
|
|
|
onCell: (record) => ({
|
2021-11-29 16:49:22 +05:00
|
|
|
|
editing: isEditing(record),
|
|
|
|
|
record,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
dataIndex: col.dataIndex ?? col.key,
|
2021-08-19 11:40:26 +05:00
|
|
|
|
key: col.key ?? col.dataIndex,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
input: col.input,
|
|
|
|
|
isRequired: col.isRequired,
|
|
|
|
|
title: col.title,
|
|
|
|
|
dataType: col.dataType,
|
|
|
|
|
formItemClass: col.formItemClass,
|
|
|
|
|
formItemRules: col.formItemRules,
|
2021-08-12 15:41:11 +05:00
|
|
|
|
initialValue: col.initialValue,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mergedColumns = [...columns.map(handleColumn), operationColumn]
|
2021-11-29 16:49:22 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
return (
|
|
|
|
|
<Form form={form} component={false}>
|
|
|
|
|
<Table
|
|
|
|
|
components={{
|
|
|
|
|
body: {
|
|
|
|
|
cell: EditableCell,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
columns={mergedColumns}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
dataSource={data}
|
2021-07-30 16:14:07 +05:00
|
|
|
|
{...otherTableProps}
|
|
|
|
|
/>
|
|
|
|
|
</Form>
|
|
|
|
|
)
|
2021-11-29 16:49:22 +05:00
|
|
|
|
}
|