From 0c55e39e8c6e89e2cfb13d08e09b94ba52fea5bc Mon Sep 17 00:00:00 2001 From: Cesar N <11819101+cesnietor@users.noreply.github.com> Date: Thu, 18 Jan 2024 16:03:01 -0800 Subject: [PATCH] Use swagger api for Add IDP Configuration (#3201) --- api/embedded_spec.go | 6 ++++ swagger.yml | 2 ++ web-app/src/api/consoleApi.ts | 1 + .../Console/IDP/AddIDPConfiguration.tsx | 33 ++++++++++--------- .../Console/IDP/AddIDPOpenIDConfiguration.tsx | 1 - 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/api/embedded_spec.go b/api/embedded_spec.go index 94ca01aeaa..1b210ef8da 100644 --- a/api/embedded_spec.go +++ b/api/embedded_spec.go @@ -2928,6 +2928,9 @@ func init() { } }, "post": { + "consumes": [ + "application/json" + ], "tags": [ "idp" ], @@ -12103,6 +12106,9 @@ func init() { } }, "post": { + "consumes": [ + "application/json" + ], "tags": [ "idp" ], diff --git a/swagger.yml b/swagger.yml index d28c570812..a538105dc4 100644 --- a/swagger.yml +++ b/swagger.yml @@ -3299,6 +3299,8 @@ paths: post: summary: Create IDP Configuration operationId: CreateConfiguration + consumes: + - application/json parameters: - name: type description: IDP Configuration Type diff --git a/web-app/src/api/consoleApi.ts b/web-app/src/api/consoleApi.ts index 3a6166c04a..ece9b190cb 100644 --- a/web-app/src/api/consoleApi.ts +++ b/web-app/src/api/consoleApi.ts @@ -5091,6 +5091,7 @@ export class Api< method: "POST", body: body, secure: true, + type: ContentType.Json, format: "json", ...params, }), diff --git a/web-app/src/screens/Console/IDP/AddIDPConfiguration.tsx b/web-app/src/screens/Console/IDP/AddIDPConfiguration.tsx index 2f2055d36e..5056cf0d12 100644 --- a/web-app/src/screens/Console/IDP/AddIDPConfiguration.tsx +++ b/web-app/src/screens/Console/IDP/AddIDPConfiguration.tsx @@ -26,7 +26,6 @@ import { Switch, } from "mds"; import { useNavigate } from "react-router-dom"; -import { ErrorResponseHandler } from "../../../common/types"; import { useAppDispatch } from "../../../store"; import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary"; import { @@ -34,9 +33,11 @@ import { setHelpName, setServerNeedsRestart, } from "../../../systemSlice"; -import useApi from "../Common/Hooks/useApi"; import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper"; import HelpMenu from "../HelpMenu"; +import { api } from "api"; +import { ApiError, HttpResponse, SetIDPResponse } from "api/consoleApi"; +import { errorToHandler } from "api/errors"; type AddIDPConfigurationProps = { classes?: any; @@ -46,7 +47,6 @@ type AddIDPConfigurationProps = { title: string; backLink: string; formFields: object; - endpoint: string; }; const AddIDPConfiguration = ({ @@ -56,7 +56,6 @@ const AddIDPConfiguration = ({ backLink, title, formFields, - endpoint, }: AddIDPConfigurationProps) => { const extraFormFields = { name: { @@ -76,16 +75,7 @@ const AddIDPConfiguration = ({ const dispatch = useAppDispatch(); const [fields, setFields] = useState({}); - - const onSuccess = (res: any) => { - navigate(backLink); - dispatch(setServerNeedsRestart(res.restart === true)); - }; - - const onError = (err: ErrorResponseHandler) => - dispatch(setErrorSnackMessage(err)); - - const [loading, invokeApi] = useApi(onSuccess, onError); + const [loadingCreate, setLoadingCreate] = useState(false); const validSave = () => { for (const [key, value] of Object.entries(extraFormFields)) { @@ -108,6 +98,7 @@ const AddIDPConfiguration = ({ }; const addRecord = (event: React.FormEvent) => { + setLoadingCreate(true); event.preventDefault(); const name = fields["name"]; let input = ""; @@ -116,7 +107,17 @@ const AddIDPConfiguration = ({ input += `${key}=${fields[key]} `; } } - invokeApi("POST", endpoint, { name, input }); + + api.idp + .createConfiguration("openid", { name, input }) + .then((res: HttpResponse) => { + navigate(backLink); + dispatch(setServerNeedsRestart(res.data.restart === true)); + }) + .catch(async (res: HttpResponse) => { + dispatch(setErrorSnackMessage(errorToHandler(res.error))); + }) + .finally(() => setLoadingCreate(false)); }; const renderFormField = (key: string, value: any) => { @@ -197,7 +198,7 @@ const AddIDPConfiguration = ({ type="submit" variant="callAction" color="primary" - disabled={loading || !validSave()} + disabled={loadingCreate || !validSave()} label={"Save"} /> diff --git a/web-app/src/screens/Console/IDP/AddIDPOpenIDConfiguration.tsx b/web-app/src/screens/Console/IDP/AddIDPOpenIDConfiguration.tsx index ae101c90e9..5d7ce1ba4d 100644 --- a/web-app/src/screens/Console/IDP/AddIDPOpenIDConfiguration.tsx +++ b/web-app/src/screens/Console/IDP/AddIDPOpenIDConfiguration.tsx @@ -38,7 +38,6 @@ const AddIDPOpenIDConfiguration = () => { header={"OpenID Configurations"} backLink={IAM_PAGES.IDP_OPENID_CONFIGURATIONS} title={"Create OpenID Configuration"} - endpoint={"/api/v1/idp/openid/"} formFields={openIDFormFields} /> );