Files
uPortal2/frontend/src/pages/ProfilePage.jsx
T

141 lines
4.3 KiB
React
Raw Normal View History

2026-10-21 13:17:44 +03:00
import React, { useRef, useState } from 'react'
import { api } from '../api.js'
export default function ProfilePage({ user, onUserChange }) {
const [fullName, setFullName] = useState(user.fullName || '')
const [email, setEmail] = useState(user.email || '')
const [profileMsg, setProfileMsg] = useState(null)
const [passwords, setPasswords] = useState({ oldPassword: '', newPassword: '' })
const [passwordMsg, setPasswordMsg] = useState(null)
const [avatarMsg, setAvatarMsg] = useState(null)
const [avatarVersion, setAvatarVersion] = useState(Date.now())
const [avatarError, setAvatarError] = useState(false)
const fileRef = useRef(null)
const initials = (user.fullName || user.login || '?').trim().charAt(0).toUpperCase()
const saveProfile = async (event) => {
event.preventDefault()
setProfileMsg(null)
try {
const updated = await api.updateProfile({ fullName, email })
onUserChange(updated)
setProfileMsg({ type: 'ok', text: 'Profile updated' })
} catch (err) {
setProfileMsg({ type: 'error', text: err.message })
}
}
const savePassword = async (event) => {
event.preventDefault()
setPasswordMsg(null)
try {
await api.updatePassword(passwords)
setPasswords({ oldPassword: '', newPassword: '' })
setPasswordMsg({ type: 'ok', text: 'Password changed' })
} catch (err) {
setPasswordMsg({ type: 'error', text: err.message })
}
}
const uploadAvatar = async (event) => {
const file = event.target.files?.[0]
if (!file) return
setAvatarMsg(null)
try {
await api.uploadAvatar(file)
setAvatarVersion(Date.now())
setAvatarError(false)
setAvatarMsg({ type: 'ok', text: 'Avatar updated' })
} catch (err) {
setAvatarMsg({ type: 'error', text: err.message })
} finally {
if (fileRef.current) fileRef.current.value = ''
}
}
return (
<div className="grid">
<section className="card">
<h2>Avatar</h2>
<div className="avatar-row">
{avatarError ? (
<div className="avatar avatar-fallback" aria-label="No avatar">
{initials}
</div>
) : (
<img
className="avatar"
src={`${api.avatarUrl()}?v=${avatarVersion}`}
alt="avatar"
onError={() => setAvatarError(true)}
/>
)}
<div>
<input
ref={fileRef}
type="file"
accept="image/png,image/jpeg"
onChange={uploadAvatar}
/>
<p className="hint">PNG or JPEG.</p>
</div>
</div>
{avatarMsg && <p className={avatarMsg.type}>{avatarMsg.text}</p>}
</section>
<section className="card">
<h2>Profile</h2>
<form onSubmit={saveProfile}>
<label>
Login
<input value={user.login} disabled />
</label>
<label>
Full name
<input value={fullName} onChange={(e) => setFullName(e.target.value)} />
</label>
<label>
Email
<input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
</label>
<button className="btn-primary" type="submit">
Save profile
</button>
{profileMsg && <p className={profileMsg.type}>{profileMsg.text}</p>}
</form>
</section>
<section className="card">
<h2>Password</h2>
<form onSubmit={savePassword}>
<label>
Current password
<input
type="password"
value={passwords.oldPassword}
onChange={(e) => setPasswords((p) => ({ ...p, oldPassword: e.target.value }))}
required
/>
</label>
<label>
New password
<input
type="password"
value={passwords.newPassword}
onChange={(e) => setPasswords((p) => ({ ...p, newPassword: e.target.value }))}
minLength={8}
required
/>
</label>
<button className="btn-primary" type="submit">
Change password
</button>
{passwordMsg && <p className={passwordMsg.type}>{passwordMsg.text}</p>}
</form>
</section>
</div>
)
}