|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { useParams } from "react-router-dom"; |
| 3 | +import { |
| 4 | + Typography, |
| 5 | + Table, |
| 6 | + TableBody, |
| 7 | + TableRow, |
| 8 | + TableCell, |
| 9 | + TableHead, |
| 10 | + Card, |
| 11 | + Box, |
| 12 | + Stack, |
| 13 | + Chip, |
| 14 | + Button, |
| 15 | + IconButton, |
| 16 | + Tooltip, |
| 17 | + LinearProgress, |
| 18 | + Grid |
| 19 | +} from "@mui/material"; |
| 20 | +import { |
| 21 | + HowToReg as RegIcon, |
| 22 | + Cancel as CancelIcon, |
| 23 | + Delete as DeleteIcon, |
| 24 | + Download as DownloadIcon |
| 25 | +} from "@mui/icons-material"; |
| 26 | +import { ApiHelper, Loading, PageHeader } from "@churchapps/apphelper"; |
| 27 | +import { type EventInterface, type RegistrationInterface } from "@churchapps/helpers"; |
| 28 | +import { RegistrationSettingsEdit } from "./components/RegistrationSettingsEdit"; |
| 29 | + |
| 30 | +export const RegistrationDetailsPage = () => { |
| 31 | + const params = useParams(); |
| 32 | + const eventId = params.eventId; |
| 33 | + const [event, setEvent] = useState<EventInterface | null>(null); |
| 34 | + const [registrations, setRegistrations] = useState<RegistrationInterface[]>([]); |
| 35 | + const [loading, setLoading] = useState(true); |
| 36 | + const [count, setCount] = useState(0); |
| 37 | + |
| 38 | + const loadData = async () => { |
| 39 | + if (!eventId) return; |
| 40 | + setLoading(true); |
| 41 | + const [eventData, regsData] = await Promise.all([ |
| 42 | + ApiHelper.get("/events/" + eventId, "ContentApi"), |
| 43 | + ApiHelper.get("/registrations/event/" + eventId, "ContentApi") |
| 44 | + ]); |
| 45 | + setEvent(eventData); |
| 46 | + setRegistrations(regsData || []); |
| 47 | + setCount((regsData || []).filter((r: RegistrationInterface) => r.status !== "cancelled").length); |
| 48 | + setLoading(false); |
| 49 | + }; |
| 50 | + |
| 51 | + useEffect(() => { loadData(); }, [eventId]); |
| 52 | + |
| 53 | + const handleCancel = async (regId: string) => { |
| 54 | + if (!confirm("Cancel this registration?")) return; |
| 55 | + await ApiHelper.post("/registrations/" + regId + "/cancel", {}, "ContentApi"); |
| 56 | + loadData(); |
| 57 | + }; |
| 58 | + |
| 59 | + const handleDelete = async (regId: string) => { |
| 60 | + if (!confirm("Permanently delete this registration?")) return; |
| 61 | + await ApiHelper.delete("/registrations/" + regId, "ContentApi"); |
| 62 | + loadData(); |
| 63 | + }; |
| 64 | + |
| 65 | + const handleExportCSV = () => { |
| 66 | + const rows = [["Name", "Members", "Status", "Date"]]; |
| 67 | + registrations.forEach((reg) => { |
| 68 | + const members = reg.members?.map((m) => `${m.firstName} ${m.lastName}`).join("; ") || ""; |
| 69 | + rows.push([ |
| 70 | + reg.personId || "Guest", |
| 71 | + members, |
| 72 | + reg.status || "", |
| 73 | + reg.registeredDate ? new Date(reg.registeredDate).toLocaleDateString() : "" |
| 74 | + ]); |
| 75 | + }); |
| 76 | + const csv = rows.map((r) => r.map((c) => `"${c}"`).join(",")).join("\n"); |
| 77 | + const blob = new Blob([csv], { type: "text/csv" }); |
| 78 | + const url = URL.createObjectURL(blob); |
| 79 | + const a = document.createElement("a"); |
| 80 | + a.href = url; |
| 81 | + a.download = `registrations-${event?.title || eventId}.csv`; |
| 82 | + a.click(); |
| 83 | + URL.revokeObjectURL(url); |
| 84 | + }; |
| 85 | + |
| 86 | + const getStatusChip = (status: string) => { |
| 87 | + const colorMap: Record<string, "success" | "warning" | "error" | "default"> = { |
| 88 | + confirmed: "success", |
| 89 | + pending: "warning", |
| 90 | + cancelled: "error", |
| 91 | + waitlisted: "default" |
| 92 | + }; |
| 93 | + return <Chip label={status} size="small" color={colorMap[status] || "default"} />; |
| 94 | + }; |
| 95 | + |
| 96 | + const getRows = () => registrations.map((reg) => ( |
| 97 | + <TableRow key={reg.id}> |
| 98 | + <TableCell> |
| 99 | + {reg.members && reg.members.length > 0 |
| 100 | + ? reg.members.map((m) => `${m.firstName} ${m.lastName}`).join(", ") |
| 101 | + : reg.personId || "Unknown" |
| 102 | + } |
| 103 | + </TableCell> |
| 104 | + <TableCell>{reg.members?.length || 0}</TableCell> |
| 105 | + <TableCell>{getStatusChip(reg.status)}</TableCell> |
| 106 | + <TableCell>{reg.registeredDate ? new Date(reg.registeredDate).toLocaleDateString() : ""}</TableCell> |
| 107 | + <TableCell align="right"> |
| 108 | + {reg.status !== "cancelled" && ( |
| 109 | + <Tooltip title="Cancel Registration" arrow> |
| 110 | + <IconButton size="small" onClick={() => handleCancel(reg.id)} color="warning"><CancelIcon fontSize="small" /></IconButton> |
| 111 | + </Tooltip> |
| 112 | + )} |
| 113 | + <Tooltip title="Delete" arrow> |
| 114 | + <IconButton size="small" onClick={() => handleDelete(reg.id)} color="error"><DeleteIcon fontSize="small" /></IconButton> |
| 115 | + </Tooltip> |
| 116 | + </TableCell> |
| 117 | + </TableRow> |
| 118 | + )); |
| 119 | + |
| 120 | + if (loading) return <Box sx={{ p: 3, textAlign: "center" }}><Loading /></Box>; |
| 121 | + if (!event) return <Typography>Event not found</Typography>; |
| 122 | + |
| 123 | + const capacityPct = event.capacity ? Math.min((count / event.capacity) * 100, 100) : 0; |
| 124 | + |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <PageHeader icon={<RegIcon />} title={event.title || "Event Registrations"} subtitle="Manage registrations for this event" /> |
| 128 | + <Box sx={{ p: 3 }}> |
| 129 | + <Grid container spacing={3}> |
| 130 | + <Grid size={{ xs: 12, md: 8 }}> |
| 131 | + <Card sx={{ borderRadius: 2, border: "1px solid", borderColor: "grey.200" }}> |
| 132 | + <Box sx={{ p: 2, borderBottom: 1, borderColor: "divider" }}> |
| 133 | + <Stack direction="row" spacing={1} alignItems="center" justifyContent="space-between"> |
| 134 | + <Stack direction="row" spacing={1} alignItems="center"> |
| 135 | + <RegIcon sx={{ color: "primary.main" }} /> |
| 136 | + <Typography variant="h6" sx={{ fontWeight: 600, color: "primary.main" }}> |
| 137 | + Registrations ({count}{event.capacity ? ` / ${event.capacity}` : ""}) |
| 138 | + </Typography> |
| 139 | + </Stack> |
| 140 | + <Button startIcon={<DownloadIcon />} size="small" onClick={handleExportCSV}>Export CSV</Button> |
| 141 | + </Stack> |
| 142 | + {event.capacity && ( |
| 143 | + <LinearProgress variant="determinate" value={capacityPct} color={capacityPct >= 100 ? "error" : "primary"} sx={{ mt: 1 }} /> |
| 144 | + )} |
| 145 | + </Box> |
| 146 | + {registrations.length === 0 ? ( |
| 147 | + <Box sx={{ p: 3, textAlign: "center" }}> |
| 148 | + <Typography variant="body2" color="text.secondary">No registrations yet.</Typography> |
| 149 | + </Box> |
| 150 | + ) : ( |
| 151 | + <Table size="small"> |
| 152 | + <TableHead sx={{ backgroundColor: "#f5f5f5" }}> |
| 153 | + <TableRow> |
| 154 | + <TableCell sx={{ fontWeight: 600 }}>Name</TableCell> |
| 155 | + <TableCell sx={{ fontWeight: 600 }}>Members</TableCell> |
| 156 | + <TableCell sx={{ fontWeight: 600 }}>Status</TableCell> |
| 157 | + <TableCell sx={{ fontWeight: 600 }}>Date</TableCell> |
| 158 | + <TableCell sx={{ fontWeight: 600 }} align="right">Actions</TableCell> |
| 159 | + </TableRow> |
| 160 | + </TableHead> |
| 161 | + <TableBody>{getRows()}</TableBody> |
| 162 | + </Table> |
| 163 | + )} |
| 164 | + </Card> |
| 165 | + </Grid> |
| 166 | + |
| 167 | + <Grid size={{ xs: 12, md: 4 }}> |
| 168 | + <RegistrationSettingsEdit event={event} onUpdate={loadData} /> |
| 169 | + </Grid> |
| 170 | + </Grid> |
| 171 | + </Box> |
| 172 | + </> |
| 173 | + ); |
| 174 | +}; |
| 175 | + |
| 176 | +export default RegistrationDetailsPage; |
0 commit comments