From 9a3ebde100f6c5ea45a161a483bc8d6b375b1da0 Mon Sep 17 00:00:00 2001 From: Alexander Madsen <alexander.madsen@ess.eu> Date: Thu, 21 Sep 2023 10:37:25 +0000 Subject: [PATCH] CE-2072: convert useJobLogById --- src/api/SwaggerApi.js | 8 ----- .../deployments/DeploymentJobOutput.js | 33 ++++++++++++++++--- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/api/SwaggerApi.js b/src/api/SwaggerApi.js index 59444ee7..e55138b5 100644 --- a/src/api/SwaggerApi.js +++ b/src/api/SwaggerApi.js @@ -272,14 +272,6 @@ export function useJobById() { return useAsync({ fcn: method, call: false, init: null }); } -export function useJobLogById() { - const api = useContext(apiContext); - const method = useCallAndUnpack((awxJobId) => - api.apis.Deployments.fetchDeploymentJobLog({ awx_job_id: awxJobId }) - ); - return useAsync({ fcn: method, call: false, init: null }); -} - export function useUpdateAndDeployIoc(id, onError) { const api = useContext(apiContext); const method = useCallAndUnpack( diff --git a/src/components/deployments/DeploymentJobOutput.js b/src/components/deployments/DeploymentJobOutput.js index 3d93d150..6356477e 100644 --- a/src/components/deployments/DeploymentJobOutput.js +++ b/src/components/deployments/DeploymentJobOutput.js @@ -1,12 +1,37 @@ -import React, { useCallback, useRef, useEffect } from "react"; +import React, { + useCallback, + useRef, + useEffect, + useContext, + useMemo +} from "react"; import { LinearProgress, Typography, Container, Stack } from "@mui/material"; -import { useJobLogById } from "../../api/SwaggerApi"; import { Console } from "../common/Console/Console"; import { useSafePolling } from "../../hooks/Polling"; +import { apiContext } from "../../api/DeployApi"; +import { useAPIMethod } from "@ess-ics/ce-ui-common"; const LOG_POLL_INTERVAL = 5000; export function DeploymentJobOutput({ deploymentJob }) { - const [log, getLogById /* reset*/, , logLoading] = useJobLogById(); + const client = useContext(apiContext); + + const params = useMemo( + () => ({ + awx_job_id: deploymentJob?.id + }), + [deploymentJob] + ); + + const { + value: log, + wrapper: getLogById, + loading: logLoading, + dataReady: logDataReady + } = useAPIMethod({ + fcn: client.apis.Deployments.fetchDeploymentJobLog, + params + }); + const finalResultsNeeded = useRef(true); useEffect(() => { @@ -19,7 +44,7 @@ export function DeploymentJobOutput({ deploymentJob }) { finalResultsNeeded.current = !deploymentJob.finished; } }, [deploymentJob.finished, deploymentJob.id, getLogById]); - useSafePolling(getLog, logLoading, LOG_POLL_INTERVAL); + useSafePolling(getLog, logLoading || !logDataReady, LOG_POLL_INTERVAL); const dataReady = useCallback(() => { return deploymentJob?.started; -- GitLab