From b5e7554c26a614de498ce1b08d58a8907dfcc80c Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Wed, 31 Jan 2024 14:09:17 +0530 Subject: [PATCH] Integrate GQL mutations for updating and deleting environment variables in frontend (#55) * Use edit and delete environment variable gql client method * Handle review changes --------- Co-authored-by: neeraj --- .../settings/DisplayEnvironmentVariables.tsx | 10 +++- .../settings/EditEnvironmentVariableRow.tsx | 50 +++++++++++++++---- .../settings/EnvironmentVariablesTabPanel.tsx | 9 ++++ 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/packages/frontend/src/components/projects/project/settings/DisplayEnvironmentVariables.tsx b/packages/frontend/src/components/projects/project/settings/DisplayEnvironmentVariables.tsx index 53573c6..75ee3da 100644 --- a/packages/frontend/src/components/projects/project/settings/DisplayEnvironmentVariables.tsx +++ b/packages/frontend/src/components/projects/project/settings/DisplayEnvironmentVariables.tsx @@ -8,11 +8,13 @@ import EditEnvironmentVariableRow from './EditEnvironmentVariableRow'; interface DisplayEnvironmentVariablesProps { environment: Environment; variables: EnvironmentVariable[]; + onUpdate: () => Promise; } const DisplayEnvironmentVariables = ({ environment, variables, + onUpdate, }: DisplayEnvironmentVariablesProps) => { const [openCollapse, setOpenCollapse] = useState(false); @@ -37,9 +39,13 @@ const DisplayEnvironmentVariables = ({ ) : ( - variables.map((variable: EnvironmentVariable, index: number) => { + variables.map((variable: EnvironmentVariable) => { return ( - + ); }) )} diff --git a/packages/frontend/src/components/projects/project/settings/EditEnvironmentVariableRow.tsx b/packages/frontend/src/components/projects/project/settings/EditEnvironmentVariableRow.tsx index 8d90518..f8a8735 100644 --- a/packages/frontend/src/components/projects/project/settings/EditEnvironmentVariableRow.tsx +++ b/packages/frontend/src/components/projects/project/settings/EditEnvironmentVariableRow.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import toast from 'react-hot-toast'; import { EnvironmentVariable } from 'gql-client'; @@ -6,6 +6,7 @@ import { EnvironmentVariable } from 'gql-client'; import { IconButton, Input, Typography } from '@material-tailwind/react'; import ConfirmDialog from '../../../shared/ConfirmDialog'; +import { useGQLClient } from '../../../../context/GQLClientContext'; const ShowPasswordIcon = ({ handler, @@ -28,9 +29,13 @@ const ShowPasswordIcon = ({ const EditEnvironmentVariableRow = ({ variable, + onUpdate, }: { variable: EnvironmentVariable; + onUpdate: () => Promise; }) => { + const client = useGQLClient(); + const { handleSubmit, register, reset } = useForm({ defaultValues: { key: variable.key, @@ -42,6 +47,39 @@ const EditEnvironmentVariableRow = ({ const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [edit, setEdit] = useState(false); + const removeEnvironmentVariableHandler = useCallback(async () => { + const { removeEnvironmentVariable: isEnvironmentVariableRemoved } = + await client.removeEnvironmentVariable(variable.id); + + if (isEnvironmentVariableRemoved) { + await onUpdate(); + setDeleteDialogOpen((preVal) => !preVal); + toast.success('Variable deleted'); + } else { + toast.error('Variable not deleted'); + } + }, [variable, onUpdate]); + + const updateEnvironmentVariableHandler = useCallback( + async (data: { key: string; value: string }) => { + const { updateEnvironmentVariable: isEnvironmentVariableUpdated } = + await client.updateEnvironmentVariable(variable.id, data); + + if (isEnvironmentVariableUpdated) { + await onUpdate(); + toast.success('Variable edited'); + setEdit((preVal) => !preVal); + } else { + toast.error('Variable not edited'); + } + }, + [variable, onUpdate], + ); + + useEffect(() => { + reset({ key: variable.key, value: variable.value }); + }, [variable]); + return ( <>
@@ -74,10 +112,7 @@ const EditEnvironmentVariableRow = ({ <>
{ - toast.success('Variable edited'); - setEdit((preVal) => !preVal); - })} + onClick={handleSubmit(updateEnvironmentVariableHandler)} size="sm" > {'S'} @@ -124,10 +159,7 @@ const EditEnvironmentVariableRow = ({ handleOpen={() => setDeleteDialogOpen((preVal) => !preVal)} open={deleteDialogOpen} confirmButtonTitle="Yes, Confirm delete" - handleConfirm={() => { - setDeleteDialogOpen((preVal) => !preVal); - toast.success('Variable deleted'); - }} + handleConfirm={removeEnvironmentVariableHandler} color="red" > diff --git a/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx b/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx index 3c5c4fb..0c4af5a 100644 --- a/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx +++ b/packages/frontend/src/components/projects/project/settings/EnvironmentVariablesTabPanel.tsx @@ -227,16 +227,25 @@ export const EnvironmentVariablesTabPanel = () => { { + await fetchEnvironmentVariables(id); + }} /> { + await fetchEnvironmentVariables(id); + }} /> { + await fetchEnvironmentVariables(id); + }} />