Newer
Older
import {
useState,
useEffect,
useCallback,
useMemo,
import { ConfirmationDialog } from "@ess-ics/ce-ui-common";

Christina Jenks
committed
import {
Button,
Typography,
Grid,
Tooltip,
CircularProgress,
Autocomplete
} from "@mui/material";
import { AccessControl } from "../../auth/AccessControl";
import { useTypingTimer } from "../../../hooks/useTypingTimer";
import {
HostInfoWithId,
IocDetails,
useLazyListHostsQuery
} from "../../../store/deployApi";
import { useUpdateActiveDeploymentHostMutation } from "../../../store/enhancedApi";
import { ApiAlertError } from "../../common/Alerts/ApiAlertError";
interface ChangeHostAdminProps {
ioc: IocDetails;
buttonDisabled: boolean;
setButtonDisabled: Dispatch<SetStateAction<boolean>>;
}
export const ChangeHostAdmin = ({
ioc,
buttonDisabled,
setButtonDisabled
}: ChangeHostAdminProps) => {
const initHost = useMemo(
() => ({
fqdn: ioc?.activeDeployment?.host?.fqdn,
hostId: ioc?.activeDeployment?.host?.hostId
const [host, setHost] = useState<HostInfoWithId | null>(initHost);
const [open, setOpen] = useState(false);
const { value: query, onKeyUp: onHostKeyUp } = useTypingTimer({
interval: 500
});

Christina Jenks
committed
const [getHosts, { data: hosts, isLoading: loadingHosts }] =
useLazyListHostsQuery();

Christina Jenks
committed
const [updateHost, { data: updatedIoc, error: updateHostError }] =
useUpdateActiveDeploymentHostMutation();
const noModification = useCallback(
() => !host || host?.hostId === ioc?.activeDeployment?.host?.hostId,
const onClose = useCallback(() => {
setOpen(false);
setHost(initHost);
}, [setOpen, initHost]);

Christina Jenks
committed
const onConfirm = useCallback(() => {
if (ioc.id) {
setButtonDisabled(true);
updateHost({
iocId: ioc.id,
updateHostRequest: {
hostId: host?.hostId
}
});
}
}, [updateHost, ioc, host?.hostId, setButtonDisabled]);

Christina Jenks
committed
useEffect(() => {
if (updateHostError) {

Christina Jenks
committed
}
useEffect(() => {
if (updatedIoc) {
// Schema change related. Wait until all files have been migrated
fqdn: updatedIoc?.activeDeployment?.host?.fqdn,
hostId: updatedIoc?.activeDeployment?.host?.hostId
}, [updatedIoc, setButtonDisabled]);

Christina Jenks
committed
useEffect(() => {
getHosts({ text: `${query}` });

Christina Jenks
committed
}, [query, getHosts]);
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={() => <></>}
>
title={
<Typography
variant="h2"
marginY={1}
>
Modifying deployment host
</Typography>
}
content={
<>
<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}
>
<Grid
item
xs={12}
>
<Autocomplete
autoHighlight
id="host"
loading={loadingHosts}
clearOnBlur={false}
value={host}
getOptionLabel={(option) => {
return option?.fqdn ?? "";
}}
renderInput={(params) => (
<TextField
{...params}
label="host"
variant="outlined"
required
InputProps={{
...params.InputProps,
endAdornment: (
{loadingHosts ? (
<CircularProgress
color="inherit"
size={20}
/>
) : null}
{params.InputProps.endAdornment}
onChange={(_event, value: HostInfoWithId | null) => {
onHostKeyUp(event as React.KeyboardEvent<HTMLInputElement>);
/>
</Grid>
<Grid
item
xs={12}
>
<Box sx={{ marginBottom: 1 }}>
</Box>
)}
<Tooltip title={disabledButtonTitle}>
<span>
<Button
color="primary"
variant="contained"
onClick={() => setOpen(true)}
disabled={
buttonDisabled ||
ioc.operationInProgress ||
!ioc.activeDeployment ||
noModification()
}
>
CHANGE HOST
</Button>
</span>
</Tooltip>