@@ -26,17 +26,18 @@ import {
2626 Switch ,
2727} from "mds" ;
2828import { useNavigate } from "react-router-dom" ;
29- import { ErrorResponseHandler } from "../../../common/types" ;
3029import { useAppDispatch } from "../../../store" ;
3130import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary" ;
3231import {
3332 setErrorSnackMessage ,
3433 setHelpName ,
3534 setServerNeedsRestart ,
3635} from "../../../systemSlice" ;
37- import useApi from "../Common/Hooks/useApi" ;
3836import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper" ;
3937import HelpMenu from "../HelpMenu" ;
38+ import { api } from "api" ;
39+ import { ApiError , HttpResponse , SetIDPResponse } from "api/consoleApi" ;
40+ import { errorToHandler } from "api/errors" ;
4041
4142type AddIDPConfigurationProps = {
4243 classes ?: any ;
@@ -46,7 +47,6 @@ type AddIDPConfigurationProps = {
4647 title : string ;
4748 backLink : string ;
4849 formFields : object ;
49- endpoint : string ;
5050} ;
5151
5252const AddIDPConfiguration = ( {
@@ -56,7 +56,6 @@ const AddIDPConfiguration = ({
5656 backLink,
5757 title,
5858 formFields,
59- endpoint,
6059} : AddIDPConfigurationProps ) => {
6160 const extraFormFields = {
6261 name : {
@@ -76,16 +75,7 @@ const AddIDPConfiguration = ({
7675 const dispatch = useAppDispatch ( ) ;
7776
7877 const [ fields , setFields ] = useState < any > ( { } ) ;
79-
80- const onSuccess = ( res : any ) => {
81- navigate ( backLink ) ;
82- dispatch ( setServerNeedsRestart ( res . restart === true ) ) ;
83- } ;
84-
85- const onError = ( err : ErrorResponseHandler ) =>
86- dispatch ( setErrorSnackMessage ( err ) ) ;
87-
88- const [ loading , invokeApi ] = useApi ( onSuccess , onError ) ;
78+ const [ loadingCreate , setLoadingCreate ] = useState < boolean > ( false ) ;
8979
9080 const validSave = ( ) => {
9181 for ( const [ key , value ] of Object . entries ( extraFormFields ) ) {
@@ -108,6 +98,7 @@ const AddIDPConfiguration = ({
10898 } ;
10999
110100 const addRecord = ( event : React . FormEvent ) => {
101+ setLoadingCreate ( true ) ;
111102 event . preventDefault ( ) ;
112103 const name = fields [ "name" ] ;
113104 let input = "" ;
@@ -116,7 +107,17 @@ const AddIDPConfiguration = ({
116107 input += `${ key } =${ fields [ key ] } ` ;
117108 }
118109 }
119- invokeApi ( "POST" , endpoint , { name, input } ) ;
110+
111+ api . idp
112+ . createConfiguration ( "openid" , { name, input } )
113+ . then ( ( res : HttpResponse < SetIDPResponse , ApiError > ) => {
114+ navigate ( backLink ) ;
115+ dispatch ( setServerNeedsRestart ( res . data . restart === true ) ) ;
116+ } )
117+ . catch ( async ( res : HttpResponse < SetIDPResponse , ApiError > ) => {
118+ dispatch ( setErrorSnackMessage ( errorToHandler ( res . error ) ) ) ;
119+ } )
120+ . finally ( ( ) => setLoadingCreate ( false ) ) ;
120121 } ;
121122
122123 const renderFormField = ( key : string , value : any ) => {
@@ -197,7 +198,7 @@ const AddIDPConfiguration = ({
197198 type = "submit"
198199 variant = "callAction"
199200 color = "primary"
200- disabled = { loading || ! validSave ( ) }
201+ disabled = { loadingCreate || ! validSave ( ) }
201202 label = { "Save" }
202203 />
203204 </ Grid >
0 commit comments