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