feat(profile): improve profile editing UX flow
This commit is contained in:
@@ -8,7 +8,7 @@ import {
|
|||||||
removeProfilePicture
|
removeProfilePicture
|
||||||
} from "../api/users"
|
} from "../api/users"
|
||||||
import { Button } from "../components/ui/button"
|
import { Button } from "../components/ui/button"
|
||||||
import { Camera, Edit2, Trash2, User as UserIcon, UploadCloud } from "lucide-react"
|
import { Camera, Edit2, Trash2, User as UserIcon, UploadCloud, X, Check } from "lucide-react"
|
||||||
import JalaliDatePicker from "../components/ui/JalaliDatePicker"
|
import JalaliDatePicker from "../components/ui/JalaliDatePicker"
|
||||||
import { toast } from "sonner"
|
import { toast } from "sonner"
|
||||||
import { Modal } from "../components/Modal"
|
import { Modal } from "../components/Modal"
|
||||||
@@ -54,8 +54,8 @@ export default function Profile() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modals state
|
// Modals & Editing state
|
||||||
const [isEditModalOpen, setIsEditModalOpen] = useState(false)
|
const [isEditing, setIsEditing] = useState(false)
|
||||||
const [isPicModalOpen, setIsPicModalOpen] = useState(false)
|
const [isPicModalOpen, setIsPicModalOpen] = useState(false)
|
||||||
const [isSaving, setIsSaving] = useState(false)
|
const [isSaving, setIsSaving] = useState(false)
|
||||||
|
|
||||||
@@ -89,10 +89,15 @@ export default function Profile() {
|
|||||||
description: user.description || "",
|
description: user.description || "",
|
||||||
birth_date: user.birth_date || "",
|
birth_date: user.birth_date || "",
|
||||||
})
|
})
|
||||||
setIsEditModalOpen(true)
|
setIsEditing(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleCancelEdit = () => {
|
||||||
|
setIsEditing(false)
|
||||||
|
setFormData({})
|
||||||
|
}
|
||||||
|
|
||||||
const handleSaveProfile = async () => {
|
const handleSaveProfile = async () => {
|
||||||
setIsSaving(true)
|
setIsSaving(true)
|
||||||
try {
|
try {
|
||||||
@@ -108,7 +113,7 @@ export default function Profile() {
|
|||||||
|
|
||||||
const updatedUser = await updateUserProfile(payload)
|
const updatedUser = await updateUserProfile(payload)
|
||||||
setUser(prev => prev ? { ...prev, ...updatedUser } : updatedUser)
|
setUser(prev => prev ? { ...prev, ...updatedUser } : updatedUser)
|
||||||
setIsEditModalOpen(false)
|
setIsEditing(false)
|
||||||
toast.success(t.profile.toasts.successEdit)
|
toast.success(t.profile.toasts.successEdit)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error(t.profile.toasts.error)
|
toast.error(t.profile.toasts.error)
|
||||||
@@ -209,145 +214,150 @@ export default function Profile() {
|
|||||||
</h1>
|
</h1>
|
||||||
<p className="text-slate-500 dark:text-slate-400 mt-1">{user.email || "-"}</p>
|
<p className="text-slate-500 dark:text-slate-400 mt-1">{user.email || "-"}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button onClick={handleEditClick} className="flex items-center gap-2">
|
|
||||||
<Edit2 className="h-4 w-4" />
|
|
||||||
{t.profile?.editInfo || 'Edit Profile'}
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Details Card */}
|
{/* Details Card */}
|
||||||
<div className="rounded-xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
|
<div className="rounded-xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
|
||||||
<h2 className="text-lg font-bold text-slate-900 dark:text-white mb-6 border-b border-slate-100 dark:border-slate-800 pb-4">
|
|
||||||
{t.profile?.title || 'Personal Information'}
|
{/* Header with Title and Edit Button */}
|
||||||
</h2>
|
<div className="flex items-center justify-between mb-6 border-b border-slate-100 dark:border-slate-800 pb-4">
|
||||||
|
<h2 className="text-lg font-bold text-slate-900 dark:text-white">
|
||||||
|
{t.profile?.title || 'Personal Information'}
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
{!isEditing && (
|
||||||
|
<Button onClick={handleEditClick} className="flex items-center gap-2">
|
||||||
|
<Edit2 className="h-4 w-4" />
|
||||||
|
{t.profile?.editInfo || 'Edit Profile'}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||||
|
|
||||||
|
{/* Row 1: First Name | Last Name */}
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.firstName || 'First Name'}</span>
|
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.firstName || 'First Name'}</span>
|
||||||
<p className="font-medium text-slate-900 dark:text-slate-100">{user.first_name || "-"}</p>
|
{isEditing ? (
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={formData.first_name || ""}
|
||||||
|
onChange={(e) => setFormData({ ...formData, first_name: e.target.value })}
|
||||||
|
disabled={isSaving}
|
||||||
|
className="mt-1 w-full rounded-md border border-slate-300 bg-transparent px-3 py-2 text-sm dark:border-slate-700 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<p className="font-medium text-slate-900 dark:text-slate-100 min-h-[38px] flex items-center">{user.first_name || "-"}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.lastName || 'Last Name'}</span>
|
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.lastName || 'Last Name'}</span>
|
||||||
<p className="font-medium text-slate-900 dark:text-slate-100">{user.last_name || "-"}</p>
|
{isEditing ? (
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={formData.last_name || ""}
|
||||||
|
onChange={(e) => setFormData({ ...formData, last_name: e.target.value })}
|
||||||
|
disabled={isSaving}
|
||||||
|
className="mt-1 w-full rounded-md border border-slate-300 bg-transparent px-3 py-2 text-sm dark:border-slate-700 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<p className="font-medium text-slate-900 dark:text-slate-100 min-h-[38px] flex items-center">{user.last_name || "-"}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Row 2: Mobile | Email */}
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.mobileNumber || 'Mobile'}</span>
|
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.mobileNumber || 'Mobile'}</span>
|
||||||
<p className="font-medium text-slate-900 dark:text-slate-100 flex items-center">
|
<p className="font-medium text-slate-900 dark:text-slate-100 min-h-[38px] flex items-center">
|
||||||
{toPersianNum(user.mobile)}
|
{toPersianNum(user.mobile)}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.birthDate || 'Birth Date'}</span>
|
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.email || 'Email'}</span>
|
||||||
<p className="font-medium text-slate-900 dark:text-slate-100">
|
{isEditing ? (
|
||||||
{formatDate(user.birth_date)} {user.age ? `(${toPersianNum(user.age)} ${t.profile?.yearsOld})` : ""}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div className="space-y-1 md:col-span-2">
|
|
||||||
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.description || 'Description'}</span>
|
|
||||||
<p className="font-medium text-slate-900 dark:text-slate-100 whitespace-pre-wrap">{user.description || "-"}</p>
|
|
||||||
</div>
|
|
||||||
{user.created_at && (
|
|
||||||
<div className="space-y-1 md:col-span-2">
|
|
||||||
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.dateJoined || 'Date Joined'}</span>
|
|
||||||
<p className="font-medium text-slate-900 dark:text-slate-100">
|
|
||||||
{formatDate(user.created_at)}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Edit Profile Modal */}
|
|
||||||
{isEditModalOpen && (
|
|
||||||
<Modal
|
|
||||||
isOpen={isEditModalOpen}
|
|
||||||
isFa={isFa}
|
|
||||||
onClose={() => setIsEditModalOpen(false)}
|
|
||||||
title={t.profile?.title || "Edit Profile"}
|
|
||||||
maxWidth="max-w-lg"
|
|
||||||
footer={
|
|
||||||
<>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={() => setIsEditModalOpen(false)}
|
|
||||||
className="px-4 py-2 text-sm font-medium text-slate-700 dark:text-slate-300 hover:bg-slate-200 dark:hover:bg-slate-700 rounded-lg"
|
|
||||||
>
|
|
||||||
{t.cancel || "Cancel"}
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={handleSaveProfile}
|
|
||||||
disabled={isSaving}
|
|
||||||
className="px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-lg disabled:opacity-50"
|
|
||||||
>
|
|
||||||
{isSaving ? "Saving..." : t.profile?.save || "Save"}
|
|
||||||
</button>
|
|
||||||
</>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<div className="space-y-4">
|
|
||||||
|
|
||||||
<div className="space-y-4">
|
|
||||||
<div className="grid grid-cols-2 gap-4">
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium dark:text-slate-300">{t.profile?.firstName || 'First Name'}</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={formData.first_name || ""}
|
|
||||||
onChange={(e) => setFormData({ ...formData, first_name: e.target.value })}
|
|
||||||
className="mt-1 w-full rounded-md border border-slate-300 bg-transparent px-3 py-2 text-sm dark:border-slate-700 dark:text-white"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium dark:text-slate-300">{t.profile?.lastName || 'Last Name'}</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={formData.last_name || ""}
|
|
||||||
onChange={(e) => setFormData({ ...formData, last_name: e.target.value })}
|
|
||||||
className="mt-1 w-full rounded-md border border-slate-300 bg-transparent px-3 py-2 text-sm dark:border-slate-700 dark:text-white"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<label className="text-sm font-medium dark:text-slate-300">{t.profile?.email || 'Email'}</label>
|
|
||||||
<input
|
<input
|
||||||
type="email"
|
type="email"
|
||||||
value={formData.email || ""}
|
value={formData.email || ""}
|
||||||
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
||||||
className="mt-1 w-full rounded-md border border-slate-300 bg-transparent px-3 py-2 text-sm dark:border-slate-700 dark:text-white flex-row-reverse"
|
disabled={isSaving}
|
||||||
dir="ltr"
|
dir="ltr"
|
||||||
|
className="mt-1 w-full rounded-md border border-slate-300 bg-transparent px-3 py-2 text-sm dark:border-slate-700 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||||
/>
|
/>
|
||||||
</div>
|
) : (
|
||||||
|
<p className="font-medium text-slate-900 dark:text-slate-100 min-h-[38px] flex items-center">{user.email || "-"}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
<JalaliDatePicker
|
{/* Row 3: Birth Date | Date Joined */}
|
||||||
label={t.profile?.birthDate || 'Birth Date'}
|
<div className="space-y-1">
|
||||||
value={formData.birth_date}
|
{isEditing ? (
|
||||||
onChange={(date) => setFormData({ ...formData, birth_date: date })}
|
<div className="pt-1">
|
||||||
disabled={isSaving}
|
<JalaliDatePicker
|
||||||
/>
|
label={t.profile?.birthDate || 'Birth Date'}
|
||||||
|
value={formData.birth_date}
|
||||||
|
onChange={(date) => setFormData({ ...formData, birth_date: date })}
|
||||||
|
disabled={isSaving}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.birthDate || 'Birth Date'}</span>
|
||||||
|
<p className="font-medium text-slate-900 dark:text-slate-100 min-h-[38px] flex items-center">
|
||||||
|
{formatDate(user.birth_date)} {user.age ? `(${toPersianNum(user.age)} ${t.profile?.yearsOld || 'years'})` : ""}
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div>
|
<div className="space-y-1">
|
||||||
<label className="text-sm font-medium dark:text-slate-300">{t.profile?.description || 'Description'}</label>
|
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.dateJoined || 'Date Joined'}</span>
|
||||||
|
<p className="font-medium text-slate-900 dark:text-slate-100 min-h-[38px] flex items-center">
|
||||||
|
{formatDate(user.created_at)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Row 4: Description (Full Width) */}
|
||||||
|
<div className="space-y-1 md:col-span-2">
|
||||||
|
<span className="text-sm font-medium text-slate-500 dark:text-slate-400">{t.profile?.description || 'Description'}</span>
|
||||||
|
{isEditing ? (
|
||||||
<textarea
|
<textarea
|
||||||
rows={3}
|
rows={4}
|
||||||
value={formData.description || ""}
|
value={formData.description || ""}
|
||||||
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
||||||
className="mt-1 w-full rounded-md border border-slate-300 bg-transparent px-3 py-2 text-sm dark:border-slate-700 dark:text-white resize-none"
|
disabled={isSaving}
|
||||||
|
className="mt-1 w-full rounded-md border border-slate-300 bg-transparent px-3 py-2 text-sm dark:border-slate-700 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500 resize-y"
|
||||||
/>
|
/>
|
||||||
</div>
|
) : (
|
||||||
|
<p className="font-medium text-slate-900 dark:text-slate-100 whitespace-pre-wrap mt-1">{user.description || "-"}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
|
||||||
)}
|
{/* Save/Cancel Buttons at Bottom (Visible only when editing) */}
|
||||||
|
{isEditing && (
|
||||||
|
<div className="mt-8 pt-4 border-t border-slate-100 dark:border-slate-800 flex justify-end items-center gap-3">
|
||||||
|
<Button variant="outline" onClick={handleCancelEdit} disabled={isSaving} className="flex items-center gap-2">
|
||||||
|
<X className="h-4 w-4" />
|
||||||
|
{t.cancel || 'Cancel'}
|
||||||
|
</Button>
|
||||||
|
<Button onClick={handleSaveProfile} disabled={isSaving} className="flex items-center gap-2 bg-blue-600 text-white hover:bg-blue-700">
|
||||||
|
<Check className="h-4 w-4" />
|
||||||
|
{isSaving ? "Saving..." : (t.profile?.save || 'Save')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Profile Picture Modal */}
|
{/* Profile Picture Modal */}
|
||||||
|
|
||||||
{isPicModalOpen && (
|
{isPicModalOpen && (
|
||||||
<Modal
|
<Modal
|
||||||
isOpen={isPicModalOpen}
|
isOpen={isPicModalOpen}
|
||||||
|
isFa={isFa}
|
||||||
onClose={() => !isSaving && setIsPicModalOpen(false)}
|
onClose={() => !isSaving && setIsPicModalOpen(false)}
|
||||||
title={t.profile?.changePicture || 'Profile Picture'}
|
title={t.profile?.changePicture || 'Profile Picture'}
|
||||||
maxWidth="max-w-sm"
|
maxWidth="max-w-sm"
|
||||||
|
|||||||
Reference in New Issue
Block a user