Iot / src /components /edit-profile-dialog.tsx
veltrixcode's picture
Upload 74 files
64fa001 verified
Raw History Blame Contribute Delete
5.92 kB
"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<typeof profileSchema>
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<ProfileFormValues>({
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 (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline">Edit Profile</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px] glass-panel border-2">
<DialogHeader>
<DialogTitle>Edit Profile</DialogTitle>
<DialogDescription>
Make changes to your profile here. Click save when you're done.
</DialogDescription>
</DialogHeader>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6 py-4">
<div className="grid gap-2">
<Label htmlFor="name">Name</Label>
<Input
id="name"
{...form.register("name")}
className="neo-brutalist"
/>
{form.formState.errors.name && (
<p className="text-xs text-destructive">{form.formState.errors.name.message}</p>
)}
</div>
<div className="grid gap-2">
<Label htmlFor="username">Username</Label>
<Input
id="username"
{...form.register("username")}
className="neo-brutalist"
/>
{form.formState.errors.username && (
<p className="text-xs text-destructive">{form.formState.errors.username.message}</p>
)}
</div>
<DialogFooter>
<Button type="submit" disabled={isLoading} className="neo-brutalist">
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Save changes
</Button>
</DialogFooter>
</form>
<div className="border-t border-destructive/20 pt-4 mt-2">
<h4 className="text-sm font-bold text-destructive mb-2 flex items-center">
<AlertTriangle className="h-4 w-4 mr-2" />
Danger Zone
</h4>
<p className="text-xs text-muted-foreground mb-4">
Permanently delete your account and all associated data.
</p>
<Button
variant="destructive"
size="sm"
className="w-full neo-brutalist"
onClick={handleDeleteAccount}
disabled={isDeleting}
>
{isDeleting ? "Deleting..." : "Delete Account"}
</Button>
</div>
</DialogContent>
</Dialog>
)
}