278 lines
8.2 KiB
JavaScript
278 lines
8.2 KiB
JavaScript
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 (
|
|
<div className="email-letter-screen" role="presentation">
|
|
<div className="email-letter-screen-sheet" role="dialog" aria-modal="true" aria-label="Email letter">
|
|
<div className="email-letter-toolbar">
|
|
<p className="m-0 text-sm font-medium text-[#134e4a]">Edit the letter</p>
|
|
<button type="button" className="admin-btn admin-btn-secondary" onClick={onClose}>
|
|
Close
|
|
</button>
|
|
</div>
|
|
<label className="grid gap-1 text-sm font-medium text-stone-600" htmlFor="email-subject-full">
|
|
Subject
|
|
<input
|
|
id="email-subject-full"
|
|
className="admin-input email-letter-subject"
|
|
value={subject}
|
|
onChange={(event) => onSubject(event.target.value)}
|
|
/>
|
|
</label>
|
|
<label className="mt-3 grid gap-1 text-sm font-medium text-stone-600" htmlFor="email-body-full">
|
|
Letter
|
|
<textarea
|
|
id="email-body-full"
|
|
className="email-letter-input"
|
|
value={body}
|
|
onChange={(event) => onBody(event.target.value)}
|
|
/>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<div>
|
|
<h1 className="admin-title">Send emails</h1>
|
|
<div className="email-send-layout">
|
|
<form className="admin-card grid content-start gap-4" onSubmit={send}>
|
|
<fieldset className="grid gap-3">
|
|
<legend className="text-sm font-medium text-stone-600">Choose an email</legend>
|
|
{TYPES.map((option) => (
|
|
<label key={option.id} className="flex items-start gap-2 text-sm">
|
|
<input
|
|
type="radio"
|
|
name="email-type"
|
|
value={option.id}
|
|
checked={type === option.id}
|
|
onChange={() => {
|
|
watch.current += 1;
|
|
setBusy(false);
|
|
setResult('');
|
|
setType(option.id);
|
|
}}
|
|
/>
|
|
<span>
|
|
<span className="font-medium text-[#134e4a]">{option.label}</span>
|
|
<span className="block text-stone-600">{option.detail}</span>
|
|
</span>
|
|
</label>
|
|
))}
|
|
</fieldset>
|
|
<p className="m-0 text-sm text-stone-600">
|
|
{recipientCount === null ? 'Counting doctors…' : `${recipientCount} doctors`}
|
|
</p>
|
|
{error ? <p className="m-0 text-red-700">{error}</p> : null}
|
|
{result ? (
|
|
<p className="admin-card-status admin-card-status-success" role="status">
|
|
{result}
|
|
</p>
|
|
) : null}
|
|
<button type="submit" className="admin-btn admin-btn-primary" disabled={busy || !ready || !subject.trim() || !body.trim()}>
|
|
Send emails
|
|
</button>
|
|
</form>
|
|
<section className="admin-card email-letter-card" aria-label="Email letter">
|
|
<div className="email-letter-toolbar">
|
|
<p className="m-0 text-xs font-semibold tracking-wide text-stone-500">Letter</p>
|
|
<button
|
|
type="button"
|
|
className="admin-btn admin-btn-secondary"
|
|
disabled={!ready}
|
|
onClick={() => setFullScreen(true)}
|
|
>
|
|
Full screen
|
|
</button>
|
|
</div>
|
|
<label className="grid gap-1 text-sm font-medium text-stone-600" htmlFor="email-subject">
|
|
Subject
|
|
<input
|
|
id="email-subject"
|
|
className="admin-input email-letter-subject"
|
|
value={subject}
|
|
onChange={(event) => setSubject(event.target.value)}
|
|
/>
|
|
</label>
|
|
<label className="mt-3 grid gap-1 text-sm font-medium text-stone-600" htmlFor="email-body">
|
|
Letter
|
|
<textarea
|
|
id="email-body"
|
|
className="email-letter-input"
|
|
value={body}
|
|
onChange={(event) => setBody(event.target.value)}
|
|
/>
|
|
</label>
|
|
</section>
|
|
</div>
|
|
{fullScreen ? (
|
|
<LetterScreen
|
|
subject={subject}
|
|
body={body}
|
|
onSubject={setSubject}
|
|
onBody={setBody}
|
|
onClose={() => setFullScreen(false)}
|
|
/>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|