diff --git a/src/api/DeployApi.js b/src/api/DeployApi.js index 641fe78e403e4ba14ce786175f1ce168a02e97ad..ac9d3b235ff111f85c083c02b03baca96a29f94c 100644 --- a/src/api/DeployApi.js +++ b/src/api/DeployApi.js @@ -21,7 +21,19 @@ export const apiContext = createContext(null); const apiOptions = { url: `${window.API_BASE_ENDPOINT}`, - server: `${window.SERVER_ADDRESS}` + server: `${window.SERVER_ADDRESS}`, + // Workaround for https://github.com/swagger-api/swagger-js/issues/1022 + // Empty body POST requests are sent with Content-Type text/plaintext + // instead of no content type, or application/json + requestInterceptor: (req) => { + req.headers["Content-Type"] = "application/json"; + req.headers.accept = "application/json"; + window.req = req; + return req; + }, + responseInterceptor: (res) => { + return res; + } }; export function DeployAPIProvider({ children }) { diff --git a/src/api/SwaggerApi.js b/src/api/SwaggerApi.js index 7a445d0ce338f9a38ae42ce1808691d64d400766..5de619f3aefacff0e31fc607c8ad3368e6dad54d 100644 --- a/src/api/SwaggerApi.js +++ b/src/api/SwaggerApi.js @@ -241,26 +241,6 @@ export function useJobLogById() { return useAsync({ fcn: method, call: false, init: null }); } -export function useUpdateAndDeployIoc(id, onError) { - const api = useContext(apiContext); - const method = useCallAndUnpack( - (body) => - api.apis.IOCs.updateAndDeployIoc({ ioc_id: id }, { requestBody: body }), - unpackDeployment - ); - return useAsync({ fcn: method, call: false, onError: onError }); -} - -export function useCreateUndeployment(id, onError) { - const api = useContext(apiContext); - const method = useCallAndUnpack( - (body) => - api.apis.IOCs.createUndeployment({ ioc_id: id }, { requestBody: body }), - unpackDeployment - ); - return useAsync({ fcn: method, call: false, onError: onError }); -} - export function unpackHost(host) { return { ...host }; } diff --git a/src/components/IOC/DeployIOC/DeployIOC.js b/src/components/IOC/DeployIOC/DeployIOC.js index f59b47f55f521e15d535b02c30f09ce82733fa6f..83c29376b7154c2642a43cd644bb1c29602232c1 100644 --- a/src/components/IOC/DeployIOC/DeployIOC.js +++ b/src/components/IOC/DeployIOC/DeployIOC.js @@ -1,23 +1,36 @@ -import React, { useContext, useState } from "react"; +import React, { useContext, useState, useEffect } from "react"; import { Navigate } from "react-router-dom"; import { IOCDeployDialog } from "../IOCDeployDialog"; import { notificationContext } from "../../common/notification/Notifications"; +import { apiContext } from "../../../api/DeployApi"; +import { useAPIMethod } from "@ess-ics/ce-ui-common"; // Process component export function DeployIOC({ open, setOpen, submitCallback, - hook, + iocId, hasActiveDeployment, init = {} }) { - function onError(message) { - setError(message); - } - - const [deployment, action] = hook(onError); const [error, setError] = useState(); + const client = useContext(apiContext); + const { + value: deployment, + wrapper: action, + error: deployError + } = useAPIMethod({ + fcn: client.apis.IOCs.updateAndDeployIoc, + call: false + }); + + useEffect(() => { + if (deployError) { + setError(deployError?.message); + } + }, [deployError]); + const { watchDeployment } = useContext(notificationContext); if (!deployment) { @@ -25,6 +38,7 @@ export function DeployIOC({ <IOCDeployDialog open={open} setOpen={setOpen} + iocId={iocId} submitCallback={action} init={init} hasActiveDeployment={hasActiveDeployment} diff --git a/src/components/IOC/IOCDeployDialog/IOCDeployDialog.js b/src/components/IOC/IOCDeployDialog/IOCDeployDialog.js index ac33d4e5ee303dec93e185dc327fa81ad0c00aca..55c81f0232a68e6b608871dbcc56151b569a7f0e 100644 --- a/src/components/IOC/IOCDeployDialog/IOCDeployDialog.js +++ b/src/components/IOC/IOCDeployDialog/IOCDeployDialog.js @@ -19,6 +19,7 @@ import { formatDate, transformHostQuery } from "../../common/Helper"; export function IOCDeployDialog({ open, setOpen, + iocId, submitCallback, hasActiveDeployment, init = {}, @@ -58,15 +59,22 @@ export function IOCDeployDialog({ const { git: gitText } = event.currentTarget.elements; const git = gitText.value; - submitCallback({ - sourceUrl: git, - sourceVersion: gitVersion, - hostCSEntryId: host - ? Number(host.csEntryHost.id) - : init.csEntryHost - ? init.csEntryHost.id - : undefined - }); + submitCallback( + { + ioc_id: iocId + }, + { + requestBody: { + sourceUrl: git, + sourceVersion: gitVersion, + hostCSEntryId: host + ? Number(host.csEntryHost.id) + : init.csEntryHost + ? init.csEntryHost.id + : undefined + } + } + ); }; // Creates, and formats tags/commitIds with dates into a table-format for using it in autocomplete diff --git a/src/components/IOC/IOCManage/IOCManage.js b/src/components/IOC/IOCManage/IOCManage.js index 8fd33debf2707041743fc91e98895c16eeceae60..d2b8f3f7a1a09d132d4e91361170e393b5799b63 100644 --- a/src/components/IOC/IOCManage/IOCManage.js +++ b/src/components/IOC/IOCManage/IOCManage.js @@ -9,10 +9,6 @@ import React, { import { IOCDetails } from "../IOCDetails"; import { DeployIOC } from "../DeployIOC"; import { UndeployIOC } from "../UndeployIOC"; -import { - useUpdateAndDeployIoc, - useCreateUndeployment -} from "../../../api/SwaggerApi"; import { userContext, SimpleAccordion, @@ -247,7 +243,7 @@ export function IOCManage({ setOpen={setDeployDialogOpen} submitCallback={closeDeployModal} init={formInit} - hook={useUpdateAndDeployIoc.bind(null, ioc.id)} + iocId={ioc.id} hasActiveDeployment={ioc.activeDeployment} /> <UndeployIOC @@ -255,7 +251,6 @@ export function IOCManage({ setOpen={setUndeployDialogOpen} submitCallback={closeUndeployModal} ioc={ioc} - hook={useCreateUndeployment.bind(null, ioc.id)} /> </AccessControl> </> diff --git a/src/components/IOC/IOCUndeployDialog/IOCUndeployDialog.js b/src/components/IOC/IOCUndeployDialog/IOCUndeployDialog.js index 1e2c50faaa003082ee47ff29f67eff6f03a23729..ddacda21cfde10c5d974e1a5a0500e85bf8f3b62 100644 --- a/src/components/IOC/IOCUndeployDialog/IOCUndeployDialog.js +++ b/src/components/IOC/IOCUndeployDialog/IOCUndeployDialog.js @@ -23,7 +23,9 @@ export function IOCUndeployDialog({ const onSubmit = (event) => { event.preventDefault(); - submitCallback(); + submitCallback({ + ioc_id: ioc.id + }); }; return ( diff --git a/src/components/IOC/UndeployIOC/UndeployIOC.js b/src/components/IOC/UndeployIOC/UndeployIOC.js index aec5774bc2d6d8adeca75221d99e8236c0700dfb..a098bc57bd8032f5f5798ccb718511ca87ad1e95 100644 --- a/src/components/IOC/UndeployIOC/UndeployIOC.js +++ b/src/components/IOC/UndeployIOC/UndeployIOC.js @@ -1,15 +1,29 @@ -import React, { useContext, useState } from "react"; +import React, { useContext, useState, useEffect } from "react"; import { Navigate } from "react-router-dom"; import { IOCUndeployDialog } from "../IOCUndeployDialog"; import { notificationContext } from "../../common/notification/Notifications"; +import { apiContext } from "../../../api/DeployApi"; +import { useAPIMethod } from "@ess-ics/ce-ui-common"; // Process component -export function UndeployIOC({ open, setOpen, submitCallback, hook, ioc }) { - function onError(message) { - setError(message); - } - const [deployment, action] = hook(onError); +export function UndeployIOC({ open, setOpen, submitCallback, ioc }) { const [error, setError] = useState(); + const client = useContext(apiContext); + const { + value: deployment, + wrapper: action, + error: deploymentError + } = useAPIMethod({ + fcn: client.apis.IOCs.createUndeployment, + call: false + }); + + useEffect(() => { + if (deploymentError) { + setError(deploymentError?.message); + } + }, [deploymentError]); + const { watchDeployment } = useContext(notificationContext); if (!deployment) {