File size: 7,342 Bytes
2e208d4
d696cd8
6b7630c
2e208d4
6b7630c
 
 
 
 
cd85d9e
 
2bdad3e
b88bbad
cd85d9e
2e208d4
b88bbad
4138aee
 
8828184
2e208d4
16f8eca
8828184
 
16f8eca
2e208d4
8828184
 
 
 
 
 
 
6b7630c
 
 
 
 
 
 
 
 
 
 
 
 
 
2bdad3e
 
 
 
8aa83b1
2e208d4
 
 
 
 
 
 
6d81859
2e208d4
 
 
 
4138aee
 
 
 
 
 
 
 
 
 
 
 
 
6b7630c
4138aee
 
 
 
 
 
 
 
 
 
 
 
 
 
b88bbad
 
 
 
4138aee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e208d4
4138aee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e208d4
4138aee
edf3ea6
2e208d4
 
 
 
edf3ea6
4138aee
 
 
 
 
edf3ea6
 
 
 
 
2e208d4
 
99e5e05
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { CloseOutlined } from '@ant-design/icons';
import { Button, Card, Divider, Form, Input, Select } from 'antd';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import {
  Operator,
  SwitchElseTo,
  SwitchLogicOperatorOptions,
  SwitchOperatorOptions,
} from '../../constant';
import { useBuildFormSelectOptions } from '../../form-hooks';
import { useBuildComponentIdSelectOptions } from '../../hooks/use-get-begin-query';
import { IOperatorForm } from '../../interface';
import { getOtherFieldValues } from '../../utils';

import { ISwitchForm } from '@/interfaces/database/flow';
import styles from './index.less';

const SwitchForm = ({ onValuesChange, node, form }: IOperatorForm) => {
  const { t } = useTranslation();
  const buildCategorizeToOptions = useBuildFormSelectOptions(
    Operator.Switch,
    node?.id,
  );

  const getSelectedConditionTos = () => {
    const conditions: ISwitchForm['conditions'] =
      form?.getFieldValue('conditions');

    return conditions?.filter((x) => !!x).map((x) => x?.to) ?? [];
  };

  const switchOperatorOptions = useMemo(() => {
    return SwitchOperatorOptions.map((x) => ({
      value: x.value,
      label: t(`flow.switchOperatorOptions.${x.label}`),
    }));
  }, [t]);

  const switchLogicOperatorOptions = useMemo(() => {
    return SwitchLogicOperatorOptions.map((x) => ({
      value: x,
      label: t(`flow.switchLogicOperatorOptions.${x}`),
    }));
  }, [t]);

  const componentIdOptions = useBuildComponentIdSelectOptions(
    node?.id,
    node?.parentId,
  );

  return (
    <Form
      form={form}
      name="dynamic_form_complex"
      autoComplete="off"
      initialValues={{ conditions: [{}] }}
      onValuesChange={onValuesChange}
      layout={'vertical'}
    >
      <Form.List name="conditions">
        {(fields, { add, remove }) => (
          <div style={{ display: 'flex', rowGap: 16, flexDirection: 'column' }}>
            {fields.map((field) => {
              return (
                <Card
                  size="small"
                  title={`Case ${field.name + 1}`}
                  key={field.key}
                  className={styles.caseCard}
                  extra={
                    <CloseOutlined
                      onClick={() => {
                        remove(field.name);
                      }}
                    />
                  }
                >
                  <Form.Item noStyle dependencies={[field.name, 'items']}>
                    {({ getFieldValue }) =>
                      getFieldValue(['conditions', field.name, 'items'])
                        ?.length > 1 && (
                        <Form.Item
                          label={t('flow.logicalOperator')}
                          name={[field.name, 'logical_operator']}
                        >
                          <Select options={switchLogicOperatorOptions} />
                        </Form.Item>
                      )
                    }
                  </Form.Item>
                  <Form.Item
                    label={t('flow.nextStep')}
                    name={[field.name, 'to']}
                  >
                    <Select
                      allowClear
                      options={buildCategorizeToOptions([
                        form?.getFieldValue(SwitchElseTo),
                        ...getOtherFieldValues(
                          form!,
                          'conditions',
                          field,
                          'to',
                        ),
                      ])}
                    />
                  </Form.Item>
                  <Form.Item label="Condition">
                    <Form.List name={[field.name, 'items']}>
                      {(subFields, subOpt) => (
                        <div
                          style={{
                            display: 'flex',
                            flexDirection: 'column',
                            rowGap: 16,
                          }}
                        >
                          {subFields.map((subField) => (
                            <Card
                              key={subField.key}
                              title={null}
                              size="small"
                              className={styles.conditionCard}
                              bordered
                              extra={
                                <CloseOutlined
                                  onClick={() => {
                                    subOpt.remove(subField.name);
                                  }}
                                />
                              }
                            >
                              <Form.Item
                                label={t('flow.componentId')}
                                name={[subField.name, 'cpn_id']}
                              >
                                <Select
                                  placeholder={t('flow.componentId')}
                                  options={componentIdOptions}
                                />
                              </Form.Item>
                              <Form.Item
                                label={t('flow.operator')}
                                name={[subField.name, 'operator']}
                              >
                                <Select
                                  placeholder={t('flow.operator')}
                                  options={switchOperatorOptions}
                                />
                              </Form.Item>
                              <Form.Item
                                label={t('flow.value')}
                                name={[subField.name, 'value']}
                              >
                                <Input placeholder={t('flow.value')} />
                              </Form.Item>
                            </Card>
                          ))}
                          <Button
                            onClick={() => {
                              form?.setFieldValue(
                                ['conditions', field.name, 'logical_operator'],
                                SwitchLogicOperatorOptions[0],
                              );
                              subOpt.add({
                                operator: SwitchOperatorOptions[0].value,
                              });
                            }}
                            block
                            className={styles.addButton}
                          >
                            + Add Condition
                          </Button>
                        </div>
                      )}
                    </Form.List>
                  </Form.Item>
                </Card>
              );
            })}

            <Button onClick={() => add()} block className={styles.addButton}>
              + Add Case
            </Button>
          </div>
        )}
      </Form.List>
      <Divider />
      <Form.Item
        label={'ELSE'}
        name={[SwitchElseTo]}
        className={styles.elseCase}
      >
        <Select
          allowClear
          options={buildCategorizeToOptions(getSelectedConditionTos())}
        />
      </Form.Item>
    </Form>
  );
};

export default SwitchForm;