import React, { useState, useEffect } from 'react'; import api from '../utils/api'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from './ui/dialog'; import { Button } from './ui/button'; import { Checkbox } from './ui/checkbox'; import { toast } from 'sonner'; export const AttendanceDialog = ({ event, open, onOpenChange, onSuccess }) => { const [rsvps, setRsvps] = useState([]); const [attendance, setAttendance] = useState({}); const [loading, setLoading] = useState(false); useEffect(() => { if (open && event) { fetchRSVPs(); } }, [open, event]); const fetchRSVPs = async () => { try { const response = await api.get(`/admin/events/${event.id}/rsvps`); setRsvps(response.data); // Pre-populate with existing attendance const attendanceMap = {}; response.data.forEach(rsvp => { attendanceMap[rsvp.user_id] = rsvp.attended || false; }); setAttendance(attendanceMap); } catch (error) { toast.error('Failed to fetch RSVPs'); } }; const handleSave = async () => { setLoading(true); try { // Mark attendance for each user for (const [userId, attended] of Object.entries(attendance)) { await api.put(`/admin/events/${event.id}/attendance`, { user_id: userId, attended: attended }); } toast.success('Attendance updated successfully'); onSuccess(); onOpenChange(false); } catch (error) { toast.error('Failed to update attendance'); } finally { setLoading(false); } }; return ( Mark Attendance: {event?.title}
{rsvps.length === 0 ? (

No RSVPs yet

) : ( rsvps.map((rsvp) => (
{ setAttendance({ ...attendance, [rsvp.user_id]: checked }); }} className="w-5 h-5" />

{rsvp.user_name}

{rsvp.user_email}

{rsvp.attended && ( ✓ Attended )}
)) )}
); };