This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDynamicPayloadModal.tsx
More file actions
127 lines (116 loc) · 3.66 KB
/
Copy pathDynamicPayloadModal.tsx
File metadata and controls
127 lines (116 loc) · 3.66 KB
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
import { Modal, Form, Select, Input, InputNumber, Checkbox } from "antd"
import { Env, DynamicPayloadInput, DynamicPayloadInputTypeEnum } from "../models"
export interface DynamicPayloadModalProps {
visible: boolean
env: Env
onClickOk(values: any): void
onClickCancel(): void
}
export default function DynamicPayloadModal(props: DynamicPayloadModalProps): JSX.Element {
const [ form ] = Form.useForm()
const onClickOk = () => {
form.validateFields()
.then(values => {
props.onClickOk(values)
})
.catch(info => {
console.log(info)
})
}
const onClickCancel = () => {
props.onClickCancel()
}
// Build items dynamically
const items = new Array<JSX.Element>()
if (props.env.dynamicPayload) {
// Object.entries(props.env.dynamicPayload.inputs).forEach()
Object.entries(props.env.dynamicPayload.inputs).forEach((entry) => {
const [name, input] = entry
items.push(<DynamicItem key={name} name={name} input={input}/>)
})
}
// Build the initialValues
const initialValues: any = {}
if (props.env.dynamicPayload) {
Object.entries(props.env.dynamicPayload.inputs).forEach((entry) => {
const [name, input] = entry
initialValues[name] = input.default
})
}
return (
<Modal
visible={props.visible}
onOk={onClickOk}
onCancel={onClickCancel}
>
<Form
form={form}
layout="vertical"
name="dynamic_payload"
initialValues={initialValues}
>
{items.map(item => item)}
</Form>
</Modal>
)
}
interface DynamicItemProps {
name: string
input: DynamicPayloadInput
}
function DynamicItem({name, input}: DynamicItemProps): JSX.Element {
// Capitalize the first character.
const label = name.charAt(0).toUpperCase() + name.slice(1)
const description = input.description
const rules = (input.required)? [{required: true}] : []
switch (input.type) {
case DynamicPayloadInputTypeEnum.Select:
return (
<Form.Item
label={label}
name={name}
tooltip={description}
rules={rules}
>
<Select >
{input.options?.map((option: any, idx: any) =>
<Select.Option key={idx} value={option}>{option}</Select.Option>
)}
</Select>
</Form.Item>
)
case DynamicPayloadInputTypeEnum.String:
return (
<Form.Item
label={label}
name={name}
tooltip={description}
rules={rules}
>
<Input />
</Form.Item>
)
case DynamicPayloadInputTypeEnum.Number:
return (
<Form.Item
label={label}
name={name}
tooltip={description}
rules={rules}
>
<InputNumber />
</Form.Item>
)
case DynamicPayloadInputTypeEnum.Boolean:
return (
<Form.Item
label={label}
name={name}
tooltip={description}
rules={rules}
>
<Checkbox />
</Form.Item>
)
}
}