import { addRule, removeRule, rule, updateRule } from '@/services/ant-design-pro/api'; import { PlusOutlined } from '@ant-design/icons'; import type { ActionType, ProColumns, ProDescriptionsItemProps } from '@ant-design/pro-components'; import { FooterToolbar, ModalForm, PageContainer, ProDescriptions, ProFormText, ProFormTextArea, ProTable, } from '@ant-design/pro-components'; import { Button, Drawer, Input, message } from 'antd'; import React, { useRef, useState } from 'react'; import { FormattedMessage, useIntl } from 'umi'; import type { FormValueType } from './components/UpdateForm'; import UpdateForm from './components/UpdateForm'; /** * @en-US Add node * @zh-CN 添加节点 * @param fields */ const handleAdd = async (fields: API.RuleListItem) => { const hide = message.loading('正在添加'); try { await addRule({ ...fields }); hide(); message.success('Added successfully'); return true; } catch (error) { hide(); message.error('Adding failed, please try again!'); return false; } }; /** * @en-US Update node * @zh-CN 更新节点 * * @param fields */ const handleUpdate = async (fields: FormValueType) => { const hide = message.loading('Configuring'); try { await updateRule({ name: fields.name, desc: fields.desc, key: fields.key, }); hide(); message.success('Configuration is successful'); return true; } catch (error) { hide(); message.error('Configuration failed, please try again!'); return false; } }; /** * Delete node * @zh-CN 删除节点 * * @param selectedRows */ const handleRemove = async (selectedRows: API.RuleListItem[]) => { const hide = message.loading('正在删除'); if (!selectedRows) return true; try { await removeRule({ key: selectedRows.map((row) => row.key), }); hide(); message.success('Deleted successfully and will refresh soon'); return true; } catch (error) { hide(); message.error('Delete failed, please try again'); return false; } }; const TableList: React.FC = () => { /** * @en-US Pop-up window of new window * @zh-CN 新建窗口的弹窗 * */ const [createModalVisible, handleModalVisible] = useState(false); /** * @en-US The pop-up window of the distribution update window * @zh-CN 分布更新窗口的弹窗 * */ const [updateModalVisible, handleUpdateModalVisible] = useState(false); const [showDetail, setShowDetail] = useState(false); const actionRef = useRef(); const [currentRow, setCurrentRow] = useState(); const [selectedRowsState, setSelectedRows] = useState([]); /** * @en-US International configuration * @zh-CN 国际化配置 * */ const intl = useIntl(); const columns: ProColumns[] = [ { title: ( ), dataIndex: 'name', tip: 'The rule name is the unique key', render: (dom, entity) => { return ( { setCurrentRow(entity); setShowDetail(true); }} > {dom} ); }, }, { title: , dataIndex: 'desc', valueType: 'textarea', }, { title: ( ), dataIndex: 'callNo', sorter: true, hideInForm: true, renderText: (val: string) => `${val}${intl.formatMessage({ id: 'pages.searchTable.tenThousand', defaultMessage: ' 万 ', })}`, }, { title: , dataIndex: 'status', hideInForm: true, valueEnum: { 0: { text: ( ), status: 'Default', }, 1: { text: ( ), status: 'Processing', }, 2: { text: ( ), status: 'Success', }, 3: { text: ( ), status: 'Error', }, }, }, { title: ( ), sorter: true, dataIndex: 'updatedAt', valueType: 'dateTime', renderFormItem: (item, { defaultRender, ...rest }, form) => { const status = form.getFieldValue('status'); if (`${status}` === '0') { return false; } if (`${status}` === '3') { return ( ); } return defaultRender(item); }, }, { title: , dataIndex: 'option', valueType: 'option', render: (_, record) => [ { handleUpdateModalVisible(true); setCurrentRow(record); }} > , , ], }, ]; return ( headerTitle={intl.formatMessage({ id: 'pages.searchTable.title', defaultMessage: 'Enquiry form', })} actionRef={actionRef} rowKey="key" search={{ labelWidth: 120, }} toolBarRender={() => [ , ]} request={rule} columns={columns} rowSelection={{ onChange: (_, selectedRows) => { setSelectedRows(selectedRows); }, }} /> {selectedRowsState?.length > 0 && ( {' '} {selectedRowsState.length}{' '}    {' '} {selectedRowsState.reduce((pre, item) => pre + item.callNo!, 0)}{' '} } > )} { const success = await handleAdd(value as API.RuleListItem); if (success) { handleModalVisible(false); if (actionRef.current) { actionRef.current.reload(); } } }} > ), }, ]} width="md" name="name" /> { const success = await handleUpdate(value); if (success) { handleUpdateModalVisible(false); setCurrentRow(undefined); if (actionRef.current) { actionRef.current.reload(); } } }} onCancel={() => { handleUpdateModalVisible(false); if (!showDetail) { setCurrentRow(undefined); } }} updateModalVisible={updateModalVisible} values={currentRow || {}} /> { setCurrentRow(undefined); setShowDetail(false); }} closable={false} > {currentRow?.name && ( column={2} title={currentRow?.name} request={async () => ({ data: currentRow || {}, })} params={{ id: currentRow?.name, }} columns={columns as ProDescriptionsItemProps[]} /> )} ); }; export default TableList;