'use client'; import { useSession } from '@/lib/auth-client'; import { useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; import { Button } from '@/components/ui/Button'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/Card'; import { Input } from '@/components/ui/Input'; interface ProfileData { id: string; name: string; email: string; image: string | null; profile: { bio: string | null; phone: string | null; city: string | null; skillLevel: string; favoriteSports: string[]; } | null; } const SPORT_OPTIONS = [ { value: 'FOOTBALL', label: 'Futbal' }, { value: 'BASKETBALL', label: 'Basketbal' }, { value: 'TENNIS', label: 'Tenis' }, { value: 'VOLLEYBALL', label: 'Volejbal' }, { value: 'BADMINTON', label: 'Bedminton' }, { value: 'TABLE_TENNIS', label: 'Stolný tenis' }, { value: 'RUNNING', label: 'Beh' }, { value: 'CYCLING', label: 'Cyklistika' }, { value: 'SWIMMING', label: 'Plávanie' }, { value: 'GYM', label: 'Fitnes' }, { value: 'OTHER', label: 'Iné' }, ]; const SKILL_OPTIONS = [ { value: 'BEGINNER', label: 'Začiatočník' }, { value: 'INTERMEDIATE', label: 'Stredne pokročilý' }, { value: 'ADVANCED', label: 'Pokročilý' }, { value: 'EXPERT', label: 'Expert' }, ]; export default function ProfileEditPage() { const { data: session, isPending } = useSession(); const router = useRouter(); const [profile, setProfile] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [formData, setFormData] = useState({ name: '', bio: '', phone: '', city: '', skillLevel: 'BEGINNER', favoriteSports: [] as string[], image: '', }); const [imagePreview, setImagePreview] = useState(null); useEffect(() => { if (!isPending && !session) { router.push('/auth/signin'); } }, [session, isPending, router]); useEffect(() => { if (session) { fetchProfile(); } }, [session]); const fetchProfile = async () => { try { setLoading(true); const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/profile`, { credentials: 'include', }); if (!response.ok) { throw new Error('Failed to fetch profile'); } const data = await response.json(); setProfile(data); setFormData({ name: data.name || '', bio: data.profile?.bio || '', phone: data.profile?.phone || '', city: data.profile?.city || '', skillLevel: data.profile?.skillLevel || 'BEGINNER', favoriteSports: data.profile?.favoriteSports || [], image: data.image || '', }); setImagePreview(data.image); } catch (err) { console.error('Error fetching profile:', err); setError('Nepodarilo sa načítať profil'); } finally { setLoading(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setSuccess(false); setSaving(true); try { const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/profile`, { method: 'PUT', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify(formData), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || 'Failed to update profile'); } setSuccess(true); setTimeout(() => { router.push('/profile'); }, 1500); } catch (err: any) { console.error('Error updating profile:', err); setError(err.message || 'Nepodarilo sa aktualizovať profil'); } finally { setSaving(false); } }; const handleSportToggle = (sport: string) => { setFormData((prev) => ({ ...prev, favoriteSports: prev.favoriteSports.includes(sport) ? prev.favoriteSports.filter((s) => s !== sport) : [...prev.favoriteSports, sport], })); }; const handleImageUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; if (file.size > 2 * 1024 * 1024) { setError('Obrázok je príliš veľký. Maximum je 2MB.'); return; } if (!file.type.startsWith('image/')) { setError('Môžete nahrať iba obrázky.'); return; } const reader = new FileReader(); reader.onloadend = () => { const base64String = reader.result as string; setFormData((prev) => ({ ...prev, image: base64String })); setImagePreview(base64String); }; reader.readAsDataURL(file); }; const handleRemoveImage = () => { setFormData((prev) => ({ ...prev, image: '' })); setImagePreview(null); }; if (isPending || loading) { return (

Načítavam profil...

); } if (!session || !profile) { return null; } return (

Upraviť profil

Aktualizujte svoje informácie a športové preferencie

{imagePreview ? ( Náhľad ) : (
{formData.name.charAt(0).toUpperCase()}
)}
{imagePreview && ( )}

JPG, PNG alebo GIF. Maximum 2MB.

setFormData({ ...formData, name: e.target.value })} required placeholder="Vaše meno" />