'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 { useEffect, useState } from 'react' 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 EditWebhookDialogProps { open: boolean onOpenChange: (open: boolean) => void webhook: WebhookData } export function EditWebhookDialog({ open, onOpenChange, webhook, }: EditWebhookDialogProps) { const queryClient = useQueryClient() const { toast } = useToast() const form = useForm>({ resolver: zodResolver(formSchema), values: { deliveryUrl: webhook.deliveryUrl, events: webhook.events, isActive: webhook.isActive, signingSecret: webhook.signingSecret, }, }) const { mutate: updateWebhook, isPending } = useMutation({ mutationFn: async (values: z.infer) => { return httpBrowserClient.patch( ApiEndpoints.gateway.updateWebhook(webhook._id), values ) }, onSuccess: () => { toast({ title: 'Success', description: 'Webhook updated successfully', }) // Invalidate and refetch webhooks list queryClient.invalidateQueries({ queryKey: ['webhooks'] }) onOpenChange(false) }, onError: () => { toast({ title: 'Error', description: 'Failed to update webhook', variant: 'destructive', }) }, }) const onSubmit = (values: z.infer) => { updateWebhook(values) } return ( Edit Webhook Update your webhook configuration.
( 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 )} />
) }