'use client'; import React, { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { getProfile } from '../API/profile/getProfile'; import { getActivityHistory } from '../API/profile/updateProfile'; import { getTwoFactorAuthStatus } from '../API/profile/twoFactorAuth'; import Link from 'next/link'; import { Tabs, Tab } from 'react-bootstrap'; import './profile.scss'; interface ProfileData { name: string; lastname: string; email: string; profile_picture?: string; phone_number?: string; role: string; created_at: string; address?: string; city?: string; marked_for_deletion?: boolean; deletion_date?: string; two_factor_enabled?: boolean; verified?: boolean; last_login_ip?: string; last_login_device?: string; } interface ActivityData { id: number; activity_type: string; details: any; createdAt: string; ip_address?: string; user_agent?: string; } const ProfilePage = () => { const router = useRouter(); const [profile, setProfile] = useState(null); const [activities, setActivities] = useState([]); const [loading, setLoading] = useState(true); const [activityLoading, setActivityLoading] = useState(false); const [twoFactorEnabled, setTwoFactorEnabled] = useState(false); const [activeTab, setActiveTab] = useState('overview'); const [error, setError] = useState(null); useEffect(() => { const fetchProfile = async () => { try { const response = await getProfile(); if (response.success && response.data) { setProfile(response.data); } else { setError(response.message || 'Failed to load profile'); } } catch (err) { setError('Error loading profile'); console.error('Profile fetch error:', err); } finally { setLoading(false); } }; const fetch2FAStatus = async () => { try { const response = await getTwoFactorAuthStatus(); if (response.status === 'success' && response.data) { setTwoFactorEnabled(response.data.enabled); } } catch (err) { console.error('2FA status error:', err); // Don't set an error here as it's not critical } }; fetchProfile(); fetch2FAStatus(); }, []); const loadActivityHistory = async () => { if (activeTab === 'activity' && activities.length === 0) { setActivityLoading(true); try { const response = await getActivityHistory(); if (response.status === 'success' && response.data) { setActivities(response.data.activities); } } catch (error) { console.error('Error fetching activities:', error); } finally { setActivityLoading(false); } } }; useEffect(() => { loadActivityHistory(); }, [activeTab]); const calculateProfileCompletion = () => { if (!profile) return 0; const fields = [ !!profile.name, !!profile.lastname, !!profile.email, !!profile.profile_picture, !!profile.phone_number, !!profile.address, !!profile.city ]; const completedFields = fields.filter(Boolean).length; return Math.round((completedFields / fields.length) * 100); }; if (loading) { return (
Loading...
); } if (error) { return (
{error}
); } return (
{profile?.marked_for_deletion && (
Your account is scheduled for deletion on {new Date(profile.deletion_date || '').toLocaleDateString()}. Cancel Deletion
)}
{profile?.profile_picture ? ( Profile { console.error('Image load error'); e.currentTarget.style.display = 'none'; const placeholder = document.createElement('div'); placeholder.className = 'avatar-placeholder'; placeholder.innerText = profile?.name?.[0]?.toUpperCase() || 'U'; e.currentTarget.parentNode?.appendChild(placeholder); }} /> ) : (
{profile?.name?.[0]?.toUpperCase() || 'U'}
)}

{`${profile?.name || ''} ${profile?.lastname || ''}`}

{profile?.email}

{twoFactorEnabled && ( Secured )} {profile?.verified && ( Verified )}
Profile Completion {calculateProfileCompletion()}%
Edit Profile Change Password Account Settings
Quick Links
  • Dashboard
  • Orders
  • Wishlist
  • Activity Log
  • Security Settings
setActiveTab(k || 'overview')} id="profile-tabs" className="mb-0 border-bottom-0" > Overview} /> Activity} /> Security} />
{activeTab === 'overview' && (

{`${profile?.name || ''} ${profile?.lastname || ''}`}

{profile?.email}

{profile?.phone_number || 'Not provided'}

{new Date(profile?.created_at || '').toLocaleDateString()}

{profile?.address ? `${profile.address}, ${profile.city || ''}` : 'Not provided'}

Account Information

{profile?.marked_for_deletion ? ( Pending Deletion ) : ( Active )}

{profile?.last_login_ip || 'Not available'}

{profile?.verified ? ( Verified ) : ( Not Verified )}

{profile?.last_login_device || 'Not available'}

)} {activeTab === 'activity' && (
{activityLoading ? (
Loading...
) : activities.length > 0 ? (
{activities.map(activity => (
{activity.activity_type} {activity.details?.message || 'Activity recorded'}
{new Date(activity.createdAt).toLocaleString()}
{activity.ip_address && (
IP: {activity.ip_address}
)}
))}
) : (

No activity recorded yet

)}
)} {activeTab === 'security' && (
Security Center

Access all security features including two-factor authentication and more

Security Center

Two-Factor Authentication

{twoFactorEnabled ? ( <> Enabled Your account is protected with an additional security layer ) : ( <> Disabled Add an extra layer of security to your account )}

{twoFactorEnabled ? 'Manage 2FA' : 'Setup 2FA'}

Password Management

It's a good idea to use a strong password that you're not using elsewhere

Change Password

Activity History

View a detailed log of your account activity

View Activity

Delete Account

Once you delete your account, there is no going back. Please be certain.

Delete Account
)}
); }; export default ProfilePage;