Files
sign/apps/web/src/components/(dashboard)/settings/webhooks/create-webhook-dialog.tsx

233 lines
7.0 KiB
TypeScript
Raw Normal View History

2024-02-07 16:04:12 +02:00
'use client';
import { useState } from 'react';
2024-02-09 16:07:33 +02:00
2024-02-07 16:04:12 +02:00
import { useRouter } from 'next/navigation';
import { zodResolver } from '@hookform/resolvers/zod';
import type * as DialogPrimitive from '@radix-ui/react-dialog';
import { useForm } from 'react-hook-form';
2024-02-09 16:07:33 +02:00
import type { z } from 'zod';
2024-02-07 16:04:12 +02:00
2024-02-09 16:07:33 +02:00
import { trpc } from '@documenso/trpc/react';
2024-02-27 16:56:32 +11:00
import { ZCreateWebhookMutationSchema } from '@documenso/trpc/server/webhook-router/schema';
2024-02-07 16:04:12 +02:00
import { Button } from '@documenso/ui/primitives/button';
2024-02-09 16:07:33 +02:00
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@documenso/ui/primitives/dialog';
2024-02-07 16:04:12 +02:00
import {
Form,
FormControl,
2024-02-26 22:24:23 +11:00
FormDescription,
2024-02-07 16:04:12 +02:00
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@documenso/ui/primitives/form/form';
2024-02-09 16:07:33 +02:00
import { Input } from '@documenso/ui/primitives/input';
2024-02-16 11:44:03 +02:00
import { PasswordInput } from '@documenso/ui/primitives/password-input';
2024-02-09 16:07:33 +02:00
import { Switch } from '@documenso/ui/primitives/switch';
import { useToast } from '@documenso/ui/primitives/use-toast';
2024-02-07 16:04:12 +02:00
2024-02-27 16:56:32 +11:00
import { useOptionalCurrentTeam } from '~/providers/team';
2024-02-26 22:24:23 +11:00
import { TriggerMultiSelectCombobox } from './trigger-multiselect-combobox';
2024-02-07 16:04:12 +02:00
2024-02-27 16:56:32 +11:00
const ZCreateWebhookFormSchema = ZCreateWebhookMutationSchema.omit({ teamId: true });
2024-02-09 16:07:33 +02:00
type TCreateWebhookFormSchema = z.infer<typeof ZCreateWebhookFormSchema>;
2024-02-07 16:04:12 +02:00
export type CreateWebhookDialogProps = {
trigger?: React.ReactNode;
} & Omit<DialogPrimitive.DialogProps, 'children'>;
2024-02-06 16:00:28 +02:00
export const CreateWebhookDialog = ({ trigger, ...props }: CreateWebhookDialogProps) => {
2024-02-07 16:04:12 +02:00
const router = useRouter();
2024-02-09 16:07:33 +02:00
const { toast } = useToast();
2024-02-27 16:56:32 +11:00
const team = useOptionalCurrentTeam();
2024-02-07 16:04:12 +02:00
const [open, setOpen] = useState(false);
2024-02-09 16:07:33 +02:00
const form = useForm<TCreateWebhookFormSchema>({
resolver: zodResolver(ZCreateWebhookFormSchema),
2024-02-07 16:04:12 +02:00
values: {
webhookUrl: '',
eventTriggers: [],
secret: '',
enabled: true,
},
});
2024-02-09 16:07:33 +02:00
const { mutateAsync: createWebhook } = trpc.webhook.createWebhook.useMutation();
2024-02-27 16:56:32 +11:00
const onSubmit = async ({
enabled,
eventTriggers,
secret,
webhookUrl,
}: TCreateWebhookFormSchema) => {
2024-02-09 16:07:33 +02:00
try {
2024-02-27 16:56:32 +11:00
await createWebhook({
enabled,
eventTriggers,
secret,
webhookUrl,
teamId: team?.id,
});
2024-02-09 16:07:33 +02:00
setOpen(false);
toast({
title: 'Webhook created',
description: 'The webhook was successfully created.',
});
form.reset();
router.refresh();
} catch (err) {
toast({
title: 'Error',
description: 'An error occurred while creating the webhook. Please try again.',
variant: 'destructive',
});
}
};
2024-02-07 16:04:12 +02:00
return (
<Dialog
open={open}
onOpenChange={(value) => !form.formState.isSubmitting && setOpen(value)}
{...props}
>
<DialogTrigger onClick={(e) => e.stopPropagation()} asChild>
{trigger ?? <Button className="flex-shrink-0">Create Webhook</Button>}
</DialogTrigger>
2024-02-26 22:24:23 +11:00
<DialogContent className="max-w-lg" position="center">
2024-02-07 16:04:12 +02:00
<DialogHeader>
<DialogTitle>Create webhook</DialogTitle>
<DialogDescription>On this page, you can create a new webhook.</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<fieldset
className="flex h-full flex-col space-y-4"
disabled={form.formState.isSubmitting}
>
2024-02-26 22:24:23 +11:00
<div className="flex flex-col-reverse gap-4 md:flex-row">
<FormField
control={form.control}
name="webhookUrl"
render={({ field }) => (
<FormItem className="flex-1">
<FormLabel required>Webhook URL</FormLabel>
<FormControl>
<Input className="bg-background" {...field} />
</FormControl>
<FormDescription>
The URL for Documenso to send webhook events to.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="enabled"
render={({ field }) => (
<FormItem>
<FormLabel>Enabled</FormLabel>
<div>
<FormControl>
<Switch
className="bg-background"
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</div>
<FormMessage />
</FormItem>
)}
/>
</div>
2024-02-07 16:04:12 +02:00
<FormField
control={form.control}
name="eventTriggers"
render={({ field: { onChange, value } }) => (
<FormItem className="flex flex-col gap-2">
2024-02-26 22:24:23 +11:00
<FormLabel required>Triggers</FormLabel>
2024-02-07 16:04:12 +02:00
<FormControl>
2024-02-26 22:24:23 +11:00
<TriggerMultiSelectCombobox
2024-02-07 16:04:12 +02:00
listValues={value}
onChange={(values: string[]) => {
2024-02-09 16:07:33 +02:00
onChange(values);
2024-02-07 16:04:12 +02:00
}}
/>
</FormControl>
2024-02-26 22:24:23 +11:00
<FormDescription>
The events that will trigger a webhook to be sent to your URL.
</FormDescription>
2024-02-07 16:04:12 +02:00
<FormMessage />
</FormItem>
)}
/>
2024-02-09 16:07:33 +02:00
<FormField
2024-02-07 16:04:12 +02:00
control={form.control}
name="secret"
render={({ field }) => (
<FormItem>
<FormLabel>Secret</FormLabel>
<FormControl>
2024-02-16 11:44:03 +02:00
<PasswordInput
className="bg-background"
{...field}
value={field.value ?? ''}
/>
2024-02-07 16:04:12 +02:00
</FormControl>
2024-02-26 22:24:23 +11:00
<FormDescription>
A secret that will be sent to your URL so you can verify that the request has
been sent by Documenso.
</FormDescription>
2024-02-07 16:04:12 +02:00
<FormMessage />
</FormItem>
)}
/>
<DialogFooter>
<div className="flex w-full flex-nowrap gap-4">
2024-02-09 16:07:33 +02:00
<Button type="button" variant="secondary" onClick={() => setOpen(false)}>
2024-02-07 16:04:12 +02:00
Cancel
</Button>
<Button type="submit" loading={form.formState.isSubmitting}>
Create
</Button>
</div>
</DialogFooter>
</fieldset>
</form>
</Form>
</DialogContent>
</Dialog>
);
2024-02-06 16:00:28 +02:00
};