edit-url-modal.tsx158 lines, 4.4 KB
| 1 | import { updateUrl } from "@/server/actions/urls/update-url"; |
| 2 | import { zodResolver } from "@hookform/resolvers/zod"; |
| 3 | import { useEffect, useState } from "react"; |
| 4 | import { useForm } from "react-hook-form"; |
| 5 | import { toast } from "sonner"; |
| 6 | import { z } from "zod"; |
| 7 | import { |
| 8 | Dialog, |
| 9 | DialogContent, |
| 10 | DialogDescription, |
| 11 | DialogFooter, |
| 12 | DialogHeader, |
| 13 | DialogTitle, |
| 14 | } from "../ui/dialog"; |
| 15 | import { |
| 16 | Form, |
| 17 | FormControl, |
| 18 | FormField, |
| 19 | FormItem, |
| 20 | FormMessage, |
| 21 | } from "../ui/form"; |
| 22 | import { Input } from "../ui/input"; |
| 23 | import { Button } from "../ui/button"; |
| 24 | import { Loader2 } from "lucide-react"; |
| 25 | |
| 26 | const editUrlSchema = z.object({ |
| 27 | customCode: z |
| 28 | .string() |
| 29 | .min(3, "Custom code must be at least 3 characters") |
| 30 | .max(255, "Custom code must be less than 255 characters") |
| 31 | .regex(/^[a-zA-Z0-9_-]+$/, "Custom code must be alphanumeric or hyphen"), |
| 32 | }); |
| 33 | |
| 34 | type EditUrlFormData = z.infer<typeof editUrlSchema>; |
| 35 | |
| 36 | interface EditUrlModalProps { |
| 37 | isOpen: boolean; |
| 38 | onOpenChange: (open: boolean) => void; |
| 39 | urlId: number; |
| 40 | currentShortCode: string; |
| 41 | onSuccess: (newShortCode: string) => void; |
| 42 | } |
| 43 | |
| 44 | export function EditUrlModal({ |
| 45 | isOpen, |
| 46 | onOpenChange, |
| 47 | urlId, |
| 48 | currentShortCode, |
| 49 | onSuccess, |
| 50 | }: EditUrlModalProps) { |
| 51 | const [isLoading, setIsLoading] = useState(false); |
| 52 | |
| 53 | const baseUrl = process.env.NEXT_PUBLIC_APP_URL || window.location.origin; |
| 54 | |
| 55 | const form = useForm<EditUrlFormData>({ |
| 56 | resolver: zodResolver(editUrlSchema), |
| 57 | defaultValues: { |
| 58 | customCode: currentShortCode, |
| 59 | }, |
| 60 | }); |
| 61 | |
| 62 | useEffect(() => { |
| 63 | form.reset({ customCode: currentShortCode }); |
| 64 | }, [currentShortCode]); |
| 65 | |
| 66 | const onSubmit = async (data: EditUrlFormData) => { |
| 67 | setIsLoading(true); |
| 68 | |
| 69 | try { |
| 70 | const formData = new FormData(); |
| 71 | formData.append("id", urlId.toString()); |
| 72 | formData.append("customCode", data.customCode); |
| 73 | |
| 74 | const response = await updateUrl(formData); |
| 75 | |
| 76 | if (response.success && response.data) { |
| 77 | toast.success("URL updated successfully", { |
| 78 | description: "The URL has been updated successfully", |
| 79 | }); |
| 80 | onSuccess(data.customCode); |
| 81 | onOpenChange(false); |
| 82 | } else { |
| 83 | toast.error("Failed to update URL", { |
| 84 | description: response.error || "An error occurred", |
| 85 | }); |
| 86 | } |
| 87 | } catch (error) { |
| 88 | console.error("Failed to update URL", error); |
| 89 | toast.error("Failed to update URL", { |
| 90 | description: "An error occurred", |
| 91 | }); |
| 92 | } finally { |
| 93 | setIsLoading(false); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | return ( |
| 98 | <Dialog open={isOpen} onOpenChange={onOpenChange}> |
| 99 | <DialogContent className="sm:max-w-md"> |
| 100 | <DialogHeader> |
| 101 | <DialogTitle>Edit Short URL</DialogTitle> |
| 102 | <DialogDescription> |
| 103 | Customize the short code for this URL. The short code must be unique |
| 104 | and can only contain letters, numbers, hyphens, and underscores. |
| 105 | </DialogDescription> |
| 106 | </DialogHeader> |
| 107 | |
| 108 | <Form {...form}> |
| 109 | <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 110 | <FormField |
| 111 | control={form.control} |
| 112 | name="customCode" |
| 113 | render={({ field }) => ( |
| 114 | <FormItem> |
| 115 | <FormControl> |
| 116 | <div className="flex items-center"> |
| 117 | <span className="text-sm text-muted-foreground mr-2"> |
| 118 | {baseUrl}/r/ |
| 119 | </span> |
| 120 | <Input |
| 121 | placeholder="Custom code" |
| 122 | {...field} |
| 123 | disabled={isLoading} |
| 124 | className="flex-1" |
| 125 | /> |
| 126 | </div> |
| 127 | </FormControl> |
| 128 | <FormMessage /> |
| 129 | </FormItem> |
| 130 | )} |
| 131 | /> |
| 132 | |
| 133 | <DialogFooter className="sm:justify-end"> |
| 134 | <Button |
| 135 | type="button" |
| 136 | variant={"outline"} |
| 137 | onClick={() => onOpenChange(false)} |
| 138 | disabled={isLoading} |
| 139 | > |
| 140 | Cancel |
| 141 | </Button> |
| 142 | <Button type="submit" disabled={isLoading}> |
| 143 | {isLoading ? ( |
| 144 | <> |
| 145 | <Loader2 className="animate-spin size-4 mr-2" /> |
| 146 | Updating... |
| 147 | </> |
| 148 | ) : ( |
| 149 | "Save Changes" |
| 150 | )} |
| 151 | </Button> |
| 152 | </DialogFooter> |
| 153 | </form> |
| 154 | </Form> |
| 155 | </DialogContent> |
| 156 | </Dialog> |
| 157 | ); |
| 158 | } |