index.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import { Button, Card, Form, Input } from 'antd';
  3. import useLocation from '@/hooks/route/useLocation';
  4. import Device from '../Save/device/index';
  5. import Manual from '../Save/manual/index';
  6. import Timer from '../Save/timer/index';
  7. import { TitleComponent } from '@/components';
  8. import { observable } from '@formily/reactive';
  9. import { observer } from '@formily/react';
  10. import type { FormModelType } from '@/pages/rule-engine/Scene/typings';
  11. import { useEffect, useCallback } from 'react';
  12. import { service } from '@/pages/rule-engine/Scene';
  13. import './index.less';
  14. import { onlyMessage } from '@/utils/util';
  15. import { useHistory } from 'umi';
  16. import { getMenuPathByCode } from '@/utils/menu';
  17. export const FormModel = observable<FormModelType>({
  18. trigger: {
  19. type: '',
  20. options: {},
  21. },
  22. actions: [],
  23. options: {
  24. terms: [
  25. {
  26. terms: [],
  27. },
  28. ],
  29. },
  30. branches: [
  31. {
  32. when: [
  33. {
  34. terms: [
  35. {
  36. column: undefined,
  37. value: undefined,
  38. termType: undefined,
  39. key: 'params_1',
  40. type: 'and',
  41. },
  42. ],
  43. type: 'and',
  44. key: 'terms_1',
  45. },
  46. ],
  47. key: 'branckes_1',
  48. shakeLimit: {
  49. enabled: true,
  50. time: 1,
  51. threshold: 1,
  52. alarmFirst: false,
  53. },
  54. then: [],
  55. },
  56. {
  57. when: [],
  58. key: 'branckes_2',
  59. shakeLimit: {
  60. enabled: true,
  61. time: 1,
  62. threshold: 1,
  63. alarmFirst: false,
  64. },
  65. then: [],
  66. },
  67. ],
  68. });
  69. export default observer(() => {
  70. const location = useLocation();
  71. const triggerType = location?.query?.triggerType || '';
  72. const id = location?.query?.id || '';
  73. const history = useHistory();
  74. useEffect(() => {
  75. if (id) {
  76. service.detail(id).then((resp) => {
  77. if (resp.status === 200) {
  78. Object.assign(FormModel, resp.result);
  79. }
  80. });
  81. }
  82. }, [id]);
  83. const triggerRender = (type: string) => {
  84. FormModel.trigger!.type = type;
  85. switch (type) {
  86. case 'device':
  87. return (
  88. <Form.Item label={<TitleComponent style={{ fontSize: 14 }} data={'设备触发'} />}>
  89. <Device />
  90. </Form.Item>
  91. );
  92. case 'manual':
  93. return (
  94. <Form.Item label={<TitleComponent style={{ fontSize: 14 }} data={'手动触发'} />}>
  95. <Manual />
  96. </Form.Item>
  97. );
  98. case 'timer':
  99. return (
  100. <Form.Item label={<TitleComponent style={{ fontSize: 14 }} data={'定时触发'} />}>
  101. <Timer />
  102. </Form.Item>
  103. );
  104. default:
  105. return null;
  106. }
  107. };
  108. const submit = useCallback(() => {
  109. service.updateScene(FormModel).then((res) => {
  110. if (res.status === 200) {
  111. onlyMessage('操作成功', 'success');
  112. const url = getMenuPathByCode('rule-engine/Scene');
  113. history.push(url);
  114. }
  115. });
  116. }, [id, FormModel]);
  117. return (
  118. <PageContainer>
  119. <Card>
  120. <Form name="timer" layout={'vertical'}>
  121. {triggerRender(triggerType)}
  122. <Form.Item
  123. label={<TitleComponent style={{ fontSize: 14 }} data={'说明'} />}
  124. name="description"
  125. >
  126. <Input.TextArea showCount maxLength={200} placeholder={'请输入说明'} rows={4} />
  127. </Form.Item>
  128. <Form.Item>
  129. <Button type="primary" htmlType="submit" onClick={submit}>
  130. 保存
  131. </Button>
  132. </Form.Item>
  133. </Form>
  134. </Card>
  135. </PageContainer>
  136. );
  137. });