|
| 1 | +// src/app/profile/ProfileForm.tsx |
| 2 | + |
| 3 | +"use client"; |
| 4 | + |
| 5 | +import { useTransition, FormEvent } from "react"; |
| 6 | +import { Loader2 } from "lucide-react"; |
| 7 | + |
| 8 | +import { |
| 9 | + Card, |
| 10 | + CardHeader, |
| 11 | + CardTitle, |
| 12 | + CardDescription, |
| 13 | + CardContent, |
| 14 | + CardFooter, |
| 15 | +} from "@/components/ui/card"; |
| 16 | +import { Input } from "@/components/ui/input"; |
| 17 | +import { Label } from "@/components/ui/label"; |
| 18 | +import { |
| 19 | + Avatar, |
| 20 | + AvatarImage, |
| 21 | + AvatarFallback, |
| 22 | +} from "@/components/ui/avatar"; |
| 23 | +import { Button } from "@/components/ui/button"; |
| 24 | +import { toast } from "sonner"; |
| 25 | +import { updateProfile } from "@/actions/update-profile"; |
| 26 | + |
| 27 | +type ProfileFormProps = { |
| 28 | + name: string; |
| 29 | + email: string; |
| 30 | + image: string | null; |
| 31 | + initials: string; |
| 32 | +}; |
| 33 | + |
| 34 | +export function ProfileForm({ name, email, image, initials }: ProfileFormProps) { |
| 35 | + const [isPending, startTransition] = useTransition(); |
| 36 | + |
| 37 | + const handleSubmit = (e: FormEvent<HTMLFormElement>) => { |
| 38 | + e.preventDefault(); |
| 39 | + const formData = new FormData(e.currentTarget); |
| 40 | + |
| 41 | + startTransition(async () => { |
| 42 | + try { |
| 43 | + await updateProfile(formData); |
| 44 | + toast.success("Profile updated", { |
| 45 | + description: "Your changes have been saved.", |
| 46 | + }); |
| 47 | + } catch (err) { |
| 48 | + console.error(err); |
| 49 | + toast.error("Update failed", { |
| 50 | + description: "Something went wrong while saving your profile.", |
| 51 | + }); |
| 52 | + } |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <Card className="border border-border/60 shadow-sm"> |
| 58 | + <CardHeader> |
| 59 | + <div className="flex items-center gap-4"> |
| 60 | + <Avatar className="h-16 w-16"> |
| 61 | + {image ? ( |
| 62 | + <AvatarImage src={image} alt={name || email} /> |
| 63 | + ) : ( |
| 64 | + <> |
| 65 | + <AvatarImage src="/user.png" alt="User avatar" /> |
| 66 | + <AvatarFallback>{initials}</AvatarFallback> |
| 67 | + </> |
| 68 | + )} |
| 69 | + </Avatar> |
| 70 | + |
| 71 | + <div className="space-y-1"> |
| 72 | + <CardTitle className="text-xl">Your Profile</CardTitle> |
| 73 | + <CardDescription> |
| 74 | + View and update your account information. |
| 75 | + </CardDescription> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </CardHeader> |
| 79 | + |
| 80 | + <form onSubmit={handleSubmit}> |
| 81 | + <CardContent className="space-y-6"> |
| 82 | + {/* Name (editable) */} |
| 83 | + <div className="grid gap-2"> |
| 84 | + <Label htmlFor="name">Name</Label> |
| 85 | + <Input |
| 86 | + id="name" |
| 87 | + name="name" |
| 88 | + defaultValue={name} |
| 89 | + placeholder="Your name" |
| 90 | + required |
| 91 | + disabled={isPending} |
| 92 | + /> |
| 93 | + </div> |
| 94 | + |
| 95 | + {/* Email (read-only) */} |
| 96 | + <div className="grid gap-2"> |
| 97 | + <Label htmlFor="email">Email</Label> |
| 98 | + <Input |
| 99 | + id="email" |
| 100 | + value={email} |
| 101 | + disabled |
| 102 | + className="bg-muted/50" |
| 103 | + /> |
| 104 | + <p className="text-xs text-muted-foreground"> |
| 105 | + Email is managed by authentication and cannot be changed here. |
| 106 | + </p> |
| 107 | + </div> |
| 108 | + |
| 109 | + {/* Avatar URL (editable) */} |
| 110 | + <div className="grid gap-2"> |
| 111 | + <Label htmlFor="image">Avatar URL</Label> |
| 112 | + <Input |
| 113 | + id="image" |
| 114 | + name="image" |
| 115 | + defaultValue={image ?? ""} |
| 116 | + placeholder="https://example.com/avatar.png" |
| 117 | + disabled={isPending} |
| 118 | + /> |
| 119 | + <p className="text-xs text-muted-foreground"> |
| 120 | + Paste a direct image URL. Later you can replace this with an |
| 121 | + upload flow using your S3/blob system. |
| 122 | + </p> |
| 123 | + </div> |
| 124 | + </CardContent> |
| 125 | + |
| 126 | + <CardFooter className="flex justify-center pt-4 gap-2"> |
| 127 | + <Button type="submit" disabled={isPending}> |
| 128 | + {isPending && ( |
| 129 | + <Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
| 130 | + )} |
| 131 | + {isPending ? "Saving..." : "Save changes"} |
| 132 | + </Button> |
| 133 | + </CardFooter> |
| 134 | + </form> |
| 135 | + </Card> |
| 136 | + ); |
| 137 | +} |
0 commit comments