diff --git a/src/api/SwaggerApi.js b/src/api/SwaggerApi.js index 5dd3f4220621ba758539e84ba573c499a844d889..7a445d0ce338f9a38ae42ce1808691d64d400766 100644 --- a/src/api/SwaggerApi.js +++ b/src/api/SwaggerApi.js @@ -520,20 +520,6 @@ export function unpackUpdateIoc(updateIoc) { return d; } -export function unpackUndeployInDb(deployment) { - return { ...deployment }; -} - -export function useUndeployInDb(id, onError) { - const api = useContext(apiContext); - const method = useCallAndUnpack( - api.apis.Deployments.unDeployInDb, - unpackUndeployInDb - ); - const boundMethod = useCallback(method.bind(null, { ioc_id: id }), [id]); - return useAsync({ fcn: boundMethod, call: false, onError: onError }); -} - export function unpackUpdateActiveDeploymentHost(ioc) { return { ...ioc }; } diff --git a/src/components/IOC/AdministerUndeployment/AdministerUndeployment.js b/src/components/IOC/AdministerUndeployment/AdministerUndeployment.js index f808af1504442060c97e8e0ed472eb00b11a1243..c6f175f699f9769e4389f8f00de35656c47e5aa2 100644 --- a/src/components/IOC/AdministerUndeployment/AdministerUndeployment.js +++ b/src/components/IOC/AdministerUndeployment/AdministerUndeployment.js @@ -1,24 +1,55 @@ -import React, { useState, useEffect, useCallback } from "react"; +import React, { + useState, + useEffect, + useCallback, + useContext, + useMemo +} from "react"; import { useNavigate } from "react-router-dom"; -import { Button, Typography, Grid, Tooltip } from "@mui/material"; -import { useUndeployInDb } from "../../../api/SwaggerApi"; +import { + Button, + Typography, + Grid, + Tooltip, + LinearProgress +} from "@mui/material"; import { SimpleAccordion, ConfirmationDialog } from "@ess-ics/ce-ui-common"; import Alert from "@mui/material/Alert"; import AccessControl from "../../auth/AccessControl"; import { IocActiveDeployment } from "../../../api/DataTypes"; +import { apiContext } from "../../../api/DeployApi"; +import { useAPIMethod } from "@ess-ics/ce-ui-common/dist/hooks/API"; export default function AdministerUndeployment({ ioc, buttonDisabled }) { const navigate = useNavigate(); - function onError(message) { - setError(message); - } - // for the dialog const [error, setError] = useState(); const [open, setOpen] = useState(false); - const [response, undeployIOC] = useUndeployInDb(ioc.id, onError); + const client = useContext(apiContext); + + const params = useMemo( + () => ({ + ioc_id: ioc?.id + }), + [ioc] + ); + + const { + value: response, + wrapper: undeployIOC, + loading, + error: errorResponse + } = useAPIMethod({ + fcn: client.apis.IOCs.unDeployInDb, + call: false, + params + }); + + useEffect(() => { + setError(errorResponse?.message); + }, [errorResponse]); const onClose = useCallback(() => { setOpen(false); @@ -30,22 +61,22 @@ export default function AdministerUndeployment({ ioc, buttonDisabled }) { useEffect(() => { if (response) { + setOpen(false); navigate(-1); } }, [response, navigate]); - const failedDeployment = new IocActiveDeployment( + const hasDeployment = new IocActiveDeployment( ioc.activeDeployment - ).failedDeployment(); + ).hasDeployment(); let disabledButtonTitle = ""; if (buttonDisabled || ioc.operationInProgress) { disabledButtonTitle = "There is an ongoing operation, you cannot 'undeploy' the IOC right now"; } else { - if (!failedDeployment) { - disabledButtonTitle = - "IOC doesn't have failed (un)deployment, you cannot 'undeploy' it"; + if (!hasDeployment) { + disabledButtonTitle = "IOC is not deployed"; } } @@ -70,6 +101,7 @@ export default function AdministerUndeployment({ ioc, buttonDisabled }) { </Typography>{" "} ? </Typography> + {loading ? <LinearProgress /> : null} </> } confirmText="Undeploy IOC" @@ -108,7 +140,8 @@ export default function AdministerUndeployment({ ioc, buttonDisabled }) { disabled={ buttonDisabled || ioc.operationInProgress || - !failedDeployment + !hasDeployment || + loading } color="error" variant="contained"