Skip to content

Commit 7ce6a58

Browse files
authored
Use swagger api for Get API from Subnet (#3207)
1 parent 57d6aca commit 7ce6a58

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

web-app/src/screens/Console/Support/GetApiKeyModal.tsx

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616

1717
import React, { useState } from "react";
1818
import { Box, FormLayout, InfoIcon, InputBox, LockIcon, UsersIcon } from "mds";
19-
import { ErrorResponseHandler } from "../../../common/types";
2019
import { useAppDispatch } from "../../../store";
2120
import { setErrorSnackMessage } from "../../../systemSlice";
2221
import ConfirmDialog from "../Common/ModalWrapper/ConfirmDialog";
23-
import useApi from "../Common/Hooks/useApi";
22+
import { api } from "api";
23+
import {
24+
ApiError,
25+
ApiKey,
26+
HttpResponse,
27+
SubnetLoginResponse,
28+
} from "api/consoleApi";
29+
import { errorToHandler } from "api/errors";
2430

2531
interface IGetApiKeyModalProps {
2632
open: boolean;
@@ -34,39 +40,75 @@ const GetApiKeyModal = ({ open, closeModal, onSet }: IGetApiKeyModalProps) => {
3440
const [password, setPassword] = useState("");
3541
const [mfaToken, setMfaToken] = useState("");
3642
const [subnetOTP, setSubnetOTP] = useState("");
43+
const [loadingSave, setLoadingSave] = useState<boolean>(false);
3744

38-
const onError = (err: ErrorResponseHandler) => {
39-
dispatch(setErrorSnackMessage(err));
45+
const onError = (err: ApiError) => {
46+
dispatch(setErrorSnackMessage(errorToHandler(err)));
4047
closeModal();
4148
setEmail("");
4249
setPassword("");
4350
setMfaToken("");
4451
setSubnetOTP("");
4552
};
4653

47-
const onSuccess = (res: any) => {
48-
if (res.mfa_token) {
49-
setMfaToken(res.mfa_token);
50-
} else if (res.access_token) {
51-
invokeApi("GET", `/api/v1/subnet/apikey?token=${res.access_token}`);
54+
const onConfirm = () => {
55+
if (mfaToken !== "") {
56+
submitSubnetMfa();
5257
} else {
53-
onSet(res.apiKey);
54-
closeModal();
58+
submitSubnetLogin();
5559
}
5660
};
5761

58-
const [isLoading, invokeApi] = useApi(onSuccess, onError);
59-
60-
const onConfirm = () => {
61-
if (mfaToken !== "") {
62-
invokeApi("POST", "/api/v1/subnet/login/mfa", {
62+
const submitSubnetMfa = () => {
63+
setLoadingSave(true);
64+
api.subnet
65+
.subnetLoginMfa({
6366
username: email,
6467
otp: subnetOTP,
6568
mfa_token: mfaToken,
66-
});
67-
} else {
68-
invokeApi("POST", "/api/v1/subnet/login", { username: email, password });
69-
}
69+
})
70+
.then((res: HttpResponse<SubnetLoginResponse, ApiError>) => {
71+
if (res.data && res.data.access_token) {
72+
getApiKey(res.data.access_token);
73+
}
74+
})
75+
.catch(async (res: HttpResponse<SubnetLoginResponse, ApiError>) => {
76+
onError(res.error);
77+
})
78+
.finally(() => setLoadingSave(false));
79+
};
80+
81+
const getApiKey = (access_token: string) => {
82+
setLoadingSave(true);
83+
api.subnet
84+
.subnetApiKey({
85+
token: access_token,
86+
})
87+
.then((res: HttpResponse<ApiKey, ApiError>) => {
88+
if (res.data && res.data.apiKey) {
89+
onSet(res.data.apiKey);
90+
closeModal();
91+
}
92+
})
93+
.catch(async (res: HttpResponse<SubnetLoginResponse, ApiError>) => {
94+
onError(res.error);
95+
})
96+
.finally(() => setLoadingSave(false));
97+
};
98+
99+
const submitSubnetLogin = () => {
100+
setLoadingSave(true);
101+
api.subnet
102+
.subnetLogin({ username: email, password })
103+
.then((res: HttpResponse<SubnetLoginResponse, ApiError>) => {
104+
if (res.data && res.data.mfa_token) {
105+
setMfaToken(res.data.mfa_token);
106+
}
107+
})
108+
.catch(async (res: HttpResponse<SubnetLoginResponse, ApiError>) => {
109+
onError(res.error);
110+
})
111+
.finally(() => setLoadingSave(false));
70112
};
71113

72114
const getDialogContent = () => {
@@ -153,17 +195,17 @@ const GetApiKeyModal = ({ open, closeModal, onSet }: IGetApiKeyModalProps) => {
153195
confirmText={"Get API Key"}
154196
isOpen={open}
155197
titleIcon={<InfoIcon />}
156-
isLoading={isLoading}
198+
isLoading={loadingSave}
157199
cancelText={"Cancel"}
158200
onConfirm={onConfirm}
159201
onClose={closeModal}
160202
confirmButtonProps={{
161203
variant: "callAction",
162-
disabled: !email || !password || isLoading,
204+
disabled: !email || !password || loadingSave,
163205
hidden: true,
164206
}}
165207
cancelButtonProps={{
166-
disabled: isLoading,
208+
disabled: loadingSave,
167209
}}
168210
confirmationContent={getDialogContent()}
169211
/>

0 commit comments

Comments
 (0)