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 { Calendar, User, CheckCircle, Clock, AlertCircle, Mail } from 'lucide-react'; import { toast } from 'sonner'; const Dashboard = () => { const { user, resendVerificationEmail } = useAuth(); const [events, setEvents] = useState([]); const [loading, setLoading] = useState(true); const [resendLoading, setResendLoading] = useState(false); useEffect(() => { fetchUpcomingEvents(); }, []); 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 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'; toast.error(errorMessage); } finally { setResendLoading(false); } }; const getStatusBadge = (status) => { const statusConfig = { pending_email: { icon: Clock, label: 'Pending Email', className: 'bg-[#F2CC8F] text-[#3D405B]' }, pending_approval: { icon: Clock, label: 'Pending Approval', className: 'bg-[#A3B1C6] text-white' }, pre_approved: { icon: CheckCircle, label: 'Pre-Approved', className: 'bg-[#81B29A] text-white' }, payment_pending: { icon: AlertCircle, label: 'Payment Pending', className: 'bg-[#E07A5F] text-white' }, active: { icon: CheckCircle, label: 'Active', className: 'bg-[#81B29A] text-white' }, inactive: { icon: AlertCircle, label: 'Inactive', className: 'bg-[#6B708D] text-white' } }; 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_approval: 'Your application is under review by our admin team.', pre_approved: '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.' }; 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'}

{/* 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_approval' && (

Application Under Review

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

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

Complete Your Payment

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

)}
); }; export default Dashboard;