Skip to content

Commit d9f945b

Browse files
authored
Use swagger api for Add KMS Key (#3202)
1 parent 0c55e39 commit d9f945b

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

web-app/src/screens/Console/KMS/AddKey.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@ import React, { Fragment, useEffect } from "react";
1818
import { BackLink, Grid } from "mds";
1919
import { useNavigate } from "react-router-dom";
2020
import { IAM_PAGES } from "../../../common/SecureComponent/permissions";
21-
import { ErrorResponseHandler } from "../../../common/types";
22-
import { setErrorSnackMessage, setHelpName } from "../../../systemSlice";
2321
import { useAppDispatch } from "../../../store";
2422
import AddKeyForm from "./AddKeyForm";
2523
import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper";
2624
import HelpMenu from "../HelpMenu";
25+
import { setHelpName } from "systemSlice";
2726

2827
const AddKey = () => {
2928
const dispatch = useAppDispatch();
3029
const navigate = useNavigate();
3130

32-
const onSuccess = () => navigate(`${IAM_PAGES.KMS_KEYS}`);
33-
34-
const onError = (err: ErrorResponseHandler) =>
35-
dispatch(setErrorSnackMessage(err));
36-
3731
useEffect(() => {
3832
dispatch(setHelpName("add_key"));
3933
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -51,7 +45,7 @@ const AddKey = () => {
5145
}
5246
actions={<HelpMenu />}
5347
/>
54-
<AddKeyForm onError={onError} onSuccess={onSuccess} />
48+
<AddKeyForm />
5549
</Grid>
5650
</Fragment>
5751
);

web-app/src/screens/Console/KMS/AddKeyForm.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,36 @@ import {
2323
Grid,
2424
InputBox,
2525
} from "mds";
26-
import { ErrorResponseHandler } from "../../../common/types";
2726
import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary";
28-
import useApi from "../Common/Hooks/useApi";
2927
import KMSHelpBox from "./KMSHelpbox";
28+
import { api } from "api";
29+
import { useAppDispatch } from "store";
30+
import { useNavigate } from "react-router-dom";
31+
import { ApiError, HttpResponse } from "api/consoleApi";
32+
import { setErrorSnackMessage } from "systemSlice";
33+
import { errorToHandler } from "api/errors";
34+
import { IAM_PAGES } from "common/SecureComponent/permissions";
3035

31-
interface IAddKeyFormProps {
32-
onSuccess: () => void;
33-
onError: (err: ErrorResponseHandler) => void;
34-
}
36+
const AddKeyForm = () => {
37+
const dispatch = useAppDispatch();
38+
const navigate = useNavigate();
3539

36-
const AddKeyForm = ({ onSuccess, onError }: IAddKeyFormProps) => {
37-
const [loading, invokeApi] = useApi(onSuccess, onError);
3840
const [keyName, setKeyName] = useState<string>("");
41+
const [loadingCreate, setLoadingCreate] = useState<boolean>(false);
3942

4043
const addRecord = (event: React.FormEvent) => {
4144
event.preventDefault();
42-
invokeApi("POST", "/api/v1/kms/keys/", { key: keyName });
45+
setLoadingCreate(true);
46+
api.kms
47+
.kmsCreateKey({ key: keyName })
48+
.then((_) => {
49+
navigate(`${IAM_PAGES.KMS_KEYS}`);
50+
})
51+
.catch(async (res: HttpResponse<void, ApiError>) => {
52+
const err = (await res.json()) as ApiError;
53+
dispatch(setErrorSnackMessage(errorToHandler(err)));
54+
})
55+
.finally(() => setLoadingCreate(false));
4356
};
4457

4558
const resetForm = () => {
@@ -103,7 +116,7 @@ const AddKeyForm = ({ onSuccess, onError }: IAddKeyFormProps) => {
103116
type="submit"
104117
variant="callAction"
105118
color="primary"
106-
disabled={loading || !validSave}
119+
disabled={loadingCreate || !validSave}
107120
label={"Save"}
108121
/>
109122
</Grid>

0 commit comments

Comments
 (0)