@@ -14,6 +14,7 @@ import {
1414 Text ,
1515 useToast ,
1616 Input ,
17+ Spinner ,
1718} from '@chakra-ui/react' ;
1819import { useClient } from 'urql' ;
1920import { FaSave } from 'react-icons/fa' ;
@@ -53,37 +54,49 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => {
5354 const [ stateVariables , setStateVariables ] = React . useState < stateVarTypes > ( {
5455 ...initState ,
5556 } ) ;
57+ const [ isLoading , setIsLoading ] = React . useState ( false ) ;
58+
5659 React . useEffect ( ( ) => {
5760 if ( isOpen ) {
5861 setStateVariables ( { ...initState , JWT_TYPE : jwtType } ) ;
5962 }
6063 } , [ isOpen ] ) ;
64+
6165 const fetchKeys = async ( ) => {
62- const res = await client
63- . mutation ( GenerateKeys , { params : { type : stateVariables . JWT_TYPE } } )
64- . toPromise ( ) ;
65- if ( res ?. error ) {
66- toast ( {
67- title : 'Error occurred generating jwt keys' ,
68- isClosable : true ,
69- status : 'error' ,
70- position : 'bottom-right' ,
71- } ) ;
72- closeHandler ( ) ;
73- } else {
74- setStateVariables ( {
75- ...stateVariables ,
76- JWT_SECRET : res ?. data ?. _generate_jwt_keys ?. secret || '' ,
77- JWT_PRIVATE_KEY : res ?. data ?. _generate_jwt_keys ?. private_key || '' ,
78- JWT_PUBLIC_KEY : res ?. data ?. _generate_jwt_keys ?. public_key || '' ,
79- } ) ;
66+ setIsLoading ( true ) ;
67+ try {
68+ const res = await client
69+ . mutation ( GenerateKeys , { params : { type : stateVariables . JWT_TYPE } } )
70+ . toPromise ( ) ;
71+ if ( res ?. error ) {
72+ toast ( {
73+ title : 'Error occurred generating jwt keys' ,
74+ isClosable : true ,
75+ status : 'error' ,
76+ position : 'bottom-right' ,
77+ } ) ;
78+ closeHandler ( ) ;
79+ } else {
80+ setStateVariables ( {
81+ ...stateVariables ,
82+ JWT_SECRET : res ?. data ?. _generate_jwt_keys ?. secret || '' ,
83+ JWT_PRIVATE_KEY : res ?. data ?. _generate_jwt_keys ?. private_key || '' ,
84+ JWT_PUBLIC_KEY : res ?. data ?. _generate_jwt_keys ?. public_key || '' ,
85+ } ) ;
86+ }
87+ } catch ( error ) {
88+ console . log ( error ) ;
89+ } finally {
90+ setIsLoading ( false ) ;
8091 }
8192 } ;
93+
8294 React . useEffect ( ( ) => {
8395 if ( isOpen && stateVariables . JWT_TYPE ) {
8496 fetchKeys ( ) ;
8597 }
8698 } , [ stateVariables . JWT_TYPE ] ) ;
99+
87100 const saveHandler = async ( ) => {
88101 const res = await client
89102 . mutation ( UpdateEnvVariables , { params : { ...stateVariables } } )
@@ -110,9 +123,10 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => {
110123
111124 const closeHandler = ( ) => {
112125 setStateVariables ( { ...initState } ) ;
113- onClose ( ) ;
114126 getData ( ) ;
127+ onClose ( ) ;
115128 } ;
129+
116130 return (
117131 < >
118132 < Button
@@ -124,7 +138,7 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => {
124138 >
125139 Generate new keys
126140 </ Button >
127- < Modal isOpen = { isOpen } onClose = { onClose } >
141+ < Modal isOpen = { isOpen } onClose = { closeHandler } >
128142 < ModalOverlay />
129143 < ModalContent >
130144 < ModalHeader > New JWT keys</ ModalHeader >
@@ -146,56 +160,67 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => {
146160 } }
147161 />
148162 </ Flex >
149- { Object . values ( HMACEncryptionType ) . includes (
150- stateVariables . JWT_TYPE
151- ) ? (
152- < Flex marginTop = "8" >
153- < Flex w = "23%" justifyContent = "start" alignItems = "center" >
154- < Text fontSize = "sm" > JWT Secret</ Text >
155- </ Flex >
156- < Center w = "77%" >
157- < Input
158- size = "sm"
159- value = { stateVariables . JWT_SECRET }
160- onChange = { ( event : any ) =>
161- setStateVariables ( {
162- ...stateVariables ,
163- JWT_SECRET : event . target . value ,
164- } )
165- }
166- />
167- </ Center >
168- </ Flex >
163+ { isLoading ? (
164+ < Center minH = "25vh" >
165+ < Spinner />
166+ </ Center >
169167 ) : (
170168 < >
171- < Flex marginTop = "8" >
172- < Flex w = "23%" justifyContent = "start" alignItems = "center" >
173- < Text fontSize = "sm" > Public Key</ Text >
174- </ Flex >
175- < Center w = "77%" >
176- < InputField
177- variables = { stateVariables }
178- setVariables = { setStateVariables }
179- inputType = { TextAreaInputType . JWT_PUBLIC_KEY }
180- placeholder = "Add public key here"
181- minH = "25vh"
182- />
183- </ Center >
184- </ Flex >
185- < Flex marginTop = "8" >
186- < Flex w = "23%" justifyContent = "start" alignItems = "center" >
187- < Text fontSize = "sm" > Private Key</ Text >
169+ { Object . values ( HMACEncryptionType ) . includes (
170+ stateVariables . JWT_TYPE
171+ ) ? (
172+ < Flex marginTop = "8" >
173+ < Flex w = "23%" justifyContent = "start" alignItems = "center" >
174+ < Text fontSize = "sm" > JWT Secret</ Text >
175+ </ Flex >
176+ < Center w = "77%" >
177+ < Input
178+ size = "sm"
179+ value = { stateVariables . JWT_SECRET }
180+ onChange = { ( event : any ) =>
181+ setStateVariables ( {
182+ ...stateVariables ,
183+ JWT_SECRET : event . target . value ,
184+ } )
185+ }
186+ readOnly
187+ />
188+ </ Center >
188189 </ Flex >
189- < Center w = "77%" >
190- < InputField
191- variables = { stateVariables }
192- setVariables = { setStateVariables }
193- inputType = { TextAreaInputType . JWT_PRIVATE_KEY }
194- placeholder = "Add private key here"
195- minH = "25vh"
196- />
197- </ Center >
198- </ Flex >
190+ ) : (
191+ < >
192+ < Flex marginTop = "8" >
193+ < Flex w = "23%" justifyContent = "start" alignItems = "center" >
194+ < Text fontSize = "sm" > Public Key</ Text >
195+ </ Flex >
196+ < Center w = "77%" >
197+ < InputField
198+ variables = { stateVariables }
199+ setVariables = { setStateVariables }
200+ inputType = { TextAreaInputType . JWT_PUBLIC_KEY }
201+ placeholder = "Add public key here"
202+ minH = "25vh"
203+ readOnly
204+ />
205+ </ Center >
206+ </ Flex >
207+ < Flex marginTop = "8" >
208+ < Flex w = "23%" justifyContent = "start" alignItems = "center" >
209+ < Text fontSize = "sm" > Private Key</ Text >
210+ </ Flex >
211+ < Center w = "77%" >
212+ < InputField
213+ variables = { stateVariables }
214+ setVariables = { setStateVariables }
215+ inputType = { TextAreaInputType . JWT_PRIVATE_KEY }
216+ placeholder = "Add private key here"
217+ minH = "25vh"
218+ readOnly
219+ />
220+ </ Center >
221+ </ Flex >
222+ </ >
223+ ) }
199224 </ >
200225 ) }
201226 </ ModalBody >
@@ -206,7 +231,7 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => {
206231 colorScheme = "blue"
207232 variant = "solid"
208233 onClick = { saveHandler }
209- isDisabled = { false }
234+ isDisabled = { isLoading }
210235 >
211236 < Center h = "100%" pt = "5%" >
212237 Apply
0 commit comments