index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { DownOutlined, PlusOutlined } from '@ant-design/icons';
  2. import { Button, Divider, Dropdown, Menu, message, Input } 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. renderFormItem: (item, { defaultRender, ...rest }, form) => {
  116. const status = form.getFieldValue('status');
  117. if (`${status}` === '0') {
  118. return false;
  119. }
  120. if (`${status}` === '3') {
  121. return <Input {...rest} placeholder="请输入异常原因!" />;
  122. }
  123. return defaultRender(item);
  124. },
  125. },
  126. {
  127. title: '操作',
  128. dataIndex: 'option',
  129. valueType: 'option',
  130. render: (_, record) => (
  131. <>
  132. <a
  133. onClick={() => {
  134. handleUpdateModalVisible(true);
  135. setStepFormValues(record);
  136. }}
  137. >
  138. 配置
  139. </a>
  140. <Divider type="vertical" />
  141. <a href="">订阅警报</a>
  142. </>
  143. ),
  144. },
  145. ];
  146. return (
  147. <PageHeaderWrapper>
  148. <ProTable<TableListItem>
  149. headerTitle="查询表格"
  150. actionRef={actionRef}
  151. rowKey="key"
  152. onChange={(_, _filter, _sorter) => {
  153. const sorterResult = _sorter as SorterResult<TableListItem>;
  154. if (sorterResult.field) {
  155. setSorter(`${sorterResult.field}_${sorterResult.order}`);
  156. }
  157. }}
  158. params={{
  159. sorter,
  160. }}
  161. toolBarRender={(action, { selectedRows }) => [
  162. <Button type="primary" onClick={() => handleModalVisible(true)}>
  163. <PlusOutlined /> 新建
  164. </Button>,
  165. selectedRows && selectedRows.length > 0 && (
  166. <Dropdown
  167. overlay={
  168. <Menu
  169. onClick={async (e) => {
  170. if (e.key === 'remove') {
  171. await handleRemove(selectedRows);
  172. action.reload();
  173. }
  174. }}
  175. selectedKeys={[]}
  176. >
  177. <Menu.Item key="remove">批量删除</Menu.Item>
  178. <Menu.Item key="approval">批量审批</Menu.Item>
  179. </Menu>
  180. }
  181. >
  182. <Button>
  183. 批量操作 <DownOutlined />
  184. </Button>
  185. </Dropdown>
  186. ),
  187. ]}
  188. tableAlertRender={({ selectedRowKeys, selectedRows }) => (
  189. <div>
  190. 已选择 <a style={{ fontWeight: 600 }}>{selectedRowKeys.length}</a> 项&nbsp;&nbsp;
  191. <span>
  192. 服务调用次数总计 {selectedRows.reduce((pre, item) => pre + item.callNo, 0)} 万
  193. </span>
  194. </div>
  195. )}
  196. request={(params) => queryRule(params)}
  197. columns={columns}
  198. rowSelection={{}}
  199. />
  200. <CreateForm onCancel={() => handleModalVisible(false)} modalVisible={createModalVisible}>
  201. <ProTable<TableListItem, TableListItem>
  202. onSubmit={async (value) => {
  203. const success = await handleAdd(value);
  204. if (success) {
  205. handleModalVisible(false);
  206. if (actionRef.current) {
  207. actionRef.current.reload();
  208. }
  209. }
  210. }}
  211. rowKey="key"
  212. type="form"
  213. columns={columns}
  214. rowSelection={{}}
  215. />
  216. </CreateForm>
  217. {stepFormValues && Object.keys(stepFormValues).length ? (
  218. <UpdateForm
  219. onSubmit={async (value) => {
  220. const success = await handleUpdate(value);
  221. if (success) {
  222. handleUpdateModalVisible(false);
  223. setStepFormValues({});
  224. if (actionRef.current) {
  225. actionRef.current.reload();
  226. }
  227. }
  228. }}
  229. onCancel={() => {
  230. handleUpdateModalVisible(false);
  231. setStepFormValues({});
  232. }}
  233. updateModalVisible={updateModalVisible}
  234. values={stepFormValues}
  235. />
  236. ) : null}
  237. </PageHeaderWrapper>
  238. );
  239. };
  240. export default TableList;