diff --git a/packages/frontend/src/components/projects/project/settings/DisplayEnvironmentVariables.tsx b/packages/frontend/src/components/projects/project/settings/DisplayEnvironmentVariables.tsx index 53573c6a..75ee3dad 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 8d90518a..f8a87352 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 3c5c4fb8..0c4af5a8 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); + }} />