@@ -27,34 +27,41 @@ import {
27
27
} from "mds" ;
28
28
import { useNavigate } from "react-router-dom" ;
29
29
import { IAM_PAGES } from "../../../common/SecureComponent/permissions" ;
30
- import { ErrorResponseHandler } from "../../../common/types" ;
31
30
import { setErrorSnackMessage , setHelpName } from "../../../systemSlice" ;
32
31
import { useAppDispatch } from "../../../store" ;
33
32
import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary" ;
34
- import useApi from "../Common/Hooks/useApi" ;
35
33
import KMSHelpBox from "./KMSHelpbox" ;
36
34
import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper" ;
37
35
import HelpMenu from "../HelpMenu" ;
36
+ import { api } from "api" ;
37
+ import { ApiError , HttpResponse } from "api/consoleApi" ;
38
+ import { errorToHandler } from "api/errors" ;
38
39
39
40
export const emptyContent = '{\n "bytes": ""\n}' ;
40
41
41
42
const ImportKey = ( ) => {
42
43
const dispatch = useAppDispatch ( ) ;
43
44
const navigate = useNavigate ( ) ;
44
45
45
- const onSuccess = ( ) => navigate ( `${ IAM_PAGES . KMS_KEYS } ` ) ;
46
-
47
- const onError = ( err : ErrorResponseHandler ) =>
48
- dispatch ( setErrorSnackMessage ( err ) ) ;
49
-
50
- const [ loading , invokeApi ] = useApi ( onSuccess , onError ) ;
46
+ const [ loadingImport , setLoadingImport ] = useState < boolean > ( false ) ;
51
47
const [ keyName , setKeyName ] = useState < string > ( "" ) ;
52
48
const [ keyContent , setKeyContent ] = useState < string > ( emptyContent ) ;
53
49
54
50
const importRecord = ( event : React . FormEvent ) => {
51
+ setLoadingImport ( true ) ;
55
52
event . preventDefault ( ) ;
56
53
let data = JSON . parse ( keyContent ) ;
57
- invokeApi ( "POST" , `/api/v1/kms/keys/${ keyName } /import` , data ) ;
54
+
55
+ api . kms
56
+ . kmsImportKey ( keyName , data )
57
+ . then ( ( _ ) => {
58
+ navigate ( `${ IAM_PAGES . KMS_KEYS } ` ) ;
59
+ } )
60
+ . catch ( async ( res : HttpResponse < void , ApiError > ) => {
61
+ const err = ( await res . json ( ) ) as ApiError ;
62
+ dispatch ( setErrorSnackMessage ( errorToHandler ( err ) ) ) ;
63
+ } )
64
+ . finally ( ( ) => setLoadingImport ( false ) ) ;
58
65
} ;
59
66
60
67
const resetForm = ( ) => {
@@ -140,7 +147,7 @@ const ImportKey = () => {
140
147
type = "submit"
141
148
variant = "callAction"
142
149
color = "primary"
143
- disabled = { loading || ! validSave }
150
+ disabled = { loadingImport || ! validSave }
144
151
label = { "Import" }
145
152
/>
146
153
</ Grid >
0 commit comments