@@ -23,23 +23,36 @@ import {
2323 Grid ,
2424 InputBox ,
2525} from "mds" ;
26- import { ErrorResponseHandler } from "../../../common/types" ;
2726import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary" ;
28- import useApi from "../Common/Hooks/useApi" ;
2927import 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