// src/components/EditModal.tsx import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import * as z from 'zod'; import { Link } from '../types/api'; import { editLink } from '../api/client'; import { useToast } from '@/hooks/use-toast'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; const formSchema = z.object({ url: z .string() .min(1, 'URL is required') .url('Must be a valid URL') .refine((val) => val.startsWith('http://') || val.startsWith('https://'), { message: 'URL must start with http:// or https://', }), custom_code: z .string() .regex(/^[a-zA-Z0-9_-]{1,32}$/, { message: 'Custom code must be 1-32 characters and contain only letters, numbers, underscores, and hyphens', }) .optional(), }); interface EditModalProps { isOpen: boolean; onClose: () => void; link: Link; onSuccess: () => void; } export function EditModal({ isOpen, onClose, link, onSuccess }: EditModalProps) { const [loading, setLoading] = useState(false); const { toast } = useToast(); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { url: link.original_url, custom_code: link.short_code, }, }); const onSubmit = async (values: z.infer) => { try { setLoading(true); await editLink(link.id, values); toast({ description: 'Link updated successfully', }); onSuccess(); onClose(); } catch (err: unknown) { const error = err as { response?: { data?: { error?: string } } }; toast({ variant: 'destructive', title: 'Error', description: error.response?.data?.error || 'Failed to update link', }); } finally { setLoading(false); } }; return ( Edit Link
( Destination URL )} /> ( Short Code )} />
); }