'use client' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { zodResolver } from '@hookform/resolvers/zod' import { useForm } from 'react-hook-form' import * as z from 'zod' import { v4 as uuidv4 } from 'uuid' import { WebhookData } from '@/lib/types' import { WEBHOOK_EVENTS } from '@/lib/constants' import httpBrowserClient from '@/lib/httpBrowserClient' import { ApiEndpoints } from '@/config/api' import { useToast } from '@/hooks/use-toast' import { useMutation, useQueryClient } from '@tanstack/react-query' const formSchema = z.object({ deliveryUrl: z.string().url({ message: 'Please enter a valid URL' }), events: z.array(z.string()).min(1, { message: 'Select at least one event' }), isActive: z.boolean().default(true), signingSecret: z.string().min(1, { message: 'Signing secret is required' }), }) interface CreateWebhookDialogProps { open: boolean onOpenChange: (open: boolean) => void } export function CreateWebhookDialog({ open, onOpenChange, }: CreateWebhookDialogProps) { const { toast } = useToast() const queryClient = useQueryClient() const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { deliveryUrl: '', events: [WEBHOOK_EVENTS.MESSAGE_RECEIVED], isActive: true, signingSecret: uuidv4(), }, }) const createWebhookMutation = useMutation({ mutationFn: (values: z.infer) => httpBrowserClient.post(ApiEndpoints.gateway.createWebhook(), values), onSuccess: () => { toast({ title: 'Success', description: 'Webhook created successfully', }) queryClient.invalidateQueries({ queryKey: ['webhooks'] }) onOpenChange(false) form.reset() }, onError: () => { toast({ title: 'Error', description: 'Failed to create webhook', variant: 'destructive', }) }, }) const onSubmit = (values: z.infer) => { createWebhookMutation.mutate(values) } return ( Create Webhook Configure your webhook endpoint to receive real-time SMS notifications.
( Delivery URL The URL where webhook notifications will be sent via POST requests )} /> ( Signing Secret
Used to verify webhook payload authenticity
)} /> ( Events Choose the events you want to receive notifications for )} />
) }