diff --git a/src/api/SwaggerApi.js b/src/api/SwaggerApi.js index e2421451d03c02836676ed1df4d2d8cc1ecc742a..bc285ab1ca23508eeef5f7e9666acd5f61a0f5fe 100644 --- a/src/api/SwaggerApi.js +++ b/src/api/SwaggerApi.js @@ -245,20 +245,6 @@ export function unpackRecord(record) { return { ...record }; } -export function useStartIOC(id, onError) { - const api = useContext(apiContext); - const method = useCallAndUnpack(api.apis.IOCs.startIoc); - const boundMethod = useCallback(method.bind(null, { ioc_id: id }), [id]); - return useAsync({ fcn: boundMethod, call: false, onError: onError }); -} - -export function useStopIOC(id, onError) { - const api = useContext(apiContext); - const method = useCallAndUnpack(api.apis.IOCs.stopIoc); - const boundMethod = useCallback(method.bind(null, { ioc_id: id }), [id]); - return useAsync({ fcn: boundMethod, call: false, onError: onError }); -} - export function useRenewToken() { const api = useContext(apiContext); const method = useCallAndUnpack(api.apis.Authentication.tokenRenew); diff --git a/src/components/IOC/IOCService/IOCService.js b/src/components/IOC/IOCService/IOCService.js index 9f3bfab787b5c474ed0c2f89155cb1f78c97d8eb..e4877b582eb2231dae554ea410fb27c4af3dd6d5 100644 --- a/src/components/IOC/IOCService/IOCService.js +++ b/src/components/IOC/IOCService/IOCService.js @@ -6,14 +6,20 @@ import { Tooltip } from "@mui/material"; import { styled } from "@mui/material/styles"; -import React, { useState, useEffect, useContext, useCallback } from "react"; -import { useStartIOC, useStopIOC } from "../../../api/SwaggerApi"; -import { ConfirmationDialog } from "@ess-ics/ce-ui-common"; +import React, { + useState, + useEffect, + useContext, + useCallback, + useMemo +} from "react"; +import { ConfirmationDialog, useAPIMethod } from "@ess-ics/ce-ui-common"; import { notificationContext } from "../../common/notification/Notifications"; import Alert from "@mui/material/Alert"; import { initRequestParams } from "../../common/Helper"; import AccessControl from "../../auth/AccessControl"; import { useNavigate } from "react-router-dom"; +import { apiContext } from "../../../api/DeployApi"; const StartButton = styled(Button)(({ theme }) => ({ backgroundColor: theme.palette.essGrass.main, @@ -50,19 +56,49 @@ export function IOCService({ }) { const [error, setError] = useState(); const navigate = useNavigate(); - - function onError(message) { - setError(message); - } - - const [startJob, startIOC, resetStartJob] = useStartIOC(ioc.id, onError); - const [stopJob, stopIOC, resetStopJob] = useStopIOC(ioc.id, onError); const [inProgress, setInProgress] = useState(false); const [startModalOpen, setStartModalOpen] = useState(false); const [stopModalOpen, setStopModalOpen] = useState(false); const [command, setCommand] = useState(null); const { watchCommand } = useContext(notificationContext); + const client = useContext(apiContext); + + const params = useMemo( + () => ({ + ioc_id: ioc?.id + }), + [ioc?.id] + ); + + const { + value: startJob, + wrapper: startIOC, + error: startJobError + } = useAPIMethod({ + fcn: client.apis.IOCs.startIoc, + call: false, + params + }); + + const { + value: stopJob, + wrapper: stopIOC, + error: stopJobError + } = useAPIMethod({ + fcn: client.apis.IOCs.stopIoc, + call: false, + params + }); + + useEffect(() => { + setError(startJobError?.message); + }, [startJobError]); + + useEffect(() => { + setError(stopJobError?.message); + }, [stopJobError]); + useEffect(() => { if (startJob && (!command || command.id !== startJob.id)) { watchCommand(startJob.id); @@ -80,10 +116,8 @@ export function IOCService({ const resetUI = useCallback(() => { setCommand(null); - resetStartJob(); - resetStopJob(); setError(null); - }, [resetStartJob, resetStopJob]); + }, []); const start = useCallback(() => { resetUI();