'use client' import React, { useState, useEffect } from 'react' import { useRouter } from 'next/navigation' import { useLocale } from '../contexts/LocaleContext' interface UserProfile { id: number username: string email: string full_name: string bio: string avatar: string | null cover: string | null } export default function ProfilePage() { const { t } = useLocale() const router = useRouter() const [profile, setProfile] = useState(null) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [avatarFile, setAvatarFile] = useState(null) const [coverFile, setCoverFile] = useState(null) useEffect(() => { loadProfile() }, []) const loadProfile = async () => { try { const token = localStorage.getItem('token') if (!token) { router.push('/auth/login') return } const response = await fetch('/api/auth/user', { headers: { 'Authorization': `Bearer ${token}` } }) if (!response.ok) { throw new Error('Failed to load profile') } const data = await response.json() setProfile(data) } catch (error) { console.error('Error loading profile:', error) } finally { setLoading(false) } } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!profile) return setSaving(true) try { const token = localStorage.getItem('token') const formData = new FormData() formData.append('username', profile.username) formData.append('email', profile.email) formData.append('full_name', profile.full_name) formData.append('bio', profile.bio) if (avatarFile) { formData.append('avatar', avatarFile) } if (coverFile) { formData.append('cover', coverFile) } const response = await fetch('/api/auth/user', { method: 'PUT', headers: { 'Authorization': `Bearer ${token}` }, body: formData }) if (response.ok) { const updatedProfile = await response.json() setProfile(updatedProfile) // Очистить выбранные файлы setAvatarFile(null) setCoverFile(null) } } catch (error) { console.error('Error saving profile:', error) } finally { setSaving(false) } } const removeAvatar = () => { if (profile) { setProfile({ ...profile, avatar: null }) } } const removeCover = () => { if (profile) { setProfile({ ...profile, cover: null }) } } if (loading) { return (
{t('common.loading')}
) } if (!profile) { return (
{t('common.error')}: Profile not found
) } return (

{t('profile.edit')}

{/* Avatar */}
Avatar
{(profile.avatar || avatarFile) && ( )}
{/* Cover Image */}
{(profile.cover || coverFile) && (
Cover
)}
{(profile.cover || coverFile) && ( )}
{/* Basic Info */}
setProfile({ ...profile, username: e.target.value })} required />
setProfile({ ...profile, email: e.target.value })} required />
setProfile({ ...profile, full_name: e.target.value })} />