File size: 2,936 Bytes
f859b0d
de9090d
 
f859b0d
de9090d
 
 
 
be99f83
f859b0d
 
be99f83
 
 
 
13080d4
f859b0d
de9090d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be99f83
 
de9090d
 
 
 
 
 
be99f83
de9090d
 
 
 
 
 
be99f83
de9090d
 
f859b0d
 
 
de9090d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f859b0d
de9090d
f859b0d
de9090d
f859b0d
de9090d
 
 
 
 
 
 
 
 
 
 
 
be99f83
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { PlusOutlined } from '@ant-design/icons';
import { Button, Form, Input } from 'antd';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { BeginQuery, IOperatorForm } from '../../interface';
import { useEditQueryRecord } from './hooks';
import { ModalForm } from './paramater-modal';
import QueryTable from './query-table';

import styles from './index.less';

type FieldType = {
  prologue?: string;
};

const BeginForm = ({ onValuesChange, form }: IOperatorForm) => {
  const { t } = useTranslation();
  const {
    ok,
    currentRecord,
    visible,
    hideModal,
    showModal,
    otherThanCurrentQuery,
  } = useEditQueryRecord({
    form,
    onValuesChange,
  });

  const handleDeleteRecord = useCallback(
    (idx: number) => {
      const query = form?.getFieldValue('query') || [];
      const nextQuery = query.filter(
        (item: BeginQuery, index: number) => index !== idx,
      );
      onValuesChange?.(
        { query: nextQuery },
        { query: nextQuery, prologue: form?.getFieldValue('prologue') },
      );
    },
    [form, onValuesChange],
  );

  return (
    <Form.Provider
      onFormFinish={(name, { values }) => {
        if (name === 'queryForm') {
          ok(values as BeginQuery);
        }
      }}
    >
      <Form
        name="basicForm"
        onValuesChange={onValuesChange}
        autoComplete="off"
        form={form}
        layout="vertical"
      >
        <Form.Item<FieldType>
          name={'prologue'}
          label={t('chat.setAnOpener')}
          tooltip={t('chat.setAnOpenerTip')}
          initialValue={t('chat.setAnOpenerInitial')}
        >
          <Input.TextArea autoSize={{ minRows: 5 }} />
        </Form.Item>
        {/* Create a hidden field to make Form instance record this */}
        <Form.Item name="query" noStyle />

        <Form.Item
          shouldUpdate={(prevValues, curValues) =>
            prevValues.query !== curValues.query
          }
        >
          {({ getFieldValue }) => {
            const query: BeginQuery[] = getFieldValue('query') || [];
            return (
              <QueryTable
                data={query}
                showModal={showModal}
                deleteRecord={handleDeleteRecord}
              ></QueryTable>
            );
          }}
        </Form.Item>

        <Button
          htmlType="button"
          style={{ margin: '0 8px' }}
          onClick={() => showModal()}
          icon={<PlusOutlined />}
          block
          className={styles.addButton}
        >
          {t('flow.addItem')}
        </Button>
        {visible && (
          <ModalForm
            visible={visible}
            hideModal={hideModal}
            initialValue={currentRecord}
            onOk={ok}
            otherThanCurrentQuery={otherThanCurrentQuery}
          />
        )}
      </Form>
    </Form.Provider>
  );
};

export default BeginForm;