'use client'; import React, { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { getProfile } from '../../API/profile/getProfile'; import { updateProfile, updateProfilePicture, deleteProfilePicture } from '../../API/profile/updateProfile'; import { initiateEmailChange } from '../../API/profile/updateProfile'; import './edit.scss'; interface ProfileData { name: string; lastname: string; email: string; phone_number?: string; address?: string; city?: string; profile_picture?: string; } const EditProfilePage = () => { const router = useRouter(); const [profile, setProfile] = useState({ name: '', lastname: '', email: '', phone_number: '', address: '', city: '', }); const [initialProfile, setInitialProfile] = useState({ name: '', lastname: '', email: '', phone_number: '', address: '', city: '', }); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [selectedFile, setSelectedFile] = useState(null); useEffect(() => { loadProfile(); }, []); const loadProfile = async () => { try { const response = await getProfile(); if (response.data) { setProfile(response.data); setInitialProfile(response.data); } } catch (err) { setError('Failed to load profile'); } finally { setLoading(false); } }; const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setProfile(prev => ({ ...prev, [name]: value })); }; const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { if (!file.type.startsWith('image/')) { setError('Please upload an image file'); return; } if (file.size > 5 * 1024 * 1024) { setError('File size must be less than 5MB'); return; } setSelectedFile(file); const reader = new FileReader(); reader.onloadend = () => { setProfile(prev => ({ ...prev, profile_picture: reader.result as string })); }; reader.readAsDataURL(file); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); setError(null); try { if (profile.email !== initialProfile.email) { await initiateEmailChange(profile.email); router.push('/profile/email/pending'); return; } await updateProfile({ name: profile.name, lastname: profile.lastname, phone_number: profile.phone_number, address: profile.address, city: profile.city }); if (selectedFile) { await updateProfilePicture(selectedFile); } setSuccess(true); setTimeout(() => { router.push('/profile'); }, 2000); } catch (err) { setError('Failed to update profile'); console.error('Update error:', err); } finally { setSaving(false); } }; const handleDeletePicture = async () => { try { await deleteProfilePicture(); setProfile(prev => ({ ...prev, profile_picture: undefined })); } catch (err) { setError('Failed to delete profile picture'); } }; if (loading) { return (
Loading...
); } return (

Edit Profile

Update your personal information

{error && (
{error}
)} {success && (
Profile updated successfully! Redirecting...
)}
Profile Picture
{profile.profile_picture ? (
Profile { console.error('Image load error'); const target = e.target as HTMLImageElement; target.style.display = 'none'; const placeholder = document.createElement('div'); placeholder.className = 'avatar-placeholder'; placeholder.innerText = profile?.name?.[0]?.toUpperCase() || 'U'; target.parentElement?.appendChild(placeholder); }} />
) : (
{profile.name?.[0]?.toUpperCase() || 'U'}
)}
{profile.profile_picture && ( )}
Max size: 5MB. Supported formats: JPG, PNG, GIF

{profile.email !== initialProfile.email && ( Changing your email will require verification )}
); }; export default EditProfilePage;