"use client" import { useState } from "react" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { z } from "zod" import { AlertTriangle, Loader2 } from "lucide-react" const profileSchema = z.object({ name: z.string().min(2, "Name must be at least 2 characters"), username: z.string().min(3, "Username must be at least 3 characters").regex(/^[a-zA-Z0-9_]+$/, "Username must be alphanumeric"), }) type ProfileFormValues = z.infer interface EditProfileDialogProps { user: { name?: string | null username?: string | null email?: string | null } } export function EditProfileDialog({ user }: EditProfileDialogProps) { const router = useRouter() const [open, setOpen] = useState(false) const [isLoading, setIsLoading] = useState(false) const [isDeleting, setIsDeleting] = useState(false) const form = useForm({ resolver: zodResolver(profileSchema), defaultValues: { name: user.name || "", username: user.username || "", }, }) const onSubmit = async (data: ProfileFormValues) => { setIsLoading(true) try { const res = await fetch("/api/user", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }) if (!res.ok) { const error = await res.text() // You might want to use a toast here alert(error) return } setOpen(false) router.refresh() } catch (error) { console.error(error) } finally { setIsLoading(false) } } const handleDeleteAccount = async () => { if (!confirm("Are you ABSOLUTELY sure? This will permanently delete your account and all generated images. This action cannot be undone.")) { return } setIsDeleting(true) try { const res = await fetch("/api/user", { method: "DELETE", }) if (!res.ok) throw new Error("Failed to delete account") // Redirect to home/login after deletion window.location.href = "/" } catch (error) { console.error(error) alert("Failed to delete account. Please try again.") setIsDeleting(false) } } return ( Edit Profile Make changes to your profile here. Click save when you're done.
{form.formState.errors.name && (

{form.formState.errors.name.message}

)}
{form.formState.errors.username && (

{form.formState.errors.username.message}

)}

Danger Zone

Permanently delete your account and all associated data.

) }