index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { DownOutlined, PlusOutlined } from '@ant-design/icons';
  2. import { Button, Divider, Dropdown, Menu, message } from 'antd';
  3. import React, { useState, useRef } from 'react';
  4. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  5. import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table';
  6. import { SorterResult } from 'antd/es/table/interface';
  7. import CreateForm from './components/CreateForm';
  8. import UpdateForm, { FormValueType } from './components/UpdateForm';
  9. import { TableListItem } from './data.d';
  10. import { queryRule, updateRule, addRule, removeRule } from './service';
  11. /**
  12. * 添加节点
  13. * @param fields
  14. */
  15. const handleAdd = async (fields: TableListItem) => {
  16. const hide = message.loading('正在添加');
  17. try {
  18. await addRule({ ...fields });
  19. hide();
  20. message.success('添加成功');
  21. return true;
  22. } catch (error) {
  23. hide();
  24. message.error('添加失败请重试!');
  25. return false;
  26. }
  27. };
  28. /**
  29. * 更新节点
  30. * @param fields
  31. */
  32. const handleUpdate = async (fields: FormValueType) => {
  33. const hide = message.loading('正在配置');
  34. try {
  35. await updateRule({
  36. name: fields.name,
  37. desc: fields.desc,
  38. key: fields.key,
  39. });
  40. hide();
  41. message.success('配置成功');
  42. return true;
  43. } catch (error) {
  44. hide();
  45. message.error('配置失败请重试!');
  46. return false;
  47. }
  48. };
  49. /**
  50. * 删除节点
  51. * @param selectedRows
  52. */
  53. const handleRemove = async (selectedRows: TableListItem[]) => {
  54. const hide = message.loading('正在删除');
  55. if (!selectedRows) return true;
  56. try {
  57. await removeRule({
  58. key: selectedRows.map((row) => row.key),
  59. });
  60. hide();
  61. message.success('删除成功,即将刷新');
  62. return true;
  63. } catch (error) {
  64. hide();
  65. message.error('删除失败,请重试');
  66. return false;
  67. }
  68. };
  69. const TableList: React.FC<{}> = () => {
  70. const [sorter, setSorter] = useState<string>('');
  71. const [createModalVisible, handleModalVisible] = useState<boolean>(false);
  72. const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
  73. const [stepFormValues, setStepFormValues] = useState({});
  74. const actionRef = useRef<ActionType>();
  75. const columns: ProColumns<TableListItem>[] = [
  76. {
  77. title: '规则名称',
  78. dataIndex: 'name',
  79. rules: [
  80. {
  81. required: true,
  82. message: '规则名称为必填项',
  83. },
  84. ],
  85. },
  86. {
  87. title: '描述',
  88. dataIndex: 'desc',
  89. valueType: 'textarea',
  90. },
  91. {
  92. title: '服务调用次数',
  93. dataIndex: 'callNo',
  94. sorter: true,
  95. hideInForm: true,
  96. renderText: (val: string) => `${val} 万`,
  97. },
  98. {
  99. title: '状态',
  100. dataIndex: 'status',
  101. hideInForm: true,
  102. valueEnum: {
  103. 0: { text: '关闭', status: 'Default' },
  104. 1: { text: '运行中', status: 'Processing' },
  105. 2: { text: '已上线', status: 'Success' },
  106. 3: { text: '异常', status: 'Error' },
  107. },
  108. },
  109. {
  110. title: '上次调度时间',
  111. dataIndex: 'updatedAt',
  112. sorter: true,
  113. valueType: 'dateTime',
  114. hideInForm: true,
  115. },
  116. {
  117. title: '操作',
  118. dataIndex: 'option',
  119. valueType: 'option',
  120. render: (_, record) => (
  121. <>
  122. <a
  123. onClick={() => {
  124. handleUpdateModalVisible(true);
  125. setStepFormValues(record);
  126. }}
  127. >
  128. 配置
  129. </a>
  130. <Divider type="vertical" />
  131. <a href="">订阅警报</a>
  132. </>
  133. ),
  134. },
  135. ];
  136. return (
  137. <PageHeaderWrapper>
  138. <ProTable<TableListItem>
  139. headerTitle="查询表格"
  140. actionRef={actionRef}
  141. rowKey="key"
  142. onChange={(_, _filter, _sorter) => {
  143. const sorterResult = _sorter as SorterResult<TableListItem>;
  144. if (sorterResult.field) {
  145. setSorter(`${sorterResult.field}_${sorterResult.order}`);
  146. }
  147. }}
  148. params={{
  149. sorter,
  150. }}
  151. toolBarRender={(action, { selectedRows }) => [
  152. <Button type="primary" onClick={() => handleModalVisible(true)}>
  153. <PlusOutlined /> 新建
  154. </Button>,
  155. selectedRows && selectedRows.length > 0 && (
  156. <Dropdown
  157. overlay={
  158. <Menu
  159. onClick={async (e) => {
  160. if (e.key === 'remove') {
  161. await handleRemove(selectedRows);
  162. action.reload();
  163. }
  164. }}
  165. selectedKeys={[]}
  166. >
  167. <Menu.Item key="remove">批量删除</Menu.Item>
  168. <Menu.Item key="approval">批量审批</Menu.Item>
  169. </Menu>
  170. }
  171. >
  172. <Button>
  173. 批量操作 <DownOutlined />
  174. </Button>
  175. </Dropdown>
  176. ),
  177. ]}
  178. request={(params) => queryRule(params)}
  179. columns={columns}
  180. rowSelection={{}}
  181. />
  182. <CreateForm onCancel={() => handleModalVisible(false)} modalVisible={createModalVisible}>
  183. <ProTable<TableListItem, TableListItem>
  184. onSubmit={async (value) => {
  185. const success = await handleAdd(value);
  186. if (success) {
  187. handleModalVisible(false);
  188. if (actionRef.current) {
  189. actionRef.current.reload();
  190. }
  191. }
  192. }}
  193. rowKey="key"
  194. type="form"
  195. columns={columns}
  196. rowSelection={{}}
  197. />
  198. </CreateForm>
  199. {stepFormValues && Object.keys(stepFormValues).length ? (
  200. <UpdateForm
  201. onSubmit={async (value) => {
  202. const success = await handleUpdate(value);
  203. if (success) {
  204. handleUpdateModalVisible(false);
  205. setStepFormValues({});
  206. if (actionRef.current) {
  207. actionRef.current.reload();
  208. }
  209. }
  210. }}
  211. onCancel={() => {
  212. handleUpdateModalVisible(false);
  213. setStepFormValues({});
  214. }}
  215. updateModalVisible={updateModalVisible}
  216. values={stepFormValues}
  217. />
  218. ) : null}
  219. </PageHeaderWrapper>
  220. );
  221. };
  222. export default TableList;