balibabu
commited on
Commit
·
17cd183
1
Parent(s):
6674a75
feat: validate the name field of the categorize operator for duplicate names and nulls #918 (#1471)
Browse files### What problem does this PR solve?
feat: validate the name field of the categorize operator for duplicate
names and nulls #918
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- web/src/locales/en.ts +2 -0
- web/src/locales/zh-traditional.ts +2 -0
- web/src/locales/zh.ts +2 -0
- web/src/pages/flow/canvas/node/categorize-node.tsx +0 -1
- web/src/pages/flow/categorize-form/dynamic-categorize.tsx +98 -12
- web/src/pages/flow/categorize-form/hooks.ts +0 -2
- web/src/pages/flow/form-hooks.ts +7 -2
web/src/locales/en.ts
CHANGED
@@ -596,6 +596,8 @@ The above is the content you need to summarize.`,
|
|
596 |
blank: 'Blank',
|
597 |
createFromNothing: 'Create from nothing',
|
598 |
addItem: 'Add Item',
|
|
|
|
|
599 |
},
|
600 |
footer: {
|
601 |
profile: 'All rights reserved @ React',
|
|
|
596 |
blank: 'Blank',
|
597 |
createFromNothing: 'Create from nothing',
|
598 |
addItem: 'Add Item',
|
599 |
+
nameRequiredMsg: 'Name is required',
|
600 |
+
nameRepeatedMsg: 'The name cannot be repeated',
|
601 |
},
|
602 |
footer: {
|
603 |
profile: 'All rights reserved @ React',
|
web/src/locales/zh-traditional.ts
CHANGED
@@ -557,6 +557,8 @@ export default {
|
|
557 |
blank: '空',
|
558 |
createFromNothing: '從無到有',
|
559 |
addItem: '新增',
|
|
|
|
|
560 |
},
|
561 |
footer: {
|
562 |
profile: '“保留所有權利 @ react”',
|
|
|
557 |
blank: '空',
|
558 |
createFromNothing: '從無到有',
|
559 |
addItem: '新增',
|
560 |
+
nameRequiredMsg: '名稱不能為空',
|
561 |
+
nameRepeatedMsg: '名稱不能重複',
|
562 |
},
|
563 |
footer: {
|
564 |
profile: '“保留所有權利 @ react”',
|
web/src/locales/zh.ts
CHANGED
@@ -575,6 +575,8 @@ export default {
|
|
575 |
blank: '空',
|
576 |
createFromNothing: '从无到有',
|
577 |
addItem: '新增',
|
|
|
|
|
578 |
},
|
579 |
footer: {
|
580 |
profile: 'All rights reserved @ React',
|
|
|
575 |
blank: '空',
|
576 |
createFromNothing: '从无到有',
|
577 |
addItem: '新增',
|
578 |
+
nameRequiredMsg: '名称不能为空',
|
579 |
+
nameRepeatedMsg: '名称不能重复',
|
580 |
},
|
581 |
footer: {
|
582 |
profile: 'All rights reserved @ React',
|
web/src/pages/flow/canvas/node/categorize-node.tsx
CHANGED
@@ -46,7 +46,6 @@ export function CategorizeNode({ id, data, selected }: NodeProps<NodeData>) {
|
|
46 |
),
|
47 |
indexesInUse,
|
48 |
);
|
49 |
-
console.info('newPositionMap:', newPositionMap);
|
50 |
|
51 |
const nextPostionMap = {
|
52 |
...pick(state, intersectionKeys),
|
|
|
46 |
),
|
47 |
indexesInUse,
|
48 |
);
|
|
|
49 |
|
50 |
const nextPostionMap = {
|
51 |
...pick(state, intersectionKeys),
|
web/src/pages/flow/categorize-form/dynamic-categorize.tsx
CHANGED
@@ -1,16 +1,91 @@
|
|
1 |
import { useTranslate } from '@/hooks/commonHooks';
|
2 |
import { CloseOutlined } from '@ant-design/icons';
|
3 |
-
import { Button, Card, Form, Input, Select } from 'antd';
|
|
|
4 |
import { humanId } from 'human-id';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import { useUpdateNodeInternals } from 'reactflow';
|
6 |
import { Operator } from '../constant';
|
7 |
import { useBuildFormSelectOptions } from '../form-hooks';
|
8 |
-
import { ICategorizeItem } from '../interface';
|
9 |
|
10 |
interface IProps {
|
11 |
nodeId?: string;
|
12 |
}
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
const DynamicCategorize = ({ nodeId }: IProps) => {
|
15 |
const updateNodeInternals = useUpdateNodeInternals();
|
16 |
const form = Form.useFormInstance();
|
@@ -45,11 +120,28 @@ const DynamicCategorize = ({ nodeId }: IProps) => {
|
|
45 |
}
|
46 |
>
|
47 |
<Form.Item
|
48 |
-
label={t('name')}
|
49 |
name={[field.name, 'name']}
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
>
|
52 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
</Form.Item>
|
54 |
<Form.Item
|
55 |
label={t('description')}
|
@@ -67,13 +159,7 @@ const DynamicCategorize = ({ nodeId }: IProps) => {
|
|
67 |
<Select
|
68 |
allowClear
|
69 |
options={buildCategorizeToOptions(
|
70 |
-
(form
|
71 |
-
.map((x: ICategorizeItem) => x.to)
|
72 |
-
.filter(
|
73 |
-
(x: string) =>
|
74 |
-
x !==
|
75 |
-
form.getFieldValue(['items', field.name, 'to']),
|
76 |
-
),
|
77 |
)}
|
78 |
/>
|
79 |
</Form.Item>
|
|
|
1 |
import { useTranslate } from '@/hooks/commonHooks';
|
2 |
import { CloseOutlined } from '@ant-design/icons';
|
3 |
+
import { Button, Card, Form, FormListFieldData, Input, Select } from 'antd';
|
4 |
+
import { FormInstance } from 'antd/lib';
|
5 |
import { humanId } from 'human-id';
|
6 |
+
import trim from 'lodash/trim';
|
7 |
+
import {
|
8 |
+
ChangeEventHandler,
|
9 |
+
FocusEventHandler,
|
10 |
+
useCallback,
|
11 |
+
useEffect,
|
12 |
+
useState,
|
13 |
+
} from 'react';
|
14 |
import { useUpdateNodeInternals } from 'reactflow';
|
15 |
import { Operator } from '../constant';
|
16 |
import { useBuildFormSelectOptions } from '../form-hooks';
|
|
|
17 |
|
18 |
interface IProps {
|
19 |
nodeId?: string;
|
20 |
}
|
21 |
|
22 |
+
interface INameInputProps {
|
23 |
+
value?: string;
|
24 |
+
onChange?: (value: string) => void;
|
25 |
+
otherNames?: string[];
|
26 |
+
validate(errors: string[]): void;
|
27 |
+
}
|
28 |
+
|
29 |
+
const getOtherFieldValues = (
|
30 |
+
form: FormInstance,
|
31 |
+
field: FormListFieldData,
|
32 |
+
latestField: string,
|
33 |
+
) =>
|
34 |
+
(form.getFieldValue(['items']) ?? [])
|
35 |
+
.map((x: any) => x[latestField])
|
36 |
+
.filter(
|
37 |
+
(x: string) =>
|
38 |
+
x !== form.getFieldValue(['items', field.name, latestField]),
|
39 |
+
);
|
40 |
+
|
41 |
+
const NameInput = ({
|
42 |
+
value,
|
43 |
+
onChange,
|
44 |
+
otherNames,
|
45 |
+
validate,
|
46 |
+
}: INameInputProps) => {
|
47 |
+
const [name, setName] = useState<string | undefined>();
|
48 |
+
const { t } = useTranslate('flow');
|
49 |
+
|
50 |
+
const handleNameChange: ChangeEventHandler<HTMLInputElement> = useCallback(
|
51 |
+
(e) => {
|
52 |
+
const val = e.target.value;
|
53 |
+
// trigger validation
|
54 |
+
if (otherNames?.some((x) => x === val)) {
|
55 |
+
validate([t('nameRepeatedMsg')]);
|
56 |
+
} else if (trim(val) === '') {
|
57 |
+
validate([t('nameRequiredMsg')]);
|
58 |
+
} else {
|
59 |
+
validate([]);
|
60 |
+
}
|
61 |
+
setName(val);
|
62 |
+
},
|
63 |
+
[otherNames, validate, t],
|
64 |
+
);
|
65 |
+
|
66 |
+
const handleNameBlur: FocusEventHandler<HTMLInputElement> = useCallback(
|
67 |
+
(e) => {
|
68 |
+
const val = e.target.value;
|
69 |
+
if (otherNames?.every((x) => x !== val) && trim(val) !== '') {
|
70 |
+
onChange?.(val);
|
71 |
+
}
|
72 |
+
},
|
73 |
+
[onChange, otherNames],
|
74 |
+
);
|
75 |
+
|
76 |
+
useEffect(() => {
|
77 |
+
setName(value);
|
78 |
+
}, [value]);
|
79 |
+
|
80 |
+
return (
|
81 |
+
<Input
|
82 |
+
value={name}
|
83 |
+
onChange={handleNameChange}
|
84 |
+
onBlur={handleNameBlur}
|
85 |
+
></Input>
|
86 |
+
);
|
87 |
+
};
|
88 |
+
|
89 |
const DynamicCategorize = ({ nodeId }: IProps) => {
|
90 |
const updateNodeInternals = useUpdateNodeInternals();
|
91 |
const form = Form.useFormInstance();
|
|
|
120 |
}
|
121 |
>
|
122 |
<Form.Item
|
123 |
+
label={t('name')}
|
124 |
name={[field.name, 'name']}
|
125 |
+
validateTrigger={['onChange', 'onBlur']}
|
126 |
+
rules={[
|
127 |
+
{
|
128 |
+
required: true,
|
129 |
+
whitespace: true,
|
130 |
+
message: t('nameMessage'),
|
131 |
+
},
|
132 |
+
]}
|
133 |
>
|
134 |
+
<NameInput
|
135 |
+
otherNames={getOtherFieldValues(form, field, 'name')}
|
136 |
+
validate={(errors: string[]) =>
|
137 |
+
form.setFields([
|
138 |
+
{
|
139 |
+
name: ['items', field.name, 'name'],
|
140 |
+
errors,
|
141 |
+
},
|
142 |
+
])
|
143 |
+
}
|
144 |
+
></NameInput>
|
145 |
</Form.Item>
|
146 |
<Form.Item
|
147 |
label={t('description')}
|
|
|
159 |
<Select
|
160 |
allowClear
|
161 |
options={buildCategorizeToOptions(
|
162 |
+
getOtherFieldValues(form, field, 'to'),
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
)}
|
164 |
/>
|
165 |
</Form.Item>
|
web/src/pages/flow/categorize-form/hooks.ts
CHANGED
@@ -67,7 +67,6 @@ export const useHandleFormValuesChange = ({
|
|
67 |
|
68 |
const handleValuesChange = useCallback(
|
69 |
(changedValues: any, values: any) => {
|
70 |
-
console.info(changedValues, values);
|
71 |
onValuesChange?.(changedValues, {
|
72 |
...omit(values, 'items'),
|
73 |
category_description: buildCategorizeObjectFromList(values.items),
|
@@ -80,7 +79,6 @@ export const useHandleFormValuesChange = ({
|
|
80 |
const items = buildCategorizeListFromObject(
|
81 |
get(node, 'data.form.category_description', {}),
|
82 |
);
|
83 |
-
console.info('effect:', items);
|
84 |
form?.setFieldsValue({
|
85 |
items,
|
86 |
});
|
|
|
67 |
|
68 |
const handleValuesChange = useCallback(
|
69 |
(changedValues: any, values: any) => {
|
|
|
70 |
onValuesChange?.(changedValues, {
|
71 |
...omit(values, 'items'),
|
72 |
category_description: buildCategorizeObjectFromList(values.items),
|
|
|
79 |
const items = buildCategorizeListFromObject(
|
80 |
get(node, 'data.form.category_description', {}),
|
81 |
);
|
|
|
82 |
form?.setFieldsValue({
|
83 |
items,
|
84 |
});
|
web/src/pages/flow/form-hooks.ts
CHANGED
@@ -4,8 +4,13 @@ import useGraphStore from './store';
|
|
4 |
|
5 |
const ExcludedNodesMap = {
|
6 |
// exclude some nodes downstream of the classification node
|
7 |
-
[Operator.Categorize]: [
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
[Operator.Generate]: [Operator.Begin],
|
10 |
};
|
11 |
|
|
|
4 |
|
5 |
const ExcludedNodesMap = {
|
6 |
// exclude some nodes downstream of the classification node
|
7 |
+
[Operator.Categorize]: [
|
8 |
+
Operator.Categorize,
|
9 |
+
Operator.Answer,
|
10 |
+
Operator.Begin,
|
11 |
+
Operator.Relevant,
|
12 |
+
],
|
13 |
+
[Operator.Relevant]: [Operator.Begin, Operator.Answer, Operator.Relevant],
|
14 |
[Operator.Generate]: [Operator.Begin],
|
15 |
};
|
16 |
|