'use client'; import { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { User, Edit, Mail, Calendar, Save, X } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; interface User { id: string; email: string; name: string; surname: string; role: string; createdAt: string; } interface ProfileFormData { name: string; surname: string; } export function UserProfile() { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); const [isEditing, setIsEditing] = useState(false); const [saving, setSaving] = useState(false); const [formData, setFormData] = useState({ name: '', surname: '', }); const { toast } = useToast(); const updateFormData = (field: keyof ProfileFormData, value: string) => { setFormData((prev) => ({ ...prev, [field]: value, })); }; useEffect(() => { fetchUserProfile(); }, []); const fetchUserProfile = async () => { try { const response = await fetch('/api/users/profile'); if (response.ok) { const userData = await response.json(); setUser(userData.user); setFormData({ name: userData.user.name, surname: userData.user.surname, }); } else { toast({ title: 'Error', description: 'Failed to fetch user profile', variant: 'destructive', }); } } catch (error) { console.error('Error fetching user profile:', error); toast({ title: 'Error', description: 'Failed to fetch user profile', variant: 'destructive', }); } finally { setLoading(false); } }; const handleSave = async () => { if (!formData.name.trim() || !formData.surname.trim()) { toast({ title: 'Error', description: 'Name and surname are required', variant: 'destructive', }); return; } setSaving(true); try { const response = await fetch('/api/users/profile', { method: 'PATCH', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: formData.name.trim(), surname: formData.surname.trim(), }), }); const data = await response.json(); if (response.ok) { toast({ title: 'Success', description: 'Profile updated successfully!', }); setIsEditing(false); await fetchUserProfile(); // Refresh user data } else { toast({ title: 'Error', description: data.error || 'Failed to update profile', variant: 'destructive', }); } } catch (error) { console.error('Error updating profile:', error); toast({ title: 'Error', description: 'Failed to update profile', variant: 'destructive', }); } finally { setSaving(false); } }; const handleCancel = () => { if (user) { setFormData({ name: user.name, surname: user.surname, }); } setIsEditing(false); }; if (loading) { return (

Loading profile...

); } if (!user) { return (

Unable to load user profile

); } return (
User Profile {/* Profile Information */}
{/* Name */}
{isEditing ? ( updateFormData('name', e.target.value)} placeholder='Enter your first name' /> ) : (
{user.name}
)}
{/* Surname */}
{isEditing ? ( updateFormData('surname', e.target.value)} placeholder='Enter your last name' /> ) : (
{user.surname}
)}
{/* Email (Read-only) */}
{user.email} (Read-only)
{/* Member Since */}
{new Date(user.createdAt).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', })}
{/* Action Buttons */}
{isEditing ? ( <> ) : ( )}
{/* Account Information Card */} Account Information
{user.role}
{user.id}
); }