Loading...

Loudoun District Newsletter

Admin Dashboard

All Newsletters

`); } async function importSubscribers(e) { e.preventDefault(); const form = e.target; const text = form.emails.value.trim(); if (!text) { toast('Enter at least one email', 'error'); return; } // Parse emails (handle newlines, commas) const emails = text.split(/[\n,]+/).map(e => e.trim()).filter(e => e.includes('@')); if (emails.length === 0) { toast('No valid emails found', 'error'); return; } // Get selected tags const selectedTags = Array.from(form.querySelectorAll('input[name="tags"]:checked')).map(cb => cb.value); try { const res = await fetch('/api/subscribers?action=import', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ emails, tags: selectedTags }) }); if (!res.ok) { const err = await res.json(); throw new Error(err.error); } const data = await res.json(); const msg = data.updated > 0 ? `Added ${data.added}, updated ${data.updated} subscribers!` : `Imported ${data.added} subscribers!`; toast(msg, 'success'); manageSubscribers(); } catch (err) { toast(err.message || 'Failed to import', 'error'); } } async function sendNewsletter(id, title) { // Fetch tags to show send options try { const [tagsRes, subsRes] = await Promise.all([ fetch('/api/subscribers?action=tags'), fetch('/api/subscribers') ]); const tagsData = await tagsRes.json(); const subsData = await subsRes.json(); openModal(`Send: ${title}`, `

Choose who should receive this newsletter.

${tagsData.tags.length > 0 ? ` ` : ''}

Note: Emails will be sent immediately. This cannot be undone.

`); } catch (err) { toast('Failed to load send options', 'error'); } } async function executeSend(e, id) { e.preventDefault(); const form = e.target; const sendTo = form.sendTo.value; let tags = null; if (sendTo === 'tags') { tags = Array.from(form.querySelectorAll('input[name="selectedTags"]:checked')).map(cb => cb.value); if (tags.length === 0) { toast('Select at least one tag', 'error'); return; } } closeModal(); toast('Sending emails...', 'info'); try { const res = await fetch(`/api/newsletters/${id}/send`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ tags }) }); const data = await res.json(); if (!res.ok) { throw new Error(data.error); } toast(`Sent to ${data.sent} subscribers!`, 'success'); if (data.failed > 0) { console.warn('Failed sends:', data.errors); } } catch (err) { toast(err.message || 'Failed to send', 'error'); } }