From 47b311b9de8f08622fc3d841a18d47eafa9be4d3 Mon Sep 17 00:00:00 2001 From: Christina Jenks <christina.jenks@ess.eu> Date: Mon, 18 Sep 2023 08:35:16 +0000 Subject: [PATCH] CE-2066: replace useCreateIoc with common api --- src/api/SwaggerApi.js | 9 ---- src/components/IOC/CreateIOC/CreateIOC.js | 60 ++++++++++++++++------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/api/SwaggerApi.js b/src/api/SwaggerApi.js index 4a1c3157..269def0b 100644 --- a/src/api/SwaggerApi.js +++ b/src/api/SwaggerApi.js @@ -218,15 +218,6 @@ export function unpackIOCList(iocs) { return unpackedIOCList; } -export function useCreateIOC(onError) { - const api = useContext(apiContext); - const method = useCallAndUnpack( - (body) => api.apis.IOCs.createIoc({}, { requestBody: body }), - unpackIOC - ); - return useAsync({ fcn: method, call: false, onError: onError }); -} - export function unpackDeployment(deployment) { const d = { ...deployment }; return d; diff --git a/src/components/IOC/CreateIOC/CreateIOC.js b/src/components/IOC/CreateIOC/CreateIOC.js index 3bc4d1fd..ffa14ac9 100644 --- a/src/components/IOC/CreateIOC/CreateIOC.js +++ b/src/components/IOC/CreateIOC/CreateIOC.js @@ -1,10 +1,6 @@ -import React, { useEffect, useState } from "react"; +import React, { useEffect, useState, useContext } from "react"; import { useNavigate } from "react-router-dom"; -import { - useAllowedGitProjects, - useCreateIOC, - useNamingNames -} from "../../../api/SwaggerApi"; +import { useAllowedGitProjects, useNamingNames } from "../../../api/SwaggerApi"; import { useTypingTimer } from "../../common/SearchBoxFilter/TypingTimer"; import { RootPaper } from "@ess-ics/ce-ui-common/dist/components/common/container/RootPaper"; import { @@ -17,6 +13,20 @@ import { TextField, Typography } from "@mui/material"; +import { useAPIMethod } from "@ess-ics/ce-ui-common"; +import { apiContext } from "../../../api/DeployApi"; + +const renderErrorMessage = (error) => { + const { response, status: requestStatus, message: requestMessage } = error; + const { + body: { description }, + status: httpStatus + } = response; + + return `${httpStatus ?? requestStatus ?? "unknown"}: ${ + description ?? requestMessage ?? "An unknown error has occurred" + }`; +}; export function CreateIOC() { const navigate = useNavigate(); @@ -30,11 +40,17 @@ export function CreateIOC() { , loadingAllowedGitProjects ] = useAllowedGitProjects([]); - const [error, setError] = useState(); - const onError = (error) => { - setError(error); - }; - const [ioc, createIoc, , loading] = useCreateIOC(onError); + + const client = useContext(apiContext); + const { + value: ioc, + wrapper: createIoc, + loading, + error + } = useAPIMethod({ + fcn: client.apis.IOCs.createIoc, + call: false + }); // Return home on cancel const handleCancel = () => { @@ -53,10 +69,15 @@ export function CreateIOC() { // create the ioc on form submit const onSubmit = (event) => { event.preventDefault(); - createIoc({ - gitProjectId: gitId, - namingUuid: name ? name.uuid : undefined - }); + createIoc( + {}, + { + requestBody: { + gitProjectId: gitId, + namingUuid: name ? name.uuid : undefined + } + } + ); }; // navigate home once ioc created @@ -113,7 +134,6 @@ export function CreateIOC() { )} onChange={(event, value, reason) => { setName(value); - setError(null); }} onInputChange={(event, value, reason) => { event && onNameKeyUp(event.nativeEvent); @@ -136,7 +156,6 @@ export function CreateIOC() { }} onChange={(event, value, reason) => { setGitId(value?.id); - setError(null); }} renderInput={(params) => ( <TextField @@ -164,8 +183,11 @@ export function CreateIOC() { )} autoSelect /> - - {error ? <Alert severity="error">{error}</Alert> : <></>} + {error ? ( + <Alert severity="error">{renderErrorMessage(error)}</Alert> + ) : ( + <></> + )} <Stack direction="row" justifyContent="flex-end" -- GitLab