index.tsx 6.5 KB

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