Newer
Older
import React, { useState, useEffect, useCallback, useMemo } from "react";
import AccessControl from "../../auth/AccessControl";
import { SimpleAccordion, ConfirmationDialog } from "@ess-ics/ce-ui-common";
import {
Button,
Typography,
Grid,
Tooltip,
CircularProgress,
Alert,
Autocomplete
} from "@mui/material";
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 = useMemo(
() => ({
csEntryHost: {
fqdn: ioc.activeDeployment.host.fqdn,
id: ioc.activeDeployment.host.csEntryId
}
}),
[ioc?.activeDeployment?.host]
);
const [hosts, getHosts, , loadingHosts] = useCSEntrySearch();
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 [open, setOpen] = useState();
const [updatedIoc, updateHost] = useUpdateActiveDeploymentHost(
ioc.id,
onError
);
useEffect(() => {
if (updatedIoc) {
getIOC();
resetTab();
}
}, [updatedIoc, getIOC, resetTab]);
useEffect(
() => getHosts({ query: transformHostQuery(`${query}`) }),
[query, getHosts]
);
const onClose = useCallback(() => {
setOpen(false);
setHost(initHost);
}, [setOpen, initHost]);
const onConfirm = useCallback(() => {
hostCSEntryId: host.csEntryHost.id
}, [updateHost, 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}
/>
<SimpleAccordion
summary="Change deployment host"
defaultExpanded
>
<Grid
container
spacing={1}
>
{error ? (
<Alert severity="error">{error}</Alert>
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
) : (
<></>
)}
<Grid
item
xs={12}
>
<Autocomplete
autoHighlight
id="host"
options={hosts.hostList}
loading={loadingHosts}
clearOnBlur={false}
value={host}
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>
)
}}
/>
)}
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>
</Grid>
</SimpleAccordion>
</AccessControl>
</>
);
}