Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 4x 12x 9x 1x 1x 8x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 2x 2x 2x 2x 2x 12x 1x 1x 1x 1x 1x 11x | /**
* EventRegistrantNoticesTab (Story 7.3 hardening)
*
* Organizer tab to send a REGISTRANT-targeted notice (e.g. the "slides are online" mail) to an
* event's ACTIVE REGISTRANTS — never the global newsletter-subscriber pool. Mirrors the Newsletter
* tab UX (template select, language, preview iframe, send + confirm) but is wired to the dedicated
* registrant-notice endpoints.
*
* The language selector controls the PREVIEW only — the actual send resolves each registrant's own
* language (de-anything to German, en to English, otherwise German fallback).
*/
import React, { useEffect, useState } from 'react';
import {
Alert,
Box,
Button,
CircularProgress,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
FormControl,
InputLabel,
Link,
MenuItem,
Paper,
Select,
Stack,
Typography,
} from '@mui/material';
import { Email as EmailIcon } from '@mui/icons-material';
import axios from 'axios';
import { useTranslation } from 'react-i18next';
import { useEmailTemplates } from '@/hooks/useEmailTemplates';
import type { EmailTemplateResponse } from '@/services/emailTemplateService';
import {
usePreviewRegistrantNotice,
useSendRegistrantNotice,
} from '@/hooks/useRegistrantNotice/useRegistrantNotice';
import { useSendStatus } from '@/hooks/useNewsletter/useNewsletter';
import { useBreakpoints } from '@/hooks/useBreakpoints';
interface EventRegistrantNoticesTabProps {
eventCode: string;
eventTitle: string;
}
export const EventRegistrantNoticesTab: React.FC<EventRegistrantNoticesTabProps> = ({
eventCode,
eventTitle,
}) => {
const { t } = useTranslation(['events', 'organizer', 'common']);
const { isMobile } = useBreakpoints();
const [locale, setLocale] = useState<'de' | 'en'>('de');
const [selectedTemplateKey, setSelectedTemplateKey] = useState<string>('');
const [previewHtml, setPreviewHtml] = useState<string | null>(null);
const [recipientCount, setRecipientCount] = useState<number | null>(null);
const [confirmOpen, setConfirmOpen] = useState(false);
/** sendId of the most recently triggered send — polled for its final status. */
const [activeSendId, setActiveSendId] = useState<string | null>(null);
const templatesQuery = useEmailTemplates({ category: 'REGISTRANT_NOTICE' });
const previewMutation = usePreviewRegistrantNotice(eventCode);
const sendMutation = useSendRegistrantNotice(eventCode);
// Reuse the template-agnostic newsletter send-status endpoint (polls until terminal).
const sendStatusQuery = useSendStatus(eventCode, activeSendId);
const filteredTemplates: EmailTemplateResponse[] = React.useMemo(
() => (templatesQuery.data ?? []).filter((tpl) => tpl.locale === locale),
[templatesQuery.data, locale]
);
// Keep a valid template selected as the locale/template list changes.
useEffect(() => {
if (filteredTemplates.length === 0) {
setSelectedTemplateKey('');
return;
}
if (!filteredTemplates.some((tpl) => tpl.templateKey === selectedTemplateKey)) {
setSelectedTemplateKey(filteredTemplates[0].templateKey);
}
}, [filteredTemplates, selectedTemplateKey]);
function handlePreview() {
Iif (!selectedTemplateKey) return;
setActiveSendId(null);
previewMutation.mutate(
{ templateKey: selectedTemplateKey, locale },
{
onSuccess: (data) => {
setPreviewHtml(data.htmlPreview);
setRecipientCount(data.recipientCount);
},
}
);
}
// Open the confirm dialog with a real recipient count. If the organizer hasn't previewed yet,
// fetch the count first so the dialog never shows a misleading "0".
function handleOpenConfirm() {
Iif (!selectedTemplateKey) return;
Iif (recipientCount !== null) {
setConfirmOpen(true);
return;
}
previewMutation.mutate(
{ templateKey: selectedTemplateKey, locale },
{
onSuccess: (data) => {
setPreviewHtml(data.htmlPreview);
setRecipientCount(data.recipientCount);
setConfirmOpen(true);
},
}
);
}
function handleConfirmSend() {
Iif (!selectedTemplateKey) return;
sendMutation.mutate(selectedTemplateKey, {
onSuccess: (data) => {
setConfirmOpen(false);
setActiveSendId(data.sendId); // begin polling for the final result
},
onError: () => setConfirmOpen(false),
});
}
const noTemplates = !templatesQuery.isLoading && filteredTemplates.length === 0;
// Surface the API error. The 409 "already sent / in progress" guard is an expected outcome the
// organizer must understand (shown as an info, not a red failure); other errors show the API
// message when present, else a generic fallback.
const apiError: { severity: 'info' | 'error'; message: string } | null = (() => {
const err = sendMutation.error ?? previewMutation.error;
if (!err) return null;
Eif (axios.isAxiosError(err)) {
const status = err.response?.status;
const apiMsg = (err.response?.data as { message?: string } | undefined)?.message;
Eif (status === 409) {
return {
severity: 'info',
message: t('eventPage.registrantNotices.alreadySent', {
defaultValue:
apiMsg ??
'This notice has already been sent (or a send is in progress) for this event.',
}),
};
}
if (apiMsg) return { severity: 'error', message: apiMsg };
}
return {
severity: 'error',
message: t('eventPage.registrantNotices.error', 'Something went wrong. Please try again.'),
};
})();
return (
<Stack spacing={4}>
<Box>
<Typography variant="h6" gutterBottom>
{t('eventPage.registrantNotices.title', 'Registrant Notices')}
</Typography>
<Typography variant="body2" color="text.secondary">
{t(
'eventPage.registrantNotices.description',
'Send a one-off notice (e.g. “the slides are online”) to this event’s registrants. Recipients are the active registrants — not newsletter subscribers — and each gets the mail in their own language.'
)}
</Typography>
</Box>
{activeSendId &&
sendStatusQuery.data &&
(() => {
const s = sendStatusQuery.data;
const sending = s.status === 'PENDING' || s.status === 'IN_PROGRESS';
Iif (sending) {
return (
<Alert severity="info" data-testid="rn-send-progress">
{t('eventPage.registrantNotices.sending', {
sent: s.sentCount,
total: s.totalCount,
defaultValue: `Sending… ${s.sentCount} / ${s.totalCount}`,
})}
</Alert>
);
}
// Terminal: skipped = registrants with no resolvable email on file.
const skipped = Math.max(0, s.totalCount - s.sentCount - s.failedCount);
return (
<Alert
severity={s.sentCount > 0 ? 'success' : 'warning'}
onClose={() => setActiveSendId(null)}
data-testid="rn-send-success"
>
{t('eventPage.registrantNotices.sendResult', {
sent: s.sentCount,
total: s.totalCount,
defaultValue: `Sent to ${s.sentCount} of ${s.totalCount} registrants.`,
})}
{skipped > 0 &&
' ' +
t('eventPage.registrantNotices.sendResultSkipped', {
count: skipped,
defaultValue: `${skipped} had no email address on file (skipped).`,
})}
{s.failedCount > 0 &&
' ' +
t('eventPage.registrantNotices.sendResultFailed', {
count: s.failedCount,
defaultValue: `${s.failedCount} failed.`,
})}
</Alert>
);
})()}
<Box
sx={{
display: 'grid',
gridTemplateColumns: { xs: '1fr', md: 'minmax(300px, 34%) 1fr' },
gap: 3,
alignItems: 'start',
}}
>
<Paper variant="outlined" sx={{ p: 2 }}>
{/* LEFT: the form controls */}
<Stack spacing={2}>
{/* Preview language */}
<FormControl size="small">
<InputLabel>{t('eventPage.registrantNotices.locale', 'Preview language')}</InputLabel>
<Select
value={locale}
label={t('eventPage.registrantNotices.locale', 'Preview language')}
onChange={(e) => {
setLocale(e.target.value as 'de' | 'en');
setPreviewHtml(null);
}}
SelectDisplayProps={
{ 'data-testid': 'rn-locale-select' } as React.HTMLAttributes<HTMLDivElement>
}
>
<MenuItem value="de">Deutsch</MenuItem>
<MenuItem value="en">English</MenuItem>
</Select>
</FormControl>
{/* Template selector — REGISTRANT_NOTICE category only */}
<FormControl size="small">
<InputLabel>{t('eventPage.registrantNotices.templateSelect', 'Template')}</InputLabel>
<Select
value={selectedTemplateKey}
label={t('eventPage.registrantNotices.templateSelect', 'Template')}
onChange={(e) => {
setSelectedTemplateKey(e.target.value);
setPreviewHtml(null);
}}
disabled={templatesQuery.isLoading || noTemplates}
SelectDisplayProps={
{ 'data-testid': 'rn-template-select' } as React.HTMLAttributes<HTMLDivElement>
}
>
{filteredTemplates.map((tpl) => (
<MenuItem key={tpl.templateKey} value={tpl.templateKey}>
{tpl.templateKey}
</MenuItem>
))}
</Select>
</FormControl>
{noTemplates && (
<Alert severity="info" data-testid="rn-no-templates">
{t(
'eventPage.registrantNotices.noTemplates',
'No registrant-notice templates for this language yet.'
)}{' '}
<Link href="/organizer/admin?tab=email-templates" underline="hover">
{t('organizer:newsletter.templateSelect.createNew', 'Create one')} ↗
</Link>
</Alert>
)}
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={1} flexWrap="wrap">
<Button
variant="outlined"
startIcon={
previewMutation.isPending ? <CircularProgress size={16} /> : <EmailIcon />
}
onClick={handlePreview}
disabled={previewMutation.isPending || !selectedTemplateKey}
data-testid="rn-preview-button"
>
{t('eventPage.registrantNotices.preview', 'Preview')}
</Button>
<Button
variant="contained"
onClick={handleOpenConfirm}
disabled={
sendMutation.isPending || previewMutation.isPending || !selectedTemplateKey
}
data-testid="rn-send-button"
>
{t('eventPage.registrantNotices.send', 'Send to registrants')}
</Button>
</Stack>
{recipientCount !== null && (
<Typography variant="caption" color="text.secondary" data-testid="rn-recipient-count">
{t('eventPage.registrantNotices.recipientCount', {
count: recipientCount,
defaultValue: `${recipientCount} active registrants`,
})}
</Typography>
)}
{apiError && (
<Alert severity={apiError.severity} data-testid="rn-error">
{apiError.message}
</Alert>
)}
</Stack>
</Paper>
<Paper variant="outlined" sx={{ p: 2 }}>
{/* RIGHT: preview */}
<Typography variant="body2" color="text.secondary" gutterBottom data-testid="sec-label">
{t('eventPage.registrantNotices.previewTitle', 'Email Preview')}
</Typography>
{previewHtml ? (
<Box
component="iframe"
srcDoc={previewHtml}
sx={{
width: '100%',
height: 600,
border: '1px solid',
borderColor: 'divider',
borderRadius: 1,
}}
title="registrant-notice-preview"
sandbox="allow-same-origin"
data-testid="rn-preview-iframe"
/>
) : (
<Typography variant="body2" color="text.secondary">
Preview will appear here
</Typography>
)}
</Paper>
</Box>
{/* Confirmation dialog */}
<Dialog open={confirmOpen} onClose={() => setConfirmOpen(false)} fullScreen={isMobile}>
<DialogTitle>
{t('eventPage.registrantNotices.confirmTitle', 'Send to registrants?')}
</DialogTitle>
<DialogContent>
<DialogContentText>
{t('eventPage.registrantNotices.confirmBody', {
templateKey: selectedTemplateKey,
eventTitle,
count: recipientCount ?? 0,
defaultValue:
recipientCount !== null
? `Send '${selectedTemplateKey}' to ${recipientCount} active registrants of «${eventTitle}»? Each receives it in their own language.`
: `Send '${selectedTemplateKey}' to the active registrants of «${eventTitle}»? Each receives it in their own language.`,
})}
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => setConfirmOpen(false)} disabled={sendMutation.isPending}>
{t('common:actions.cancel', 'Cancel')}
</Button>
<Button
onClick={handleConfirmSend}
variant="contained"
disabled={sendMutation.isPending}
autoFocus
data-testid="rn-confirm-send"
>
{sendMutation.isPending ? (
<CircularProgress size={20} />
) : (
t('common.confirm', 'Confirm')
)}
</Button>
</DialogActions>
</Dialog>
</Stack>
);
};
|