Newer
Older

Christina Jenks
committed
import React, {
useState,
useEffect,
useCallback,
useMemo,
useContext
} from "react";
import AccessControl from "../../auth/AccessControl";
import { ConfirmationDialog, useAPIMethod } from "@ess-ics/ce-ui-common";

Christina Jenks
committed
import {
Button,
Typography,
Grid,
Tooltip,
CircularProgress,
Alert,
Autocomplete
} from "@mui/material";
import { useTypingTimer } from "../../common/SearchBoxFilter/TypingTimer";
import { transformHostQuery } from "../../common/Helper";

Christina Jenks
committed
import { apiContext } from "../../../api/DeployApi";
export default function ChangeHostAdmin({
ioc,
getIOC,
resetTab,
buttonDisabled
}) {
const initHost = useMemo(
() => ({
csEntryHost: {
fqdn: ioc.activeDeployment.host.fqdn,
id: ioc.activeDeployment.host.csEntryId
}
}),
[ioc?.activeDeployment?.host]
);
const [host, setHost] = useState(initHost);

Christina Jenks
committed
const client = useContext(apiContext);
const {
value: hosts,
wrapper: getHosts,
loading: loadingHosts
} = useAPIMethod({
fcn: client.apis.Hosts.listHosts,
call: false
});
const [query, onHostKeyUp] = useTypingTimer({ interval: 500 });
const noModification = useCallback(
() => !host || host.csEntryHost.id === ioc.activeDeployment.host.csEntryId,
[host, ioc]
);
// for the dialog
const [error, setError] = useState();
const [open, setOpen] = useState(false);

Christina Jenks
committed
const {
value: updatedIoc,
wrapper: updateHost,
error: updateHostError
} = useAPIMethod({
fcn: client.apis.IOCs.updateActiveDeploymentHost,
call: false
});
useEffect(() => {
if (updateHostError) {
setError(updateHostError?.message);
}
}, [updateHostError, setError]);
useEffect(() => {
if (updatedIoc) {
getIOC();
resetTab();
}
}, [updatedIoc, getIOC, resetTab]);

Christina Jenks
committed
useEffect(() => {
getHosts({ query: transformHostQuery(`${query}`) });
}, [query, getHosts]);
const onClose = useCallback(() => {
setOpen(false);
setHost(initHost);
}, [setOpen, initHost]);
const onConfirm = useCallback(() => {

Christina Jenks
committed
updateHost(
{
ioc_id: ioc.id
},
{
requestBody: {
hostCSEntryId: host.csEntryHost.id
}
}
);
}, [updateHost, ioc, host?.csEntryHost?.id]);
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={() => <></>}
>
<ConfirmationDialog
title="Modifying deployment host"
description={
<>
<Typography component="span">
Are you sure want to modify deployment host of
<Typography
component="span"
fontFamily="monospace"
{" "}
{ioc.namingName}
</Typography>{" "}
?
</Typography>
</>
}
confirmText="Modify Host"
cancelText="Cancel"
open={open}
onClose={onClose}
onConfirm={onConfirm}
/>
<Box sx={{ pt: 2 }}>
<Typography
sx={{ my: 2.5 }}
variant="h3"
>
Change deployment host
</Typography>
<Grid
container
spacing={1}
>
{error ? (
<Alert severity="error">{error}</Alert>
) : (
<></>
)}
<Grid
item
xs={12}
>
<Autocomplete
autoHighlight
id="host"
options={query ? hosts?.csEntryHosts ?? [] : []}
loading={loadingHosts}
clearOnBlur={false}
value={host}
getOptionLabel={(option) => {
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
}}
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>
)
}}
/>
)}
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={() => setOpen(true)}
disabled={
buttonDisabled ||
ioc.operationInProgress ||
!ioc.activeDeployment ||
noModification()
}
>
CHANGE HOST
</Button>
</span>
</Tooltip>
</AccessControl>
</>
);
}