import { useEffect, useRef, useState } from 'react';
import { adminApi } from './adminApi.js';
const TYPES = [
{
id: 'invitation',
label: 'Invitation email',
detail: 'Sent to every imported doctor.',
},
{
id: 'reminder',
label: 'Reminder email',
detail: 'Sent to imported doctors who still have Login, Consent, Agreement, Approval, or Survey pending.',
},
];
/**
* @param {{ status?: string, sentCount?: number, recipientCount?: number, error?: string | null, from?: string }} queued
*/
function sendResult(queued) {
if ((queued.recipientCount ?? 0) === 0) {
return 'No emails were sent.';
}
if (queued.status === 'sent') {
const from = queued.from ? ` from ${queued.from}` : '';
return `${queued.sentCount} emails were sent${from}.`;
}
if (queued.status === 'logged') {
return `Saved ${queued.recipientCount} emails. SendGrid is not configured.`;
}
if (queued.error) {
return queued.error;
}
return `Sent ${queued.sentCount ?? 0} of ${queued.recipientCount ?? 0} emails.`;
}
/**
* @param {{ sentCount?: number, recipientCount?: number }} queued
*/
function sendingProgress(queued) {
return `Sent ${queued.sentCount ?? 0} of ${queued.recipientCount ?? 0} emails.`;
}
/**
* @param {{ onClose: () => void }} props
*/
function useEscapeClose(onClose) {
useEffect(() => {
function onKey(event) {
if (event.key === 'Escape') {
onClose();
}
}
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [onClose]);
}
/**
* @param {{
* subject: string,
* body: string,
* onSubject: (value: string) => void,
* onBody: (value: string) => void,
* onClose: () => void,
* }} props
*/
function LetterScreen({ subject, body, onSubject, onBody, onClose }) {
useEscapeClose(onClose);
return (
);
}
export default function SendEmailsPage() {
const [type, setType] = useState('invitation');
const [recipientCount, setRecipientCount] = useState(null);
const [subject, setSubject] = useState('');
const [body, setBody] = useState('');
const [ready, setReady] = useState(false);
const [error, setError] = useState('');
const [result, setResult] = useState('');
const [busy, setBusy] = useState(false);
const [fullScreen, setFullScreen] = useState(false);
const watch = useRef(0);
useEffect(() => {
let cancelled = false;
setReady(false);
setFullScreen(false);
setError('');
adminApi(`/api/admin/emails/preview?type=${type}`)
.then((data) => {
if (!cancelled) {
setSubject(data.subject);
setBody(data.text);
setRecipientCount(data.recipientCount);
setReady(true);
}
})
.catch((loadError) => {
if (!cancelled) {
setError(loadError.message);
}
});
return () => {
cancelled = true;
};
}, [type]);
async function send(event) {
event.preventDefault();
const watchId = watch.current + 1;
watch.current = watchId;
setBusy(true);
setError('');
setResult('');
try {
let current = await adminApi('/api/admin/emails/queue', {
method: 'POST',
body: { type, subject, body },
});
while (current.status === 'queued' && current.id) {
if (watch.current !== watchId) {
return;
}
setResult(sendingProgress(current));
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
if (watch.current !== watchId) {
return;
}
current = await adminApi(`/api/admin/emails/campaigns/${current.id}`);
}
if (watch.current !== watchId) {
return;
}
setResult(sendResult(current));
} catch (sendError) {
if (watch.current === watchId) {
setError(sendError.message);
}
} finally {
if (watch.current === watchId) {
setBusy(false);
}
}
}
return (
Send emails
{fullScreen ? (
setFullScreen(false)}
/>
) : null}
);
}