diff --git a/dashboard/src/components/GenerateKeysModal.tsx b/dashboard/src/components/GenerateKeysModal.tsx index 213579f..30d394b 100644 --- a/dashboard/src/components/GenerateKeysModal.tsx +++ b/dashboard/src/components/GenerateKeysModal.tsx @@ -14,6 +14,7 @@ import { Text, useToast, Input, + Spinner, } from '@chakra-ui/react'; import { useClient } from 'urql'; import { FaSave } from 'react-icons/fa'; @@ -53,37 +54,49 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => { const [stateVariables, setStateVariables] = React.useState({ ...initState, }); + const [isLoading, setIsLoading] = React.useState(false); + React.useEffect(() => { if (isOpen) { setStateVariables({ ...initState, JWT_TYPE: jwtType }); } }, [isOpen]); + const fetchKeys = async () => { - const res = await client - .mutation(GenerateKeys, { params: { type: stateVariables.JWT_TYPE } }) - .toPromise(); - if (res?.error) { - toast({ - title: 'Error occurred generating jwt keys', - isClosable: true, - status: 'error', - position: 'bottom-right', - }); - closeHandler(); - } else { - setStateVariables({ - ...stateVariables, - JWT_SECRET: res?.data?._generate_jwt_keys?.secret || '', - JWT_PRIVATE_KEY: res?.data?._generate_jwt_keys?.private_key || '', - JWT_PUBLIC_KEY: res?.data?._generate_jwt_keys?.public_key || '', - }); + setIsLoading(true); + try { + const res = await client + .mutation(GenerateKeys, { params: { type: stateVariables.JWT_TYPE } }) + .toPromise(); + if (res?.error) { + toast({ + title: 'Error occurred generating jwt keys', + isClosable: true, + status: 'error', + position: 'bottom-right', + }); + closeHandler(); + } else { + setStateVariables({ + ...stateVariables, + JWT_SECRET: res?.data?._generate_jwt_keys?.secret || '', + JWT_PRIVATE_KEY: res?.data?._generate_jwt_keys?.private_key || '', + JWT_PUBLIC_KEY: res?.data?._generate_jwt_keys?.public_key || '', + }); + } + } catch (error) { + console.log(error); + } finally { + setIsLoading(false); } }; + React.useEffect(() => { if (isOpen && stateVariables.JWT_TYPE) { fetchKeys(); } }, [stateVariables.JWT_TYPE]); + const saveHandler = async () => { const res = await client .mutation(UpdateEnvVariables, { params: { ...stateVariables } }) @@ -110,9 +123,10 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => { const closeHandler = () => { setStateVariables({ ...initState }); - onClose(); getData(); + onClose(); }; + return ( <> - + New JWT keys @@ -146,56 +160,67 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => { }} /> - {Object.values(HMACEncryptionType).includes( - stateVariables.JWT_TYPE - ) ? ( - - - JWT Secret - -
- - setStateVariables({ - ...stateVariables, - JWT_SECRET: event.target.value, - }) - } - /> -
-
+ {isLoading ? ( +
+ +
) : ( <> - - - Public Key + {Object.values(HMACEncryptionType).includes( + stateVariables.JWT_TYPE + ) ? ( + + + JWT Secret + +
+ + setStateVariables({ + ...stateVariables, + JWT_SECRET: event.target.value, + }) + } + readOnly + /> +
-
- -
-
- - - Private Key - -
- -
-
+ ) : ( + <> + + + Public Key + +
+ +
+
+ + + Private Key + +
+ +
+
+ + )} )} @@ -206,7 +231,7 @@ const GenerateKeysModal = ({ jwtType, getData }: propTypes) => { colorScheme="blue" variant="solid" onClick={saveHandler} - isDisabled={false} + isDisabled={isLoading} >
Apply