Newer
Older
import React, { useState, useRef, useEffect, useCallback } from "react";
import AccessControl from "../../auth/AccessControl";
import { SimpleAccordion } from "@ess-ics/ce-ui-common";
import {
Button,
Typography,
Grid,
Tooltip,
CircularProgress,
Alert,
Autocomplete
} from "@mui/material";
import { SimpleModal } from "../../common/SimpleModal/SimpleModal";
import { ConfirmationDialog } from "../../dialog/ConfirmationDialog";
import {
useUpdateActiveDeploymentHost,
useCSEntrySearch
} from "../../../api/SwaggerApi";
import { useTypingTimer } from "../../common/SearchBoxFilter/TypingTimer";
import { transformHostQuery } from "../../common/Helper";
export default function ChangeHostAdmin({
ioc,
getIOC,
resetTab,
buttonDisabled
}) {
const initHost = {
csEntryHost: {
fqdn: ioc.activeDeployment.host.fqdn,
id: ioc.activeDeployment.host.csEntryId
}
};
const [hosts, getHosts, , loadingHosts] = useCSEntrySearch();
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
const [host, setHost] = useState(initHost);
const [query, onHostKeyUp] = useTypingTimer({ interval: 500 });
function onError(message) {
setError(message);
}
const noModification = useCallback(
() => !host || host.csEntryHost.id === ioc.activeDeployment.host.csEntryId,
[host, ioc]
);
// for the dialog
const [error, setError] = useState();
const [adHocDialogOpen, setAdHocDialogOpen] = useState(false);
const [adHocDialogTitle, setAdHocDiatlogTitle] = useState();
const [adHocDialogDescription, setAdHocDialogDescription] = useState();
const callbackRef = useRef();
const [updatedIoc, updateHost] = useUpdateActiveDeploymentHost(
ioc.id,
onError
);
useEffect(() => {
if (updatedIoc) {
getIOC();
resetTab();
}
}, [updatedIoc, getIOC, resetTab]);
useEffect(
() => getHosts({ query: transformHostQuery(`${query}`) }),
[query, getHosts]
);
const openModifyModal = () => {
setAdHocDiatlogTitle("Modifying deployment host");
setAdHocDialogDescription(
<>
<Typography style={{ display: "inline-block" }}>
Are you sure want to modify deployment host of
</Typography>
<Typography
style={{ fontFamily: "monospace", display: "inline-block" }}
>
{" "}
{ioc.namingName}?{" "}
</Typography>
</>
);
callbackRef.current = modifyHost;
setAdHocDialogOpen(true);
};
const modifyHost = () => {
updateHost({
hostCSEntryId: host.csEntryHost.id
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
});
};
let disabledButtonTitle = "";
if (buttonDisabled || ioc.operationInProgress) {
disabledButtonTitle =
"There is an ongoing operation, you cannot 'undeploy' the IOC right now";
} else {
if (!ioc.activeDeployment) {
disabledButtonTitle = "IOC has no active deployment";
}
}
return (
<>
<AccessControl
allowedRoles={["DeploymentToolAdmin"]}
renderNoAccess={() => <></>}
>
<>
<SimpleModal
open={adHocDialogOpen}
setOpen={setAdHocDialogOpen}
>
<ConfirmationDialog
open={adHocDialogOpen}
setOpen={setAdHocDialogOpen}
title={adHocDialogTitle}
description={adHocDialogDescription}
callback={callbackRef.current}
/>
</SimpleModal>
<SimpleAccordion
summary="Change deployment host"
defaultExpanded
>
<Grid
container
spacing={1}
>
{error ? (
<Grid
item
xs={12}
>
<Alert severity="error">{error}</Alert>
</Grid>
) : (
<></>
)}
<Grid
item
xs={12}
>
<Autocomplete
autoHighlight
id="host"
options={hosts.hostList}
loading={loadingHosts}
clearOnBlur={false}
defaultValue={initHost}
getOptionLabel={(option) => {
return option?.csEntryHost?.fqdn;
}}
renderInput={(params) => (
<TextField
{...params}
label="host"
variant="outlined"
required
InputProps={{
...params.InputProps,
endAdornment: (
<React.Fragment>
{loadingHosts ? (
<CircularProgress
color="inherit"
size={20}
/>
) : null}
{params.InputProps.endAdornment}
</React.Fragment>
)
}}
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
/>
)}
onChange={(event, value, reason) => {
setHost(value);
}}
onInputChange={(event, value, reason) => {
event && onHostKeyUp(event.nativeEvent);
}}
autoSelect
filterOptions={(options, state) => options}
/>
</Grid>
<Grid
item
xs={12}
>
<Tooltip title={disabledButtonTitle}>
<span>
<Button
color="primary"
variant="contained"
onClick={openModifyModal}
disabled={
buttonDisabled ||
ioc.operationInProgress ||
!ioc.activeDeployment ||
noModification()
}
>
CHANGE HOST
</Button>
</span>
</Tooltip>
</Grid>
</Grid>
</SimpleAccordion>
</>
</AccessControl>
</>
);
}