index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { useIntl } from '@/.umi/plugin-locale/localeExports';
  2. import PermissionButton from '@/components/PermissionButton';
  3. import SearchComponent from '@/components/SearchComponent';
  4. import {
  5. DeleteOutlined,
  6. EditOutlined,
  7. PlayCircleOutlined,
  8. PlusOutlined,
  9. StopOutlined,
  10. } from '@ant-design/icons';
  11. import { PageContainer } from '@ant-design/pro-layout';
  12. import { observer } from '@formily/reactive-react';
  13. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  14. import ProTable from '@jetlinks/pro-table';
  15. import { Badge, message } from 'antd';
  16. import { useEffect, useRef, useState } from 'react';
  17. import Save from './save';
  18. import Service from './service';
  19. export const service = new Service('notifications/subscriptions');
  20. const NotificationSubscription = observer(() => {
  21. const intl = useIntl();
  22. const actionRef = useRef<ActionType>();
  23. const [param, setParam] = useState({});
  24. const [visible, setVisible] = useState<boolean>(false);
  25. const [current, setCurrent] = useState<Partial<NotifitionSubscriptionItem>>({});
  26. const [typeList, setTypeList] = useState<any>({});
  27. useEffect(() => {
  28. service.getProvidersList().then((resp) => {
  29. const obj: any = {};
  30. resp.map((i: any) => {
  31. obj[i?.value] = i?.label || '';
  32. });
  33. setTypeList(obj);
  34. });
  35. }, []);
  36. const Tools = (record: any) => {
  37. return [
  38. <PermissionButton
  39. key={'update'}
  40. type={'link'}
  41. isPermission={true}
  42. style={{ padding: 0 }}
  43. tooltip={{
  44. title: intl.formatMessage({
  45. id: 'pages.data.option.edit',
  46. defaultMessage: '编辑',
  47. }),
  48. }}
  49. onClick={() => {
  50. setVisible(true);
  51. setCurrent(record);
  52. }}
  53. >
  54. <EditOutlined />
  55. </PermissionButton>,
  56. <PermissionButton
  57. key={'action'}
  58. type={'link'}
  59. style={{ padding: 0 }}
  60. isPermission={true}
  61. popConfirm={{
  62. title: intl.formatMessage({
  63. id: `pages.data.option.${
  64. record?.state?.value !== 'disabled' ? 'disabled' : 'enabled'
  65. }.tips`,
  66. defaultMessage: '确认禁用?',
  67. }),
  68. onConfirm: async () => {
  69. const resp =
  70. record?.state?.value !== 'disabled'
  71. ? await service._disabled(record.id)
  72. : await service._enabled(record.id);
  73. if (resp.status === 200) {
  74. message.success('操作成功!');
  75. actionRef.current?.reload?.();
  76. } else {
  77. message.error('操作失败!');
  78. }
  79. },
  80. }}
  81. tooltip={{
  82. title: intl.formatMessage({
  83. id: `pages.data.option.${record?.state?.value !== 'disabled' ? 'disabled' : 'enabled'}`,
  84. defaultMessage: '启用',
  85. }),
  86. }}
  87. >
  88. {record?.state?.value !== 'disabled' ? <StopOutlined /> : <PlayCircleOutlined />}
  89. </PermissionButton>,
  90. <PermissionButton
  91. key={'delete'}
  92. type={'link'}
  93. isPermission={true}
  94. style={{ padding: 0 }}
  95. disabled={record?.state?.value !== 'disabled'}
  96. popConfirm={{
  97. title: '确认删除?',
  98. disabled: record?.state?.value !== 'disabled',
  99. onConfirm: async () => {
  100. const resp: any = await service.remove(record.id);
  101. if (resp.status === 200) {
  102. message.success('操作成功!');
  103. actionRef.current?.reload?.();
  104. } else {
  105. message.error('操作失败!');
  106. }
  107. },
  108. }}
  109. tooltip={{
  110. title: record?.state?.value !== 'disabled' ? '请先禁用,再删除' : '删除',
  111. }}
  112. >
  113. <DeleteOutlined />
  114. </PermissionButton>,
  115. ];
  116. };
  117. const columns: ProColumns<NotifitionSubscriptionItem>[] = [
  118. {
  119. dataIndex: 'subscribeName',
  120. title: '名称',
  121. },
  122. {
  123. dataIndex: 'topicProvider',
  124. title: '类型',
  125. hideInSearch: true,
  126. render: (text: any, record: any) => {
  127. return <span>{typeList[record?.topicProvider] || text}</span>;
  128. },
  129. },
  130. {
  131. dataIndex: 'topicConfig',
  132. title: '告警规则',
  133. hideInSearch: true,
  134. render: (text: any, record: any) => (
  135. <span>{record?.topicConfig?.alarmConfigName || '-'}</span>
  136. ),
  137. },
  138. {
  139. dataIndex: 'state',
  140. title: '状态',
  141. hideInSearch: true,
  142. render: (text: any, record: any) => (
  143. <Badge
  144. status={record.state?.value === 'enabled' ? 'success' : 'error'}
  145. text={record?.state?.text || '-'}
  146. />
  147. ),
  148. },
  149. {
  150. title: '操作',
  151. valueType: 'option',
  152. align: 'center',
  153. width: 200,
  154. render: (text, record) => [Tools(record)],
  155. },
  156. ];
  157. return (
  158. <PageContainer>
  159. <SearchComponent<NotifitionSubscriptionItem>
  160. field={columns}
  161. target="notification-subscription"
  162. onSearch={(data) => {
  163. // 重置分页数据
  164. actionRef.current?.reset?.();
  165. setParam(data);
  166. }}
  167. />
  168. <ProTable<NotifitionSubscriptionItem>
  169. actionRef={actionRef}
  170. params={param}
  171. columns={columns}
  172. search={false}
  173. rowKey="id"
  174. request={async (params) =>
  175. service.query({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
  176. }
  177. headerTitle={[
  178. <PermissionButton
  179. onClick={() => {
  180. setVisible(true);
  181. setCurrent({});
  182. }}
  183. isPermission={true}
  184. style={{ marginRight: 12 }}
  185. key="button"
  186. icon={<PlusOutlined />}
  187. type="primary"
  188. >
  189. {intl.formatMessage({
  190. id: 'pages.data.option.add',
  191. defaultMessage: '新增',
  192. })}
  193. </PermissionButton>,
  194. ]}
  195. />
  196. {visible && (
  197. <Save
  198. close={() => {
  199. setVisible(false);
  200. }}
  201. data={current}
  202. reload={() => {
  203. setVisible(false);
  204. actionRef.current?.reload();
  205. }}
  206. />
  207. )}
  208. </PageContainer>
  209. );
  210. });
  211. export default NotificationSubscription;