File size: 3,310 Bytes
b9fe2b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
import { useResetFormOnCloseModal } from '@/hooks/logic-hooks';
import { IModalProps } from '@/interfaces/common';
import { Form, Input, Modal, Select, Switch } from 'antd';
import { DefaultOptionType } from 'antd/es/select';
import { useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { BeginQueryType, BeginQueryTypeIconMap } from '../../constant';
import { BeginQuery } from '../../interface';
import BeginDynamicOptions from './begin-dynamic-options';

export const ModalForm = ({
  visible,
  initialValue,
  hideModal,
  otherThanCurrentQuery,
}: IModalProps<BeginQuery> & {
  initialValue: BeginQuery;
  otherThanCurrentQuery: BeginQuery[];
}) => {
  const { t } = useTranslation();
  const [form] = Form.useForm();
  const options = useMemo(() => {
    return Object.values(BeginQueryType).reduce<DefaultOptionType[]>(
      (pre, cur) => {
        const Icon = BeginQueryTypeIconMap[cur];

        return [
          ...pre,
          {
            label: (
              <div className="flex items-center gap-2">
                <Icon
                  className={`size-${cur === BeginQueryType.Options ? 4 : 5}`}
                ></Icon>
                {cur}
              </div>
            ),
            value: cur,
          },
        ];
      },
      [],
    );
  }, []);

  useResetFormOnCloseModal({
    form,
    visible: visible,
  });

  useEffect(() => {
    form.setFieldsValue(initialValue);
  }, [form, initialValue]);

  const onOk = () => {
    form.submit();
  };

  return (
    <Modal
      title={t('flow.variableSettings')}
      open={visible}
      onOk={onOk}
      onCancel={hideModal}
      centered
    >
      <Form form={form} layout="vertical" name="queryForm" autoComplete="false">
        <Form.Item
          name="type"
          label="Type"
          rules={[{ required: true }]}
          initialValue={BeginQueryType.Line}
        >
          <Select options={options} />
        </Form.Item>
        <Form.Item
          name="key"
          label="Key"
          rules={[
            { required: true },
            () => ({
              validator(_, value) {
                if (
                  !value ||
                  !otherThanCurrentQuery.some((x) => x.key === value)
                ) {
                  return Promise.resolve();
                }
                return Promise.reject(new Error('The key cannot be repeated!'));
              },
            }),
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item name="name" label="Name" rules={[{ required: true }]}>
          <Input />
        </Form.Item>
        <Form.Item
          name="optional"
          label={'Optional'}
          valuePropName="checked"
          initialValue={false}
        >
          <Switch />
        </Form.Item>
        <Form.Item
          shouldUpdate={(prevValues, curValues) =>
            prevValues.type !== curValues.type
          }
        >
          {({ getFieldValue }) => {
            const type: BeginQueryType = getFieldValue('type');
            return (
              type === BeginQueryType.Options && (
                <BeginDynamicOptions></BeginDynamicOptions>
              )
            );
          }}
        </Form.Item>
      </Form>
    </Modal>
  );
};