initial commit
This commit is contained in:
423
src/pages/Profile.tsx
Normal file
423
src/pages/Profile.tsx
Normal file
@@ -0,0 +1,423 @@
|
||||
import { useEffect, useState, useRef } from "react"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
import { useTranslation } from "../hooks/useTranslation"
|
||||
import {
|
||||
getUserProfile,
|
||||
updateUserProfile,
|
||||
updateProfilePicture,
|
||||
removeProfilePicture
|
||||
} from "../api/users"
|
||||
import { Button } from "../components/ui/button"
|
||||
import { Camera, Edit2, Trash2, User as UserIcon, UploadCloud } from "lucide-react"
|
||||
import JalaliDatePicker from "../components/ui/JalaliDatePicker"
|
||||
import { toast } from "sonner"
|
||||
|
||||
export interface UserProfile {
|
||||
id?: string;
|
||||
email?: string;
|
||||
mobile?: string;
|
||||
first_name?: string;
|
||||
last_name?: string;
|
||||
full_name?: string;
|
||||
description?: string;
|
||||
birth_date?: string;
|
||||
age?: number;
|
||||
profile_picture?: string;
|
||||
created_at?: string;
|
||||
}
|
||||
|
||||
export default function Profile() {
|
||||
const navigate = useNavigate()
|
||||
const [user, setUser] = useState<UserProfile | null>(null)
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
|
||||
const { t, lang } = useTranslation()
|
||||
const isFa = lang === 'fa'
|
||||
|
||||
const toPersianNum = (num: string | number | undefined | null) => {
|
||||
if (num === null || num === undefined) return num
|
||||
if (!isFa) return num
|
||||
return num.toString().replace(/\d/g, d => '۰۱۲۳۴۵۶۷۸۹'[d as any])
|
||||
}
|
||||
|
||||
const formatDate = (dateStr: string | undefined) => {
|
||||
if (!dateStr) return "-"
|
||||
try {
|
||||
const date = new Date(dateStr)
|
||||
return new Intl.DateTimeFormat(isFa ? 'fa-IR' : 'en-US', {
|
||||
dateStyle: 'long',
|
||||
timeZone: 'Asia/Tehran'
|
||||
}).format(date)
|
||||
} catch (e) {
|
||||
return dateStr
|
||||
}
|
||||
}
|
||||
|
||||
// Modals state
|
||||
const [isEditModalOpen, setIsEditModalOpen] = useState(false)
|
||||
const [isPicModalOpen, setIsPicModalOpen] = useState(false)
|
||||
const [isSaving, setIsSaving] = useState(false)
|
||||
|
||||
// Form states
|
||||
const [formData, setFormData] = useState<Partial<UserProfile>>({})
|
||||
const [selectedFile, setSelectedFile] = useState<File | null>(null)
|
||||
const [dragActive, setDragActive] = useState(false)
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const fetchProfile = async () => {
|
||||
try {
|
||||
const data = await getUserProfile()
|
||||
setUser(data)
|
||||
} catch (error) {
|
||||
navigate("/login")
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchProfile()
|
||||
}, [])
|
||||
|
||||
const handleEditClick = () => {
|
||||
if (user) {
|
||||
setFormData({
|
||||
first_name: user.first_name || "",
|
||||
last_name: user.last_name || "",
|
||||
email: user.email || "",
|
||||
description: user.description || "",
|
||||
birth_date: user.birth_date || "",
|
||||
})
|
||||
setIsEditModalOpen(true)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSaveProfile = async () => {
|
||||
setIsSaving(true)
|
||||
try {
|
||||
const payload: Record<string, any> = {}
|
||||
|
||||
Object.entries(formData).forEach(([key, value]) => {
|
||||
if (key === "birth_date" && value === "") {
|
||||
payload[key] = null
|
||||
} else if (value !== undefined && value !== null) {
|
||||
payload[key] = value
|
||||
}
|
||||
})
|
||||
|
||||
const updatedUser = await updateUserProfile(payload)
|
||||
setUser(prev => prev ? { ...prev, ...updatedUser } : updatedUser)
|
||||
setIsEditModalOpen(false)
|
||||
toast.success(t.profile.toasts.successEdit)
|
||||
} catch (error) {
|
||||
toast.error(t.profile.toasts.error)
|
||||
console.error("Failed to update profile", error)
|
||||
} finally {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handlePictureUpload = async () => {
|
||||
if (!selectedFile) return
|
||||
setIsSaving(true)
|
||||
try {
|
||||
const response = await updateProfilePicture(selectedFile)
|
||||
setUser(prev => prev ? { ...prev, profile_picture: response.profile_picture } : response)
|
||||
setIsPicModalOpen(false)
|
||||
setSelectedFile(null)
|
||||
toast.success(t.profile.toasts.successImage)
|
||||
} catch (error) {
|
||||
toast.error(t.profile.toasts.error)
|
||||
console.error("Failed to upload picture", error)
|
||||
} finally {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleDeletePicture = async () => {
|
||||
setIsSaving(true)
|
||||
try {
|
||||
const response = await removeProfilePicture()
|
||||
setUser(prev => prev ? { ...prev, profile_picture: response.profile_picture || null } : response)
|
||||
setIsPicModalOpen(false)
|
||||
toast.success(t.profile.toasts.successRemoveImage)
|
||||
} catch (error) {
|
||||
toast.error(t.profile.toasts.error)
|
||||
console.error("Failed to delete picture", error)
|
||||
} finally {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
// Drag & Drop Handlers
|
||||
const handleDrag = (e: React.DragEvent) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
if (e.type === "dragenter" || e.type === "dragover") {
|
||||
setDragActive(true)
|
||||
} else if (e.type === "dragleave") {
|
||||
setDragActive(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleDrop = (e: React.DragEvent) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setDragActive(false)
|
||||
if (e.dataTransfer.files && e.dataTransfer.files[0]) {
|
||||
setSelectedFile(e.dataTransfer.files[0])
|
||||
}
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<>
|
||||
<div className="flex min-h-[calc(100vh-73px)] items-center justify-center bg-slate-50 dark:bg-slate-950 transition-colors">
|
||||
<div className="text-slate-500 dark:text-slate-400">Loading...</div>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
if (!user) return null
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="min-h-[calc(100vh-73px)] bg-slate-50 dark:bg-slate-950 p-6 transition-colors">
|
||||
<div className="mx-auto max-w-3xl space-y-6">
|
||||
|
||||
{/* Header Card */}
|
||||
<div className="flex flex-col md:flex-row items-center gap-6 rounded-xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
|
||||
|
||||
<div className="relative group cursor-pointer" onClick={() => setIsPicModalOpen(true)}>
|
||||
<div className="h-24 w-24 overflow-hidden rounded-full bg-slate-200 dark:bg-slate-800 flex items-center justify-center border-4 border-white dark:border-slate-900 shadow-sm">
|
||||
{user.profile_picture ? (
|
||||
<img src={user.profile_picture} alt="Profile" className="h-full w-full object-cover" />
|
||||
) : (
|
||||
<UserIcon className="h-10 w-10 text-slate-400" />
|
||||
)}
|
||||
</div>
|
||||
<div className="absolute inset-0 flex items-center justify-center rounded-full bg-black/50 opacity-0 group-hover:opacity-100 transition-opacity">
|
||||
<Camera className="h-6 w-6 text-white" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 text-center md:text-start">
|
||||
<h1 className="text-2xl font-bold text-slate-900 dark:text-white">
|
||||
{user.full_name || "-"}
|
||||
</h1>
|
||||
<p className="text-slate-500 dark:text-slate-400 mt-1">{user.email || "-"}</p>
|
||||
</div>
|
||||
|
||||
<Button onClick={handleEditClick} className="flex items-center gap-2">
|
||||
<Edit2 className="h-4 w-4" />
|
||||
{t.profile?.editInfo || 'Edit Profile'}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Details Card */}
|
||||
<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'}
|
||||
</h2>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div className="space-y-1">
|
||||
<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>
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<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>
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<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">
|
||||
{toPersianNum(user.mobile)}
|
||||
</p>
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<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">
|
||||
{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 && (
|
||||
<div
|
||||
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm px-4"
|
||||
onClick={() => !isSaving && setIsEditModalOpen(false)}
|
||||
>
|
||||
<div
|
||||
className="w-full max-w-lg rounded-xl bg-white p-6 shadow-lg dark:bg-slate-900 border dark:border-slate-800"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<h2 className="mb-4 text-xl font-bold text-slate-900 dark:text-white">
|
||||
{t.profile?.editInfo || 'Edit Profile'}
|
||||
</h2>
|
||||
|
||||
<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
|
||||
type="email"
|
||||
value={formData.email || ""}
|
||||
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"
|
||||
dir="ltr"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<JalaliDatePicker
|
||||
label={t.profile?.birthDate || 'Birth Date'}
|
||||
value={formData.birth_date}
|
||||
onChange={(date) => setFormData({ ...formData, birth_date: date })}
|
||||
disabled={isSaving}
|
||||
/>
|
||||
|
||||
<div>
|
||||
<label className="text-sm font-medium dark:text-slate-300">{t.profile?.description || 'Description'}</label>
|
||||
<textarea
|
||||
rows={3}
|
||||
value={formData.description || ""}
|
||||
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"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 flex justify-end gap-3">
|
||||
<Button variant="outline" onClick={() => setIsEditModalOpen(false)} disabled={isSaving}>
|
||||
{t.profile?.cancel || t.cancel || 'Cancel'}
|
||||
</Button>
|
||||
<Button onClick={handleSaveProfile} disabled={isSaving}>
|
||||
{isSaving ? '...' : (t.profile?.save || 'Save')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Profile Picture Modal */}
|
||||
{isPicModalOpen && (
|
||||
<div
|
||||
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm px-4"
|
||||
onClick={() => !isSaving && setIsPicModalOpen(false)}
|
||||
>
|
||||
<div
|
||||
className="w-full max-w-sm rounded-xl bg-white p-6 shadow-lg dark:bg-slate-900 border dark:border-slate-800"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<h2 className="mb-4 text-xl font-bold text-slate-900 dark:text-white">
|
||||
{t.profile?.changePicture || 'Profile Picture'}
|
||||
</h2>
|
||||
|
||||
<div className="space-y-4">
|
||||
|
||||
{/* Drag and Drop Zone */}
|
||||
<div
|
||||
onDragEnter={handleDrag}
|
||||
onDragLeave={handleDrag}
|
||||
onDragOver={handleDrag}
|
||||
onDrop={handleDrop}
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
className={`relative flex flex-col items-center justify-center w-full h-48 border-2 border-dashed rounded-xl cursor-pointer transition-colors ${
|
||||
dragActive
|
||||
? "border-blue-500 bg-blue-50 dark:bg-blue-900/20"
|
||||
: "border-slate-300 dark:border-slate-700 bg-slate-50 dark:bg-slate-800 hover:bg-slate-100 dark:hover:bg-slate-800/80"
|
||||
}`}
|
||||
>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
className="hidden"
|
||||
onChange={(e) => setSelectedFile(e.target.files?.[0] || null)}
|
||||
/>
|
||||
|
||||
{selectedFile ? (
|
||||
<div className="absolute inset-0 p-2">
|
||||
<img
|
||||
src={URL.createObjectURL(selectedFile)}
|
||||
alt="Preview"
|
||||
className="w-full h-full object-contain rounded-lg"
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col items-center text-center p-4">
|
||||
<UploadCloud className="h-10 w-10 text-slate-400 mb-2" />
|
||||
<p className="text-sm font-medium text-slate-700 dark:text-slate-300">
|
||||
{ t.profile?.imageInput }
|
||||
</p>
|
||||
<p className="text-xs text-slate-500 mt-1">
|
||||
SVG, PNG, JPG (MAX. 800x400px)
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-2 mt-4">
|
||||
<Button onClick={handlePictureUpload} disabled={!selectedFile || isSaving} className="w-full">
|
||||
{t.profile?.upload || 'Upload'}
|
||||
</Button>
|
||||
|
||||
{user.profile_picture && (
|
||||
<Button variant="destructive" onClick={handleDeletePicture} disabled={isSaving} className="w-full flex items-center justify-center gap-2">
|
||||
<Trash2 className="h-4 w-4" />
|
||||
{t.profile?.remove || "Remove"}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 flex justify-end">
|
||||
<Button variant="outline" onClick={() => setIsPicModalOpen(false)} disabled={isSaving}>
|
||||
{t.profile?.cancel || t.cancel || 'Cancel'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user