import React, { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; import api from '../utils/api'; import { Card } from '../components/ui/card'; import { Button } from '../components/ui/button'; import { Badge } from '../components/ui/badge'; import Navbar from '../components/Navbar'; import MemberFooter from '../components/MemberFooter'; import { Calendar, User, CheckCircle, Clock, AlertCircle, Mail, Users, Image, FileText, DollarSign, Scale } from 'lucide-react'; import { toast } from 'sonner'; const Dashboard = () => { const { user, resendVerificationEmail, refreshUser } = useAuth(); const [events, setEvents] = useState([]); const [loading, setLoading] = useState(true); const [resendLoading, setResendLoading] = useState(false); const [eventActivity, setEventActivity] = useState(null); const [activityLoading, setActivityLoading] = useState(true); useEffect(() => { fetchUpcomingEvents(); fetchEventActivity(); }, []); const fetchUpcomingEvents = async () => { try { const response = await api.get('/events'); const upcomingEvents = response.data.slice(0, 3); setEvents(upcomingEvents); } catch (error) { console.error('Failed to fetch events:', error); } finally { setLoading(false); } }; const fetchEventActivity = async () => { try { const response = await api.get('/members/event-activity'); setEventActivity(response.data); } catch (error) { console.error('Failed to fetch event activity:', error); } finally { setActivityLoading(false); } }; const handleResendVerification = async () => { setResendLoading(true); try { await resendVerificationEmail(); toast.success('Verification email sent! Please check your inbox.'); } catch (error) { const errorMessage = error.response?.data?.detail || 'Failed to send verification email'; // If email is already verified, refresh user data to update UI if (errorMessage === 'Email is already verified') { try { await refreshUser(); toast.success('Your email is already verified!'); } catch (refreshError) { toast.error('Email is already verified. Please refresh the page.'); } } else { toast.error(errorMessage); } } finally { setResendLoading(false); } }; const getStatusBadge = (status) => { const statusConfig = { pending_email: { icon: Clock, label: 'Pending Email', className: 'bg-orange-100 text-orange-700' }, pending_validation: { icon: Clock, label: 'Pending Validation', className: 'bg-gray-200 text-gray-700' }, pre_validated: { icon: CheckCircle, label: 'Pre-Validated', className: 'bg-var(--green-light) text-white' }, payment_pending: { icon: AlertCircle, label: 'Payment Pending', className: 'bg-orange-500 text-white' }, active: { icon: CheckCircle, label: 'Active', className: 'bg-var(--green-light) text-white' }, inactive: { icon: AlertCircle, label: 'Inactive', className: 'bg-gray-400 text-white' }, canceled: { icon: AlertCircle, label: 'Canceled', className: 'bg-red-100 text-red-700' }, expired: { icon: Clock, label: 'Expired', className: 'bg-red-500 text-white' }, abandoned: { icon: AlertCircle, label: 'Abandoned', className: 'bg-gray-300 text-gray-600' } }; const config = statusConfig[status] || statusConfig.inactive; const Icon = config.icon; return ( {config.label} ); }; const getStatusMessage = (status) => { const messages = { pending_email: 'Please check your email to verify your account.', pending_validation: 'Your application is under review by our admin team.', pre_validated: 'Your application is under review by our admin team.', payment_pending: 'Please complete your payment to activate your membership.', active: 'Your membership is active! Enjoy all member benefits.', inactive: 'Your membership is currently inactive.', canceled: 'Your membership has been canceled. Contact us to rejoin.', expired: 'Your membership has expired. Please renew to regain access.', abandoned: 'Your application was not completed. Contact us to restart the process.' }; return messages[status] || ''; }; return (
{/* Welcome Section */}

Welcome Back, {user?.first_name}!

Here's an overview of your membership status and upcoming events.

{/* Email Verification Alert */} {user && !user.email_verified && (

Verify Your Email Address

Please verify your email address to complete your registration. Check your inbox for the verification link.

)} {/* Status Card */}

Membership Status

{getStatusBadge(user?.status)}

{getStatusMessage(user?.status)}

{/* Grid Layout */}
{/* Quick Stats */}

Quick Info

Email

{user?.email}

Role

{user?.role}

Member Since

{user?.created_at ? new Date(user.created_at).toLocaleDateString() : 'N/A'}

{user?.subscription_start_date && user?.subscription_end_date && ( <>

Membership Period

{new Date(user.subscription_start_date).toLocaleDateString()} - {new Date(user.subscription_end_date).toLocaleDateString()}

Days Remaining

{Math.max(0, Math.ceil((new Date(user.subscription_end_date) - new Date()) / (1000 * 60 * 60 * 24)))} days

)}
{/* Upcoming Events */}

Upcoming Events

{loading ? (

Loading events...

) : events.length > 0 ? (
{events.map((event) => (

{event.title}

{new Date(event.start_at).toLocaleDateString()} at{' '} {new Date(event.start_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}

{event.location}

))}
) : (

No upcoming events at the moment.

Check back later for new events!

)}
{/* CTA Section */} {user?.status === 'pending_validation' && (

Application Under Review

Your membership application is being reviewed by our admin team. You'll be notified once validated!

)} {/* Payment Prompt for payment_pending status */} {user?.status === 'payment_pending' && (

Complete Your Payment

Great news! Your membership application has been validated. Complete your payment to activate your membership and gain full access to all member benefits.

)} {/* Event Activity Section */}

My Event Activity

{activityLoading ? (

Loading event activity...

) : eventActivity ? (
{/* Stats Cards */}

Total RSVPs

{eventActivity.total_rsvps}

Events Attended

{eventActivity.total_attended}

{/* Upcoming RSVP'd Events */} {eventActivity.upcoming_events && eventActivity.upcoming_events.length > 0 && (

Upcoming Events (RSVP'd)

{eventActivity.upcoming_events.map((event) => (

{event.title}

{new Date(event.start_at).toLocaleDateString()} at{' '} {new Date(event.start_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}

{event.location}

{event.rsvp_status === 'yes' ? 'Going' : event.rsvp_status === 'maybe' ? 'Maybe' : 'Not Going'}
))}
)} {/* Past Events & Attendance */} {eventActivity.past_events && eventActivity.past_events.length > 0 && (

Past Events

{eventActivity.past_events.slice(0, 5).map((event) => (

{event.title}

{new Date(event.start_at).toLocaleDateString()} at{' '} {new Date(event.start_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}

{event.attended ? 'Attended' : 'Did not attend'} {event.attended && event.attended_at && (

Checked in: {new Date(event.attended_at).toLocaleDateString()}

)}
))}
{eventActivity.past_events.length > 5 && (

Showing 5 of {eventActivity.past_events.length} past events

)}
)} {/* No Events Message */} {(!eventActivity.upcoming_events || eventActivity.upcoming_events.length === 0) && (!eventActivity.past_events || eventActivity.past_events.length === 0) && (

No Event Activity Yet

Browse upcoming events and RSVP to start building your event history!

)}
) : (

Failed to load event activity. Please try refreshing the page.

)}
); }; export default Dashboard;