feat: add edit profile page

This commit is contained in:
gosangam
2026-06-15 23:54:34 +05:30
parent 0608e2b6f6
commit 197e13c0ef
4 changed files with 146 additions and 119 deletions
+72 -68
View File
@@ -1,23 +1,27 @@
"use client";
/**
* ProfileEditForm — the editor block used in /u/me's edit mode.
* ProfileEditForm — the editor used on the dedicated /settings/profile page.
*
* Three editable fields:
* • Display name (PATCH /users/me { name })
* • Handle (PATCH /users/me { handle }) — 409 if taken
* • Avatar (R2 presign + PATCH /users/me { avatar_url })
* Three groups of editable fields, rendered as plain page sections (no card
* chrome) so the page reads as a normal settings-style screen rather than a
* widget:
* • Profile photo (R2 presign + PATCH /users/me { avatar_url })
* • Account (name, handle, country, email)
* • Devotional details (sampradāya, gender, initiation, daily rounds)
*
* `onDone` is invoked after a successful save OR when the user clicks
* "Cancel" — the parent flips back to view mode.
* The page owns the title + back button; this component renders only the
* sections and the Save/Cancel footer. `onDone` runs on "Cancel" (the parent
* navigates back to the profile). Saving stays on the page with a toast and
* re-baselines the form against the freshly-saved user.
*
* No log-out button here — that's handled by the sidebar footer so it lives
* outside the edit context.
* No log-out button here — that lives in the sidebar/settings footer so it
* stays outside the edit context.
*/
import { useRef, useState } from "react";
import Image from "next/image";
import { ImagePlus, Save, X } from "lucide-react";
import { ImagePlus, Save } from "lucide-react";
import { toast } from "sonner";
import { useAuth } from "@/components/auth/auth-provider";
import { auth, AuthError, type User } from "@/lib/auth";
@@ -135,10 +139,9 @@ export function ProfileEditForm({
}
const updated = await auth.updateProfile(patch);
// Update the cached user in place and STAY on the edit form. The
// form's fields re-baseline against the new `user` prop (so it's no
// longer "dirty"), and we keep the ?edit=1 URL rather than bouncing
// back to the profile view. Exit is via Cancel/✕.
// Update the cached user in place and STAY on the page. The form's
// fields re-baseline against the new `user` prop (so it's no longer
// "dirty"); exit is via the page's back button or Cancel.
setUser(updated);
toast.success("Profile saved");
} catch (err) {
@@ -169,60 +172,44 @@ export function ProfileEditForm({
}
return (
<form onSubmit={onSubmit} className="space-y-8 rounded-2xl bg-card ring-1 ring-foreground/10 p-5 sm:p-6">
<div className="flex items-center justify-between">
<h2 className="font-[family-name:var(--font-title)] text-xl sm:text-2xl font-semibold text-foreground">
Edit profile
</h2>
<button
type="button"
onClick={onDone}
aria-label="Cancel"
className="size-9 rounded-lg text-muted-foreground hover:text-foreground hover:bg-muted inline-flex items-center justify-center"
>
<X className="size-4" />
</button>
</div>
{/* Avatar — same widget pattern as the old /profile form */}
<section className="flex items-center gap-4">
<AvatarPreview src={avatarUrl} name={user.name} />
<div className="flex flex-col gap-2">
<input
ref={fileInput}
type="file"
accept="image/png,image/jpeg,image/webp,image/gif"
hidden
onChange={onAvatarChange}
/>
<Button
variant="outline"
type="button"
onClick={() => fileInput.current?.click()}
disabled={uploading || saving}
className="h-10 px-4 text-sm gap-1.5"
>
<ImagePlus className="size-4" />
{uploading ? "Uploading…" : avatarUrl ? "Change" : "Upload"}
</Button>
{avatarUrl ? (
<form onSubmit={onSubmit} className="space-y-10">
<Section title="Profile photo">
<div className="flex items-center gap-4">
<AvatarPreview src={avatarUrl} name={user.name} />
<div className="flex flex-col gap-2">
<input
ref={fileInput}
type="file"
accept="image/png,image/jpeg,image/webp,image/gif"
hidden
onChange={onAvatarChange}
/>
<Button
variant="ghost"
variant="outline"
type="button"
onClick={() => setAvatarUrl(null)}
onClick={() => fileInput.current?.click()}
disabled={uploading || saving}
className="h-10 px-4 text-sm"
className="h-10 px-4 text-sm gap-1.5"
>
Remove
<ImagePlus className="size-4" />
{uploading ? "Uploading…" : avatarUrl ? "Change" : "Upload"}
</Button>
) : null}
{avatarUrl ? (
<Button
variant="ghost"
type="button"
onClick={() => setAvatarUrl(null)}
disabled={uploading || saving}
className="h-10 px-4 text-sm"
>
Remove
</Button>
) : null}
</div>
</div>
</section>
</Section>
<div className="space-y-4 pt-6 border-t border-border">
<h3 className="text-sm font-semibold uppercase tracking-wide text-muted-foreground">
Account
</h3>
<Section title="Account">
<div className="space-y-1.5">
<Label htmlFor="profile-name">Display name</Label>
<Input
@@ -292,13 +279,9 @@ export function ProfileEditForm({
<AddEmailInline />
)}
</div>
</div>
<div className="space-y-4 pt-6 border-t border-border">
<h3 className="text-sm font-semibold uppercase tracking-wide text-muted-foreground">
Devotional details
</h3>
</Section>
<Section title="Devotional details">
<div className="space-y-1.5">
<Label htmlFor="profile-sampradaya">Sampradāya</Label>
<Combobox
@@ -375,7 +358,7 @@ export function ProfileEditForm({
Your daily japa vow (1–1008). Leave blank if you&apos;d rather not say.
</p>
</div>
</div>
</Section>
{error ? <p className="text-sm text-destructive">{error}</p> : null}
@@ -401,6 +384,27 @@ export function ProfileEditForm({
);
}
/** A plain page section: a bottom-bordered title over its fields. Matches the
* /settings page section style so the editor reads as a normal page. */
function Section({
title,
children,
}: {
title: string;
children: React.ReactNode;
}) {
return (
<section className="space-y-4">
<div className="border-b border-border pb-3">
<h2 className="font-[family-name:var(--font-title)] text-xl font-semibold text-foreground">
{title}
</h2>
</div>
<div className="space-y-4">{children}</div>
</section>
);
}
function AddEmailInline() {
const { setUser } = useAuth();
const [email, setEmail] = useState("");