Implement functionality for adding web hooks in settings Git tab (#37)

* Add webhookUrl card in settings/git

* Implement copy functionality

* Fix form reset

* Handle review changes

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
This commit is contained in:
Nabarun Gogoi 2023-12-28 18:21:27 +05:30 committed by GitHub
parent abff6c83e5
commit 2cb1feedcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 124 additions and 18 deletions

View File

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { useParams, Link } from 'react-router-dom'; import { useParams, Link } from 'react-router-dom';
import { Button, Typography } from '@material-tailwind/react'; import { Button, Typography } from '@material-tailwind/react';
import DomainCard from './DomainCard'; import DomainCard from './DomainCard';

View File

@ -1,4 +1,6 @@
import React, { useState } from 'react'; import React, { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import toast from 'react-hot-toast';
import { Button, Input, Switch, Typography } from '@material-tailwind/react'; import { Button, Input, Switch, Typography } from '@material-tailwind/react';
@ -6,10 +8,12 @@ import RepositoryList from '../../create/RepositoryList';
import RepoConnectedSection from './RepoConnectedSection'; import RepoConnectedSection from './RepoConnectedSection';
import GitSelectionSection from './GitSelectionSection'; import GitSelectionSection from './GitSelectionSection';
import { GitSelect, RepositoryDetails } from '../../../../types/project'; import { GitSelect, RepositoryDetails } from '../../../../types/project';
import WebhookCard from './WebhookCard';
const GitTabPanel = () => { const GitTabPanel = () => {
const [gitSelect, setGitSelect] = useState('none'); const [gitSelect, setGitSelect] = useState('none');
const [linkedRepo, setLinkedRepo] = useState<RepositoryDetails>(); const [linkedRepo, setLinkedRepo] = useState<RepositoryDetails>();
const [webhooksArray, setWebhooksArray] = useState<Array<string>>([]);
const gitSelectionHandler = (git: GitSelect) => { const gitSelectionHandler = (git: GitSelect) => {
setGitSelect(git); setGitSelect(git);
@ -19,6 +23,23 @@ const GitTabPanel = () => {
setLinkedRepo(repoDetails); setLinkedRepo(repoDetails);
}; };
const {
register,
handleSubmit,
reset,
formState: { isSubmitSuccessful },
} = useForm();
useEffect(() => {
reset();
}, [isSubmitSuccessful]);
const handleDelete = (index: number) => {
const newArray = webhooksArray.filter((value, idx) => idx != index);
setWebhooksArray(newArray);
toast.success('Webhook deleted successfully');
};
return ( return (
<> <>
<div className="mb-2 p-2"> <div className="mb-2 p-2">
@ -85,6 +106,13 @@ const GitTabPanel = () => {
</Button> </Button>
</div> </div>
<form
onSubmit={handleSubmit((data) => {
setWebhooksArray((prevArray) => [...prevArray, data.webhookUrl]);
toast.success('Webhook added successfully.');
})}
>
<div className="mb-2 p-2"> <div className="mb-2 p-2">
<Typography variant="h6" className="text-black"> <Typography variant="h6" className="text-black">
Deploy webhooks Deploy webhooks
@ -96,15 +124,28 @@ const GitTabPanel = () => {
<div className="flex gap-1"> <div className="flex gap-1">
<div className="grow"> <div className="grow">
<Typography variant="small">Webhook URL</Typography> <Typography variant="small">Webhook URL</Typography>
<Input crossOrigin={undefined} /> <Input crossOrigin={undefined} {...register('webhookUrl')} />
</div> </div>
<div className="self-end"> <div className="self-end">
<Button size="sm" disabled> <Button size="sm" type="submit">
Save Save
</Button> </Button>
</div> </div>
</div> </div>
</div> </div>
</form>
<div className="mb-2 p-2">
{webhooksArray?.map((webhookUrl, index) => {
return (
<WebhookCard
webhooksArray={webhooksArray}
webhookUrl={webhookUrl}
handleDelete={() => handleDelete(index)}
key={index}
/>
);
})}
</div>
</> </>
); );
}; };

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { Button, Typography } from '@material-tailwind/react'; import { Button, Typography } from '@material-tailwind/react';
import { RepositoryDetails } from '../../../../types/project'; import { RepositoryDetails } from '../../../../types/project';
const RepoConnectedSection = ({ const RepoConnectedSection = ({

View File

@ -1,6 +1,7 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { import {
Radio, Radio,
Typography, Typography,

View File

@ -0,0 +1,62 @@
import React, { useState } from 'react';
import toast from 'react-hot-toast';
import { Button, Typography } from '@material-tailwind/react';
import ConfirmDialog from '../../../shared/ConfirmDialog';
const WebhookCard = (props: {
webhooksArray: string[];
webhookUrl: string;
handleDelete: () => void;
}) => {
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
return (
<div className="flex justify-between w-full mb-3">
{props.webhookUrl}
<div className="flex gap-3">
<Button
size="sm"
onClick={() => {
navigator.clipboard.writeText(props.webhookUrl);
toast.success('Copied to clipboard');
}}
>
C
</Button>
<Button
color="red"
size="sm"
onClick={() => {
setDeleteDialogOpen(true);
}}
>
X
</Button>
</div>
<ConfirmDialog
dialogTitle="Delete webhook"
handleOpen={() => setDeleteDialogOpen((preVal) => !preVal)}
open={deleteDialogOpen}
confirmButtonTitle="Yes, Confirm delete"
handleConfirm={() => {
setDeleteDialogOpen((preVal) => !preVal);
props.handleDelete();
}}
color="red"
>
<Typography variant="small">
Are you sure you want to delete the variable{' '}
<span className="bg-blue-100 p-0.5 rounded-sm">
{props.webhookUrl}
</span>
?
</Typography>
</ConfirmDialog>
</div>
);
};
export default WebhookCard;