From a3d9783aef53890f4598853aa193701fb0c75fdd Mon Sep 17 00:00:00 2001 From: Anik Ghosh Date: Thu, 24 Mar 2022 21:08:10 +0530 Subject: [PATCH] mutation to update jwt vars added --- .../src/components/GenerateKeysModal.tsx | 98 ++++++++++--------- dashboard/src/graphql/mutation/index.ts | 10 ++ dashboard/src/pages/Environment.tsx | 45 ++++----- 3 files changed, 82 insertions(+), 71 deletions(-) diff --git a/dashboard/src/components/GenerateKeysModal.tsx b/dashboard/src/components/GenerateKeysModal.tsx index c5431df..c68a756 100644 --- a/dashboard/src/components/GenerateKeysModal.tsx +++ b/dashboard/src/components/GenerateKeysModal.tsx @@ -19,18 +19,17 @@ import { useClient } from 'urql'; import { FaSave } from 'react-icons/fa'; import { ECDSAEncryptionType, - envVarTypes, HMACEncryptionType, RSAEncryptionType, SelectInputType, TextAreaInputType, } from '../constants'; import InputField from './InputField'; +import { GenerateKeys, UpdateEnvVariables } from '../graphql/mutation'; interface propTypes { - saveEnvHandler: Function; - variables: envVarTypes; - setVariables: Function; + jwtType: string; + getData: Function; } interface stateVarTypes { @@ -47,11 +46,7 @@ const initState: stateVarTypes = { JWT_PUBLIC_KEY: '', }; -const GenerateKeysModal = ({ - saveEnvHandler, - variables, - setVariables, -}: propTypes) => { +const GenerateKeysModal = ({ jwtType, getData }: propTypes) => { const client = useClient(); const toast = useToast(); const { isOpen, onOpen, onClose } = useDisclosure(); @@ -60,17 +55,26 @@ const GenerateKeysModal = ({ }); React.useEffect(() => { if (isOpen) { - setStateVariables({ ...initState, JWT_TYPE: variables.JWT_TYPE }); + setStateVariables({ ...initState, JWT_TYPE: jwtType }); } }, [isOpen]); - const setKeys = () => { - // fetch keys from api - console.log('calling setKeys ==>> ', stateVariables.JWT_TYPE); - if (true) { - if (Object.values(HMACEncryptionType).includes(stateVariables.JWT_TYPE)) { + 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 { + if (res?.data?._generate_jwt_keys?.secret) { setStateVariables({ ...stateVariables, - JWT_SECRET: 'hello_world', + JWT_SECRET: res.data._generate_jwt_keys.secret, JWT_PRIVATE_KEY: '', JWT_PUBLIC_KEY: '', }); @@ -78,39 +82,45 @@ const GenerateKeysModal = ({ setStateVariables({ ...stateVariables, JWT_SECRET: '', - JWT_PRIVATE_KEY: 'test private key', - JWT_PUBLIC_KEY: 'test public key', + JWT_PRIVATE_KEY: res.data._generate_jwt_keys.private_key, + JWT_PUBLIC_KEY: res.data._generate_jwt_keys.public_key, }); } + } + }; + React.useEffect(() => { + if (isOpen && stateVariables.JWT_TYPE) { + fetchKeys(); + } + }, [stateVariables.JWT_TYPE]); + const saveHandler = async () => { + const res = await client + .mutation(UpdateEnvVariables, { params: { ...stateVariables } }) + .toPromise(); + + if (res.error) { toast({ - title: 'New keys generated', - isClosable: true, - status: 'success', - position: 'bottom-right', - }); - } else { - toast({ - title: 'Error occurred generating keys', + title: 'Error occurred setting jwt keys', isClosable: true, status: 'error', position: 'bottom-right', }); - closeHandler(); + + return; } - }; - React.useEffect(() => { - if (isOpen) { - setKeys(); - } - }, [stateVariables.JWT_TYPE]); - const saveHandler = async () => { - setVariables({ ...variables, ...stateVariables }); - saveEnvHandler(); + toast({ + title: 'JWT keys updated successfully', + isClosable: true, + status: 'success', + position: 'bottom-right', + }); closeHandler(); }; - const closeHandler = async () => { + + const closeHandler = () => { setStateVariables({ ...initState }); onClose(); + getData(); }; return ( <> @@ -149,10 +159,10 @@ const GenerateKeysModal = ({ stateVariables.JWT_TYPE ) ? ( - + JWT Secret -
+
- + Public Key -
+
- + Private Key -
+