diff --git a/src/api/SwaggerApi.js b/src/api/SwaggerApi.js index 454a50ceb892e00cdf1788020974ac8eab40e0d1..43323989d848dcf02d540566bc5285494522b3be 100644 --- a/src/api/SwaggerApi.js +++ b/src/api/SwaggerApi.js @@ -341,22 +341,6 @@ export function useAllowedGitProjects() { return useAsync({ fcn: method, call: false, init: [] }); } -export function useDeleteIOC(id, onError) { - const api = useContext(apiContext); - const method = useCallAndUnpack( - api.apis.IOCs.deleteIoc, - unpackDeleteIocResponse - ); - const boundMethod = useCallback(method.bind(null, { ioc_id: id }), [id]); - return useAsync({ fcn: boundMethod, call: false, onError: onError }); -} - -export function unpackDeleteIocResponse(_, status) { - if (status === 204) { - return "Successful delete"; - } -} - export function useUpdateIoc(id, onError) { const api = useContext(apiContext); const method = useCallAndUnpack( diff --git a/src/components/IOC/IOCDelete/IOCDelete.js b/src/components/IOC/IOCDelete/IOCDelete.js index 8f49ec1f9e2f1730e661130f923ed0c27108af3d..d5b4f3c6bc0a42a4202c248565a43a19ff30a047 100644 --- a/src/components/IOC/IOCDelete/IOCDelete.js +++ b/src/components/IOC/IOCDelete/IOCDelete.js @@ -1,29 +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 { useDeleteIOC } from "../../../api/SwaggerApi"; import { SimpleAccordion, ConfirmationDialog } from "@ess-ics/ce-ui-common"; import Alert from "@mui/material/Alert"; import AccessControl from "../../auth/AccessControl"; +import { apiContext } from "../../../api/DeployApi"; +import { useAPIMethod } from "@ess-ics/ce-ui-common"; export default function IOCDelete({ ioc, buttonDisabled }) { const navigate = useNavigate(); - function onError(message) { - setError(message); - } - // for the dialog const [error, setError] = useState(); const [open, setOpen] = useState(false); - const [response, deleteIOC] = useDeleteIOC(ioc.id, onError); + const client = useContext(apiContext); + + const params = useMemo( + () => ({ + ioc_id: ioc?.id + }), + [ioc] + ); + + const { + wrapper: deleteIOC, + dataReady: dataready, + error: errorResponse + } = useAPIMethod({ + fcn: client.apis.IOCs.deleteIoc, + call: false, + params + }); + + useEffect(() => { + if (errorResponse) { + setError(errorResponse?.message); + } + }, [errorResponse, setError]); useEffect(() => { - if (response && !error) { + if (dataready && !error) { navigate(-1); } - }, [response, navigate, error]); + }, [dataready, navigate, error]); let disabledButtonTitle = "";