|
|
@@ -1,11 +1,11 @@
|
|
|
-import { useEffect, useState } from 'react';
|
|
|
+import { useCallback, useEffect, useState } from 'react';
|
|
|
import { TitleComponent } from '@/components';
|
|
|
import { observer, Observer } from '@formily/react';
|
|
|
import { model } from '@formily/reactive';
|
|
|
import { FormModel, defaultBranches } from '@/pages/rule-engine/Scene/Save';
|
|
|
import BranchItem from './branchItem';
|
|
|
import { service } from '@/pages/rule-engine/Scene/index';
|
|
|
-import { Form, Switch } from 'antd';
|
|
|
+import { Form, FormInstance, Switch } from 'antd';
|
|
|
import type { TriggerType } from '@/pages/rule-engine/Scene/typings';
|
|
|
import Actions from '@/pages/rule-engine/Scene/Save/action';
|
|
|
import { cloneDeep, set } from 'lodash';
|
|
|
@@ -21,7 +21,11 @@ export const TermsModel = model<TermsModelProps>({
|
|
|
columnOptions: [],
|
|
|
});
|
|
|
|
|
|
-export default observer(() => {
|
|
|
+interface Props {
|
|
|
+ form: FormInstance;
|
|
|
+}
|
|
|
+
|
|
|
+export default observer((props: Props) => {
|
|
|
const [open, setOpen] = useState(true);
|
|
|
|
|
|
useEffect(() => {
|
|
|
@@ -46,37 +50,43 @@ export default observer(() => {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- const openChange = (checked: boolean) => {
|
|
|
- setOpen(checked);
|
|
|
- const key = randomString();
|
|
|
- if (checked) {
|
|
|
- FormModel.current.branches = cloneDeep([...defaultBranches, null as any]);
|
|
|
- set(FormModel.current.options!, 'when', [
|
|
|
- {
|
|
|
- terms: [
|
|
|
- {
|
|
|
- terms: [],
|
|
|
+ const openChange = useCallback(
|
|
|
+ (checked: boolean) => {
|
|
|
+ setOpen(checked);
|
|
|
+ const key = randomString();
|
|
|
+ if (checked) {
|
|
|
+ FormModel.current.branches = cloneDeep([...defaultBranches, null as any]);
|
|
|
+ set(FormModel.current.options!, 'when', [
|
|
|
+ {
|
|
|
+ terms: [
|
|
|
+ {
|
|
|
+ terms: [],
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ]);
|
|
|
+ props.form.setFieldValue(['branches'], [...defaultBranches]);
|
|
|
+ } else {
|
|
|
+ const newValue = [
|
|
|
+ {
|
|
|
+ when: [],
|
|
|
+ key: 'branches_' + key,
|
|
|
+ shakeLimit: {
|
|
|
+ enabled: false,
|
|
|
+ time: 0,
|
|
|
+ threshold: 0,
|
|
|
+ alarmFirst: false,
|
|
|
},
|
|
|
- ],
|
|
|
- },
|
|
|
- ]);
|
|
|
- } else {
|
|
|
- FormModel.current.branches = [
|
|
|
- {
|
|
|
- when: [],
|
|
|
- key: 'branches_' + key,
|
|
|
- shakeLimit: {
|
|
|
- enabled: false,
|
|
|
- time: 0,
|
|
|
- threshold: 0,
|
|
|
- alarmFirst: false,
|
|
|
+ then: [],
|
|
|
},
|
|
|
- then: [],
|
|
|
- },
|
|
|
- ];
|
|
|
- set(FormModel.current.options!, 'when', []);
|
|
|
- }
|
|
|
- };
|
|
|
+ ];
|
|
|
+ FormModel.current.branches = newValue;
|
|
|
+ set(FormModel.current.options!, 'when', []);
|
|
|
+ props.form.setFieldValue(['branches'], newValue);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ [props.form],
|
|
|
+ );
|
|
|
|
|
|
const addBranches = () => {
|
|
|
const key = randomString();
|
|
|
@@ -123,8 +133,10 @@ export default observer(() => {
|
|
|
const isFirst = index === 0;
|
|
|
return item ? (
|
|
|
<BranchItem
|
|
|
+ form={props.form}
|
|
|
data={item}
|
|
|
isFirst={isFirst}
|
|
|
+ className={isFirst ? 'first-children' : ''}
|
|
|
name={index}
|
|
|
key={item.key}
|
|
|
paramsOptions={TermsModel.columnOptions}
|
|
|
@@ -145,7 +157,13 @@ export default observer(() => {
|
|
|
}}
|
|
|
/>
|
|
|
) : (
|
|
|
- <div className="actions-terms-warp" style={{ alignItems: 'center' }}>
|
|
|
+ <div
|
|
|
+ className="actions-terms-warp"
|
|
|
+ style={{
|
|
|
+ alignItems: 'center',
|
|
|
+ marginTop: FormModel.current.branches?.length === 2 ? 0 : 24,
|
|
|
+ }}
|
|
|
+ >
|
|
|
<div className="actions-terms-title" style={{ padding: 0 }}>
|
|
|
否则
|
|
|
</div>
|
|
|
@@ -162,14 +180,27 @@ export default observer(() => {
|
|
|
}
|
|
|
</Observer>
|
|
|
) : (
|
|
|
- <Form.Item>
|
|
|
+ <Form.Item
|
|
|
+ name={['branches', 0, 'then']}
|
|
|
+ rules={[
|
|
|
+ {
|
|
|
+ validator(_, v) {
|
|
|
+ if (!v || (v && !v.length)) {
|
|
|
+ return Promise.reject('至少配置一个执行动作');
|
|
|
+ }
|
|
|
+ return Promise.resolve();
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ]}
|
|
|
+ >
|
|
|
<Actions
|
|
|
openShakeLimit={true}
|
|
|
name={0}
|
|
|
thenOptions={FormModel.current.branches ? FormModel.current.branches[0].then : []}
|
|
|
onAdd={(data) => {
|
|
|
if (FormModel.current.branches && data) {
|
|
|
- FormModel.current.branches[0].then = [...FormModel.current.branches[0].then, data];
|
|
|
+ const newThen = [...FormModel.current.branches[0].then, data];
|
|
|
+ FormModel.current.branches[0].then = newThen;
|
|
|
}
|
|
|
}}
|
|
|
onUpdate={(data, type) => {
|
|
|
@@ -177,7 +208,11 @@ export default observer(() => {
|
|
|
(item) => item.parallel === type,
|
|
|
);
|
|
|
if (indexOf !== -1) {
|
|
|
- FormModel.current.branches![0].then[indexOf] = data;
|
|
|
+ if (data.actions?.length) {
|
|
|
+ FormModel.current.branches![0].then[indexOf] = data;
|
|
|
+ } else {
|
|
|
+ FormModel.current.branches![0].then = [];
|
|
|
+ }
|
|
|
}
|
|
|
}}
|
|
|
/>
|