|
| 1 | +import React from "react"; |
| 2 | +import { Dialog, DialogTitle, DialogContent, DialogActions, Button, FormControl, InputLabel, MenuItem, Select, Typography, CircularProgress, Alert } from "@mui/material"; |
| 3 | +import type { SelectChangeEvent } from "@mui/material"; |
| 4 | +import { ApiHelper } from "@churchapps/apphelper"; |
| 5 | + |
| 6 | +interface EmailTemplateOption { |
| 7 | + id: string; |
| 8 | + name: string; |
| 9 | + subject: string; |
| 10 | + category: string; |
| 11 | +} |
| 12 | + |
| 13 | +interface PreviewData { |
| 14 | + totalMembers: number; |
| 15 | + eligibleCount: number; |
| 16 | + noEmailCount: number; |
| 17 | +} |
| 18 | + |
| 19 | +interface SendResult { |
| 20 | + totalMembers: number; |
| 21 | + recipientCount: number; |
| 22 | + successCount: number; |
| 23 | + failCount: number; |
| 24 | + noEmailCount: number; |
| 25 | +} |
| 26 | + |
| 27 | +interface Props { |
| 28 | + groupId: string; |
| 29 | + groupName: string; |
| 30 | + onClose: () => void; |
| 31 | +} |
| 32 | + |
| 33 | +export const SendEmailDialog: React.FC<Props> = (props) => { |
| 34 | + const [templates, setTemplates] = React.useState<EmailTemplateOption[]>([]); |
| 35 | + const [selectedTemplateId, setSelectedTemplateId] = React.useState(""); |
| 36 | + const [sending, setSending] = React.useState(false); |
| 37 | + const [result, setResult] = React.useState<SendResult | null>(null); |
| 38 | + const [error, setError] = React.useState(""); |
| 39 | + const [preview, setPreview] = React.useState<PreviewData | null>(null); |
| 40 | + const [loadingPreview, setLoadingPreview] = React.useState(false); |
| 41 | + const [loadingTemplates, setLoadingTemplates] = React.useState(true); |
| 42 | + |
| 43 | + // Load templates on mount |
| 44 | + React.useEffect(() => { |
| 45 | + setLoadingTemplates(true); |
| 46 | + ApiHelper.get("/messaging/emailTemplates", "MessagingApi") |
| 47 | + .then((data) => setTemplates(data || [])) |
| 48 | + .catch(() => { /* templates load failure is handled by empty list */ }) |
| 49 | + .finally(() => setLoadingTemplates(false)); |
| 50 | + }, []); |
| 51 | + |
| 52 | + // Load preview data for group |
| 53 | + React.useEffect(() => { |
| 54 | + if (!props.groupId) return; |
| 55 | + setLoadingPreview(true); |
| 56 | + ApiHelper.get("/messaging/emailTemplates/preview/" + props.groupId, "MessagingApi") |
| 57 | + .then((data) => setPreview(data)) |
| 58 | + .catch(() => { /* preview is optional */ }) |
| 59 | + .finally(() => setLoadingPreview(false)); |
| 60 | + }, [props.groupId]); |
| 61 | + |
| 62 | + const handleSend = async () => { |
| 63 | + if (!selectedTemplateId) return; |
| 64 | + setSending(true); |
| 65 | + setError(""); |
| 66 | + try { |
| 67 | + const resp = await ApiHelper.post("/messaging/emailTemplates/send", { templateId: selectedTemplateId, groupId: props.groupId }, "MessagingApi"); |
| 68 | + if (resp.error) { |
| 69 | + setError(resp.error); |
| 70 | + } else { |
| 71 | + setResult(resp); |
| 72 | + } |
| 73 | + } catch (err: any) { |
| 74 | + setError(err?.message || "Failed to send email."); |
| 75 | + } finally { |
| 76 | + setSending(false); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + const renderPreview = () => { |
| 81 | + if (loadingPreview) return <Typography variant="body2" color="textSecondary" sx={{ mb: 2 }}>Loading recipients...</Typography>; |
| 82 | + if (!preview) return <Typography variant="body2" color="textSecondary" sx={{ mb: 2 }}>This will send an email to eligible group members.</Typography>; |
| 83 | + |
| 84 | + return ( |
| 85 | + <Alert severity={preview.eligibleCount > 0 ? "info" : "warning"} sx={{ mb: 2 }}> |
| 86 | + <strong>{preview.eligibleCount}</strong> of {preview.totalMembers} member{preview.totalMembers !== 1 ? "s" : ""} will receive this email. |
| 87 | + {preview.noEmailCount > 0 && <><br />{preview.noEmailCount} ha{preview.noEmailCount !== 1 ? "ve" : "s"} no email address on file.</>} |
| 88 | + </Alert> |
| 89 | + ); |
| 90 | + }; |
| 91 | + |
| 92 | + const renderResult = () => { |
| 93 | + if (!result) return null; |
| 94 | + return ( |
| 95 | + <> |
| 96 | + <Alert severity={result.failCount === 0 ? "success" : "warning"} sx={{ mt: 1 }}> |
| 97 | + Sent to {result.successCount} of {result.recipientCount} eligible recipient{result.recipientCount !== 1 ? "s" : ""}. |
| 98 | + {result.failCount > 0 && <><br />{result.failCount} failed to send.</>} |
| 99 | + </Alert> |
| 100 | + {result.noEmailCount > 0 && ( |
| 101 | + <Alert severity="info" sx={{ mt: 1 }}> |
| 102 | + {result.noEmailCount} skipped (no email address on file). |
| 103 | + </Alert> |
| 104 | + )} |
| 105 | + </> |
| 106 | + ); |
| 107 | + }; |
| 108 | + |
| 109 | + const selectedTemplate = templates.find(t => t.id === selectedTemplateId); |
| 110 | + const canSend = !sending && !!selectedTemplateId && (!preview || preview.eligibleCount > 0); |
| 111 | + |
| 112 | + return ( |
| 113 | + <Dialog open={true} onClose={props.onClose} maxWidth="sm" fullWidth> |
| 114 | + <DialogTitle>Email Group: {props.groupName}</DialogTitle> |
| 115 | + <DialogContent> |
| 116 | + {result ? renderResult() : ( |
| 117 | + <> |
| 118 | + {renderPreview()} |
| 119 | + {error && <Alert severity="error" sx={{ mb: 2 }}>{error}</Alert>} |
| 120 | + |
| 121 | + {loadingTemplates ? ( |
| 122 | + <Typography variant="body2" color="textSecondary">Loading templates...</Typography> |
| 123 | + ) : templates.length === 0 ? ( |
| 124 | + <Alert severity="warning"> |
| 125 | + No email templates found. <a href="/email-templates">Create one first</a>. |
| 126 | + </Alert> |
| 127 | + ) : ( |
| 128 | + <> |
| 129 | + <FormControl fullWidth sx={{ mt: 1 }}> |
| 130 | + <InputLabel>Email Template</InputLabel> |
| 131 | + <Select |
| 132 | + label="Email Template" |
| 133 | + value={selectedTemplateId} |
| 134 | + onChange={(e: SelectChangeEvent) => setSelectedTemplateId(e.target.value)} |
| 135 | + disabled={sending} |
| 136 | + > |
| 137 | + {templates.map((t) => ( |
| 138 | + <MenuItem key={t.id} value={t.id}> |
| 139 | + {t.name} {t.category ? `(${t.category})` : ""} |
| 140 | + </MenuItem> |
| 141 | + ))} |
| 142 | + </Select> |
| 143 | + </FormControl> |
| 144 | + {selectedTemplate && ( |
| 145 | + <Typography variant="caption" color="textSecondary" sx={{ mt: 1, display: "block" }}> |
| 146 | + Subject: {selectedTemplate.subject} |
| 147 | + </Typography> |
| 148 | + )} |
| 149 | + </> |
| 150 | + )} |
| 151 | + </> |
| 152 | + )} |
| 153 | + </DialogContent> |
| 154 | + <DialogActions> |
| 155 | + {result ? ( |
| 156 | + <Button onClick={props.onClose}>Close</Button> |
| 157 | + ) : ( |
| 158 | + <> |
| 159 | + <Button onClick={props.onClose} disabled={sending}>Cancel</Button> |
| 160 | + <Button |
| 161 | + variant="contained" |
| 162 | + onClick={handleSend} |
| 163 | + disabled={!canSend} |
| 164 | + startIcon={sending ? <CircularProgress size={16} /> : null} |
| 165 | + > |
| 166 | + {sending ? "Sending..." : "Send Email"} |
| 167 | + </Button> |
| 168 | + </> |
| 169 | + )} |
| 170 | + </DialogActions> |
| 171 | + </Dialog> |
| 172 | + ); |
| 173 | +}; |
0 commit comments