index.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { Modal } from 'antd';
  2. import { useEffect, useMemo, useState } from 'react';
  3. import { Checkbox, Form, FormGrid, FormItem, Input, Select } from '@formily/antd';
  4. import { createForm } from '@formily/core';
  5. import type { ISchema } from '@formily/react';
  6. import { createSchemaField } from '@formily/react';
  7. import { onlyMessage, useAsyncDataSource } from '@/utils/util';
  8. import { service } from '@/pages/account/NotificationSubscription';
  9. import _ from 'lodash';
  10. interface Props {
  11. data: Partial<NotifitionSubscriptionItem>;
  12. close: () => void;
  13. reload: () => void;
  14. }
  15. const Save = (props: Props) => {
  16. const [data, setDada] = useState<any>(props.data || {});
  17. const [dataList, setDataList] = useState<any[]>([]);
  18. const form = useMemo(
  19. () =>
  20. createForm({
  21. validateFirst: true,
  22. initialValues: data,
  23. }),
  24. [],
  25. );
  26. useEffect(() => {
  27. if (props.data?.topicConfig) {
  28. const param = {
  29. ...props.data,
  30. topicConfig: {
  31. alarmConfigId: props.data?.topicConfig?.alarmConfigId.split(','),
  32. alarmConfigName: props.data?.topicConfig?.alarmConfigName.split(','),
  33. },
  34. };
  35. setDada({ ...param });
  36. form.setValues(param);
  37. }
  38. }, [props.data]);
  39. const queryProvidersList = () => service.getProvidersList();
  40. const queryAlarmConfigList = () => {
  41. return service.getAlarmConfigList().then((resp) => {
  42. setDataList(resp);
  43. return resp;
  44. });
  45. };
  46. const schema: ISchema = {
  47. type: 'object',
  48. properties: {
  49. grid: {
  50. type: 'void',
  51. 'x-component': 'FormGrid',
  52. 'x-component-props': {
  53. maxColumns: 2,
  54. minColumns: 1,
  55. },
  56. properties: {
  57. subscribeName: {
  58. title: '名称',
  59. 'x-component': 'Input',
  60. 'x-decorator': 'FormItem',
  61. required: true,
  62. 'x-decorator-props': {
  63. gridSpan: 2,
  64. labelAlign: 'left',
  65. layout: 'vertical',
  66. },
  67. 'x-component-props': {
  68. placeholder: '请输入名称',
  69. },
  70. 'x-validator': [
  71. {
  72. required: true,
  73. message: '请输入名称',
  74. },
  75. {
  76. max: 64,
  77. message: '最多可输入64个字符',
  78. },
  79. ],
  80. },
  81. topicProvider: {
  82. title: '类型',
  83. 'x-component': 'Select',
  84. 'x-decorator': 'FormItem',
  85. required: true,
  86. 'x-decorator-props': {
  87. gridSpan: 1,
  88. labelAlign: 'left',
  89. layout: 'vertical',
  90. },
  91. 'x-component-props': {
  92. placeholder: '请选择类型',
  93. },
  94. 'x-reactions': ['{{useAsyncDataSource(queryProvidersList)}}'],
  95. 'x-validator': [
  96. {
  97. required: true,
  98. message: '请选择类型',
  99. },
  100. ],
  101. },
  102. 'topicConfig.alarmConfigId': {
  103. title: '告警规则',
  104. type: 'array',
  105. 'x-component': 'Select',
  106. 'x-decorator': 'FormItem',
  107. required: true,
  108. 'x-decorator-props': {
  109. gridSpan: 1,
  110. labelAlign: 'left',
  111. layout: 'vertical',
  112. },
  113. 'x-component-props': {
  114. mode: 'multiple',
  115. showArrow: true,
  116. placeholder: '请选择告警规则',
  117. filterOption: (input: string, option: any) =>
  118. option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
  119. },
  120. 'x-reactions': ['{{useAsyncDataSource(queryAlarmConfigList)}}'],
  121. 'x-validator': [
  122. {
  123. required: true,
  124. message: '请选择告警规则',
  125. },
  126. ],
  127. },
  128. notice: {
  129. title: '通知方式',
  130. type: 'array',
  131. required: true,
  132. 'x-disabled': true,
  133. default: [1],
  134. enum: [
  135. {
  136. label: '站内通知',
  137. value: 1,
  138. },
  139. {
  140. label: '邮件通知',
  141. value: 2,
  142. },
  143. {
  144. label: '短信通知',
  145. value: 3,
  146. },
  147. ],
  148. 'x-decorator': 'FormItem',
  149. 'x-component': 'Checkbox.Group',
  150. 'x-decorator-props': {
  151. gridSpan: 2,
  152. labelAlign: 'left',
  153. layout: 'vertical',
  154. },
  155. 'x-validator': [
  156. {
  157. required: true,
  158. message: '请选择通知方式',
  159. },
  160. ],
  161. },
  162. },
  163. },
  164. },
  165. };
  166. const SchemaField = createSchemaField({
  167. components: {
  168. FormItem,
  169. FormGrid,
  170. Input,
  171. Select,
  172. Checkbox,
  173. },
  174. });
  175. const handleSave = async () => {
  176. let param: any = await form.submit();
  177. delete param.notice;
  178. const config = dataList.filter((item) =>
  179. param?.topicConfig?.alarmConfigId.includes(item?.value),
  180. );
  181. param = {
  182. ...data,
  183. ...param,
  184. topicConfig: {
  185. ...param?.topicConfig,
  186. alarmConfigId: param?.topicConfig.alarmConfigId.join(','),
  187. alarmConfigName: _.map(config, 'label').join(','),
  188. },
  189. };
  190. const response: any = await service.saveData(param);
  191. if (response.status === 200) {
  192. onlyMessage('操作成功!');
  193. props.reload();
  194. }
  195. };
  196. return (
  197. <Modal
  198. title={props.data.id ? '编辑' : '新增'}
  199. visible
  200. onCancel={props.close}
  201. onOk={() => handleSave()}
  202. width={'45vw'}
  203. >
  204. <Form form={form} layout="vertical">
  205. <SchemaField
  206. schema={schema}
  207. scope={{
  208. useAsyncDataSource,
  209. queryProvidersList,
  210. queryAlarmConfigList,
  211. }}
  212. />
  213. </Form>
  214. </Modal>
  215. );
  216. };
  217. export default Save;