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 <neeraj.rtly@gmail.com>
This commit is contained in:
Nabarun Gogoi 2024-01-31 14:09:17 +05:30 committed by Ashwin Phatak
parent c8c6f66ed2
commit b5e7554c26
3 changed files with 58 additions and 11 deletions

View File

@ -8,11 +8,13 @@ import EditEnvironmentVariableRow from './EditEnvironmentVariableRow';
interface DisplayEnvironmentVariablesProps { interface DisplayEnvironmentVariablesProps {
environment: Environment; environment: Environment;
variables: EnvironmentVariable[]; variables: EnvironmentVariable[];
onUpdate: () => Promise<void>;
} }
const DisplayEnvironmentVariables = ({ const DisplayEnvironmentVariables = ({
environment, environment,
variables, variables,
onUpdate,
}: DisplayEnvironmentVariablesProps) => { }: DisplayEnvironmentVariablesProps) => {
const [openCollapse, setOpenCollapse] = useState(false); const [openCollapse, setOpenCollapse] = useState(false);
@ -37,9 +39,13 @@ const DisplayEnvironmentVariables = ({
</Typography> </Typography>
</Card> </Card>
) : ( ) : (
variables.map((variable: EnvironmentVariable, index: number) => { variables.map((variable: EnvironmentVariable) => {
return ( return (
<EditEnvironmentVariableRow key={index} variable={variable} /> <EditEnvironmentVariableRow
onUpdate={onUpdate}
key={variable.id}
variable={variable}
/>
); );
}) })
)} )}

View File

@ -1,4 +1,4 @@
import React, { useState } from 'react'; import React, { useCallback, useEffect, useState } from 'react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import toast from 'react-hot-toast'; import toast from 'react-hot-toast';
import { EnvironmentVariable } from 'gql-client'; import { EnvironmentVariable } from 'gql-client';
@ -6,6 +6,7 @@ import { EnvironmentVariable } from 'gql-client';
import { IconButton, Input, Typography } from '@material-tailwind/react'; import { IconButton, Input, Typography } from '@material-tailwind/react';
import ConfirmDialog from '../../../shared/ConfirmDialog'; import ConfirmDialog from '../../../shared/ConfirmDialog';
import { useGQLClient } from '../../../../context/GQLClientContext';
const ShowPasswordIcon = ({ const ShowPasswordIcon = ({
handler, handler,
@ -28,9 +29,13 @@ const ShowPasswordIcon = ({
const EditEnvironmentVariableRow = ({ const EditEnvironmentVariableRow = ({
variable, variable,
onUpdate,
}: { }: {
variable: EnvironmentVariable; variable: EnvironmentVariable;
onUpdate: () => Promise<void>;
}) => { }) => {
const client = useGQLClient();
const { handleSubmit, register, reset } = useForm({ const { handleSubmit, register, reset } = useForm({
defaultValues: { defaultValues: {
key: variable.key, key: variable.key,
@ -42,6 +47,39 @@ const EditEnvironmentVariableRow = ({
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [edit, setEdit] = 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 ( return (
<> <>
<div className="flex gap-1 p-2"> <div className="flex gap-1 p-2">
@ -74,10 +112,7 @@ const EditEnvironmentVariableRow = ({
<> <>
<div className="self-end"> <div className="self-end">
<IconButton <IconButton
onClick={handleSubmit(() => { onClick={handleSubmit(updateEnvironmentVariableHandler)}
toast.success('Variable edited');
setEdit((preVal) => !preVal);
})}
size="sm" size="sm"
> >
{'S'} {'S'}
@ -124,10 +159,7 @@ const EditEnvironmentVariableRow = ({
handleOpen={() => setDeleteDialogOpen((preVal) => !preVal)} handleOpen={() => setDeleteDialogOpen((preVal) => !preVal)}
open={deleteDialogOpen} open={deleteDialogOpen}
confirmButtonTitle="Yes, Confirm delete" confirmButtonTitle="Yes, Confirm delete"
handleConfirm={() => { handleConfirm={removeEnvironmentVariableHandler}
setDeleteDialogOpen((preVal) => !preVal);
toast.success('Variable deleted');
}}
color="red" color="red"
> >
<Typography variant="small"> <Typography variant="small">

View File

@ -227,16 +227,25 @@ export const EnvironmentVariablesTabPanel = () => {
<DisplayEnvironmentVariables <DisplayEnvironmentVariables
environment={Environment.Production} environment={Environment.Production}
variables={getEnvironmentVariables(Environment.Production)} variables={getEnvironmentVariables(Environment.Production)}
onUpdate={async () => {
await fetchEnvironmentVariables(id);
}}
/> />
<HorizontalLine /> <HorizontalLine />
<DisplayEnvironmentVariables <DisplayEnvironmentVariables
environment={Environment.Preview} environment={Environment.Preview}
variables={getEnvironmentVariables(Environment.Preview)} variables={getEnvironmentVariables(Environment.Preview)}
onUpdate={async () => {
await fetchEnvironmentVariables(id);
}}
/> />
<HorizontalLine /> <HorizontalLine />
<DisplayEnvironmentVariables <DisplayEnvironmentVariables
environment={Environment.Development} environment={Environment.Development}
variables={getEnvironmentVariables(Environment.Development)} variables={getEnvironmentVariables(Environment.Development)}
onUpdate={async () => {
await fetchEnvironmentVariables(id);
}}
/> />
</div> </div>
</> </>