-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathindex.tsx
More file actions
397 lines (371 loc) · 11.9 KB
/
index.tsx
File metadata and controls
397 lines (371 loc) · 11.9 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import {
Box,
Button,
Divider,
makeStyles,
MenuItem,
TextField,
Typography,
FormControl,
InputLabel,
Select,
Stepper,
Step,
StepLabel,
StepContent,
} from "@material-ui/core";
import { FC, memo, useCallback, useState, useEffect, useMemo } from "react";
import { APPLICATION_KIND_TEXT } from "~/constants/application-kind";
import { UI_TEXT_CANCEL, UI_TEXT_SAVE } from "~/constants/ui-text";
import { useAppSelector, useAppDispatch, unwrapResult } from "~/hooks/redux";
import {
selectAllUnregisteredApplications,
fetchUnregisteredApplications,
ApplicationInfo,
} from "~/modules/unregistered-applications";
import {
addApplication,
ApplicationGitRepository,
} from "~/modules/applications";
import { sortFunc } from "~/utils/common";
import { selectAllPipeds } from "~/modules/pipeds";
import { Autocomplete } from "@material-ui/lab";
import DialogConfirm from "~/components/dialog-confirm";
const ADD_FROM_GIT_CONFIRM_DIALOG_TITLE = "Add Application";
const ADD_FROM_GIT_CONFIRM_DIALOG_DESCRIPTION =
"Are you sure you want to add the application?";
const useStyles = makeStyles((theme) => ({
title: {
padding: theme.spacing(2),
},
textInput: {
flex: 1,
},
inputGroup: {
display: "flex",
gap: theme.spacing(2),
},
inputGroupSpace: {
width: theme.spacing(3),
},
formItem: {
width: "100%",
marginTop: theme.spacing(4),
},
select: {
width: "100%",
},
applicationDetail: {
width: "100%",
},
actionButtons: {
paddingLeft: theme.spacing(2),
},
}));
enum STEP {
SELECT_PIPED,
SELECT_APPLICATION,
CONFIRM_INFORMATION,
}
type DeployTargetOption = {
pluginName: string;
deployTarget: string;
value: string;
};
type Props = {
title: string;
onClose: () => void;
onFinished: () => void;
};
const ApplicationFormSuggestionV1: FC<Props> = ({
title,
onClose,
onFinished: onAdded,
}) => {
const [loading, setLoading] = useState(false);
const [activeStep, setActiveStep] = useState(STEP.SELECT_PIPED);
const [showConfirm, setShowConfirm] = useState(false);
const [selectedPipedId, setSelectedPipedId] = useState("");
const [selectedDeployTargets, setSelectedDeployTargets] = useState<
DeployTargetOption[]
>([]);
const [
selectedApp,
setSelectedApp,
] = useState<ApplicationInfo.AsObject | null>(null);
const [appToAdd, setAppToAdd] = useState({
name: "",
pipedId: "",
repo: {} as ApplicationGitRepository.AsObject,
repoPath: "",
configFilename: "",
labels: new Array<[string, string]>(),
deployTargets: new Array<DeployTargetOption>(),
});
const dispatch = useAppDispatch();
const classes = useStyles();
useEffect(() => {
dispatch(fetchUnregisteredApplications());
}, [dispatch]);
const apps = useAppSelector(selectAllUnregisteredApplications);
const ps = useAppSelector(selectAllPipeds);
const selectedPiped = useMemo(
() => ps.find((piped) => piped.id === selectedPipedId),
[ps, selectedPipedId]
);
const deployTargetOptions = useMemo(() => {
if (!selectedPiped) return [];
if (selectedPiped.pluginsList.length === 0) return [];
return selectedPiped.pluginsList.reduce((all, plugin) => {
plugin.deployTargetsList.forEach((deployTarget) => {
all.push({
deployTarget,
pluginName: plugin.name,
value: `${deployTarget} - ${plugin.name}`,
});
});
return all;
}, [] as DeployTargetOption[]);
}, [selectedPiped]);
const filteredApps = useMemo(
() =>
apps
.filter((app) => app.pipedId === selectedPipedId)
.sort((a, b) => sortFunc(a.name, b.name)),
[apps, selectedPipedId]
);
const pipedOptions = useMemo(() => {
return ps
.filter((piped) => !piped.disabled)
.sort((a, b) => sortFunc(a.name, b.name));
}, [ps]);
/**
* Auto change step based on selectedApp and selectedPipedId
*/
useEffect(() => {
if (selectedApp) {
setActiveStep(STEP.CONFIRM_INFORMATION);
return;
}
if (selectedPipedId && selectedDeployTargets.length > 0) {
setActiveStep(STEP.SELECT_APPLICATION);
return;
}
setActiveStep(STEP.SELECT_PIPED);
}, [selectedApp, selectedDeployTargets.length, selectedPipedId]);
/**
* Init selectedPipedId if there is only one piped
*/
useEffect(() => {
if (pipedOptions.length === 1 && !selectedApp) {
setSelectedPipedId(pipedOptions[0].id);
}
}, [pipedOptions, selectedApp]);
/**
* Init selectedApp if there is only one app
*/
useEffect(() => {
if (filteredApps.length === 1 && !selectedApp) {
setSelectedApp(filteredApps[0]);
}
}, [apps.length, filteredApps, filteredApps.length, selectedApp]);
const onSelectPiped = useCallback((pipedId: string) => {
setSelectedPipedId(pipedId);
setSelectedDeployTargets([]);
setSelectedApp(null);
}, []);
const onSelectDeployTargets = useCallback((targets: DeployTargetOption[]) => {
setSelectedDeployTargets(targets);
setSelectedApp(null);
}, []);
const onSubmitForm = (): void => {
if (!selectedApp) return;
setAppToAdd({
name: selectedApp.name,
pipedId: selectedApp.pipedId,
repo: { id: selectedApp.repoId } as ApplicationGitRepository.AsObject,
repoPath: selectedApp.path,
configFilename: selectedApp.configFilename,
labels: selectedApp.labelsMap,
deployTargets: selectedDeployTargets,
});
setShowConfirm(true);
};
const onCreateApplication = (): void => {
setLoading(true);
dispatch(addApplication(appToAdd))
.then(unwrapResult)
.then(() => {
onAdded();
})
.finally(() => {
setLoading(true);
setShowConfirm(false);
});
};
return (
<>
<Box width="100%">
<Typography className={classes.title} variant="h6">
{title}
</Typography>
<Divider />
<Stepper activeStep={activeStep} orientation="vertical">
<Step key="Select piped and deploy targets" active>
<StepLabel>Select piped and deploy targets</StepLabel>
<StepContent>
<div className={classes.inputGroup}>
<FormControl className={classes.formItem} variant="outlined">
<InputLabel id="filter-piped">Piped</InputLabel>
<Select
labelId="filter-piped"
id="filter-piped"
label="Piped"
value={selectedPipedId}
className={classes.select}
defaultValue={""}
onChange={(e) => {
onSelectPiped(e.target.value as string);
}}
>
{pipedOptions.map((e) => (
<MenuItem value={e.id} key={`piped-${e.id}`}>
{e.name} ({e.id})
</MenuItem>
))}
</Select>
</FormControl>
<FormControl className={classes.formItem} variant="outlined">
<Autocomplete
id="deploy-targets"
options={deployTargetOptions.map(({ value }) => value)}
multiple={true}
value={selectedDeployTargets.map((item) => item.value)}
disabled={!selectedPipedId}
onChange={(_e, value) => {
const selected = deployTargetOptions.filter((item) =>
value.includes(item.value)
);
onSelectDeployTargets(selected);
}}
openOnFocus
autoComplete={false}
noOptionsText="No deploy targets found"
renderInput={(params) => (
<TextField
{...params}
label="Deploy targets"
variant="outlined"
/>
)}
/>
</FormControl>
</div>
</StepContent>
</Step>
<Step key="Select application to add" expanded={activeStep !== 0}>
<StepLabel>Select application to add</StepLabel>
<StepContent>
<FormControl className={classes.formItem} variant="outlined">
<Autocomplete
id="filter-app"
options={filteredApps}
getOptionLabel={(app) =>
`name: ${app.name}, repo: ${app.repoId}`
}
value={selectedApp}
onChange={(_e, value) => {
setSelectedApp(value || null);
}}
openOnFocus
renderInput={(params) => (
<TextField
{...params}
label="Application"
variant="outlined"
/>
)}
/>
</FormControl>
</StepContent>
</Step>
<Step key="Confirm information before adding">
<StepLabel>Confirm information before adding</StepLabel>
<StepContent>
{selectedApp && (
<Typography className={classes.applicationDetail}>
<div className={classes.inputGroup}>
<TextField
id={"kind"}
label="Kind"
margin="dense"
fullWidth
variant="outlined"
value={APPLICATION_KIND_TEXT[selectedApp.kind]}
className={classes.textInput}
inputProps={{ readOnly: true }}
/>
</div>
<div className={classes.inputGroup}>
<TextField
id={"path"}
label="Path"
margin="dense"
variant="outlined"
value={selectedApp.path}
className={classes.textInput}
inputProps={{ readOnly: true }}
/>
<TextField
id={"configFilename-"}
label="Config Filename"
margin="dense"
variant="outlined"
value={selectedApp.configFilename}
className={classes.textInput}
inputProps={{ readOnly: true }}
/>
</div>
{selectedApp.labelsMap.map((label, index) => (
<div className={classes.inputGroup} key={label[0]}>
<TextField
id={"label-" + "-" + index}
label={"Label " + index}
margin="dense"
variant="outlined"
value={label[0] + ": " + label[1]}
className={classes.textInput}
inputProps={{ readOnly: true }}
/>
</div>
))}
</Typography>
)}
</StepContent>
</Step>
</Stepper>
<Box className={classes.actionButtons}>
<Button
color="primary"
type="submit"
onClick={onSubmitForm}
disabled={!selectedApp}
>
{UI_TEXT_SAVE}
</Button>
<Button onClick={onClose}>{UI_TEXT_CANCEL}</Button>
</Box>
</Box>
<DialogConfirm
open={showConfirm}
onClose={() => setShowConfirm(false)}
onCancel={() => setShowConfirm(false)}
title={ADD_FROM_GIT_CONFIRM_DIALOG_TITLE}
description={ADD_FROM_GIT_CONFIRM_DIALOG_DESCRIPTION}
onConfirm={onCreateApplication}
loading={loading}
/>
</>
);
};
export default memo(ApplicationFormSuggestionV1);