-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathBasicInfo.tsx
266 lines (250 loc) · 7.91 KB
/
BasicInfo.tsx
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import ArrowDropDown from '@mui/icons-material/ArrowDropDown'
import ArrowDropUp from '@mui/icons-material/ArrowDropUp'
import {
Box,
Checkbox,
Divider,
FormControlLabel,
Grid,
IconButton,
Paper,
Radio,
RadioGroup,
Switch,
TextField,
Typography
} from '@mui/material'
import {makeStyles} from '@mui/styles'
import React from 'react'
import {Channel, ChannelMethod, ChannelType} from '../../types'
export function BasicInfo(props: {
channel: Channel
onChange: (event: {channel: Channel; isValid: boolean}) => unknown
}) {
const [channel, setChannel] = React.useState(props.channel)
const allowedMethods: Array<ChannelMethod> = [
'GET',
'HEAD',
'POST',
'TRACE',
'DELETE',
'CONNECT',
'PUT',
'PATCH',
'OPTIONS'
]
const [formTouched, setFormTouched] = React.useState(false)
const channelTypes: Array<ChannelType> = ['http', 'polling']
const [expandOptionalSettings, setExpandOptionalSettings] =
React.useState(false)
React.useEffect(() => {
props.onChange({
channel: channel,
isValid: !!channel.name.trim()
})
}, [channel])
const handleCheckToggle = (method: ChannelMethod, checked: boolean) => {
let methods = []
if (!channel.methods) channel.methods = []
if (checked) {
methods = [...channel.methods, method]
} else {
methods = channel.methods.filter(m => m !== method)
}
setChannel({...channel, methods: Array.from(new Set(methods))})
}
return (
<Box>
<Typography variant="h5">Basic Info</Typography>
<Typography variant="subtitle1">
Describe some basic information about the channel and choose its overall
type.
</Typography>
<Divider
style={{
marginTop: '10px',
margin: '0px',
width: '100%',
marginBottom: '10px',
overflow: 'visible'
}}
/>
<br />
<Grid container spacing={2}>
<Grid item xs={6}>
<TextField
label="Channel Name"
variant="outlined"
fullWidth
margin="normal"
value={channel.name}
onChange={e => {
setFormTouched(true)
setChannel({...channel, name: e.target.value})
}}
error={channel.name.trim() === '' && formTouched}
helperText={
channel.name.trim() === '' && formTouched
? 'Channel Name cannot be empty'
: 'Choose a short but descriptive name.'
}
/>
</Grid>
<Grid item xs={12}>
<Box style={{padding: '10px'}}>
<Typography variant="h6">Allowed Methods</Typography>
<Grid container>
{allowedMethods.map(method => (
<Grid item xs={6} key={method}>
<FormControlLabel
control={
<Checkbox
checked={channel.methods?.includes(method)}
onChange={e =>
handleCheckToggle(method, e.target.checked)
}
/>
}
label={method}
/>
</Grid>
))}
</Grid>
</Box>
</Grid>
</Grid>
<Divider
style={{
marginTop: '10px',
margin: '0px',
width: '100%',
marginBottom: '30px',
overflow: 'visible'
}}
/>
<br />
<Paper elevation={2} style={{borderRadius: '20px', padding: '12px'}}>
<Grid container>
<Grid item xs={11}>
<Typography variant="body1">Optional Settings</Typography>
</Grid>
<Grid item xs={1}>
<IconButton
onClick={() => setExpandOptionalSettings(!expandOptionalSettings)}
>
{expandOptionalSettings ? <ArrowDropUp /> : <ArrowDropDown />}
</IconButton>
</Grid>
</Grid>
{expandOptionalSettings && (
<React.Fragment>
<Divider />
<Grid container spacing={2}>
<Grid item xs={8}>
<TextField
label="Channel Description"
variant="outlined"
fullWidth
margin="normal"
value={channel.description}
onChange={e =>
setChannel({...channel, description: e.target.value})
}
/>
</Grid>
<Grid item xs={12}>
<Typography variant="h6">Channel Type</Typography>
<Box style={{padding: '10px'}}>
<RadioGroup
defaultValue="http"
value={channel.type}
onChange={e =>
setChannel({
...channel,
type: e.target.value as ChannelType
})
}
>
{channelTypes.map(type => (
<FormControlLabel
key={type}
value={type}
control={<Radio />}
label={type.toUpperCase()}
/>
))}
</RadioGroup>
</Box>
<Grid item xs={5}>
{channel.type === 'polling' && (
<TextField
label="Polling Schedule"
variant="outlined"
fullWidth
margin="normal"
type="string"
value={channel.pollingSchedule}
onChange={e =>
setChannel({
...channel,
pollingSchedule: e.target.value
})
}
error={
channel.type === 'polling' &&
channel.pollingSchedule === ''
}
helperText={
channel.type === 'polling' &&
channel.pollingSchedule === ''
? `Cron format: '*/10 * * * *'\nor Written format: '10 minutes'`
: undefined
}
/>
)}
<TextField
label="Timeout ms"
variant="outlined"
fullWidth
margin="normal"
type="number"
value={channel.timeout}
onChange={e =>
setChannel({...channel, timeout: Number(e.target.value)})
}
error={
Number.isSafeInteger(channel.timeout) &&
channel.timeout < 0
}
helperText={
Number.isSafeInteger(channel.timeout) &&
channel.timeout < 0
? 'Timeout cannot be negative'
: undefined
}
/>
</Grid>
<Grid item xs={6}>
<FormControlLabel
control={
<Switch
checked={channel.status === 'enabled'}
onChange={e =>
setChannel({
...channel,
status: e.target.checked ? 'enabled' : 'disabled'
})
}
/>
}
label="Enable channel"
/>
</Grid>
</Grid>
</Grid>
</React.Fragment>
)}
</Paper>
</Box>
)
}