import React, { useState, useEffect } from 'react'; import { db } from '../lib/firebase'; import { collection, addDoc, updateDoc, deleteDoc, doc, getDocs, query, where } from 'firebase/firestore'; import { useAuth } from '../context/AuthContext'; interface EmailTemplate { id: string; name: string; subject: string; body: string; type: 'prayer_update' | 'reminder' | 'summary'; createdAt: string; updatedAt: string; } interface EmailTemplatesManagerProps { onTemplateSelect?: (templateId: string) => void; selectedTemplateId?: string; } export default function EmailTemplatesManager({ onTemplateSelect, selectedTemplateId }: EmailTemplatesManagerProps) { const { user } = useAuth(); const [templates, setTemplates] = useState([]); const [loading, setLoading] = useState(true); const [showForm, setShowForm] = useState(false); const [editingTemplate, setEditingTemplate] = useState(null); const [form, setForm] = useState({ name: '', subject: '', body: '', type: 'prayer_update' as EmailTemplate['type'] }); useEffect(() => { if (user) { loadTemplates(); } }, [user]); const loadTemplates = async () => { if (!user) return; try { const q = query(collection(db, 'emailTemplates'), where('userId', '==', user.uid)); const querySnapshot = await getDocs(q); const templatesData = querySnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })) as EmailTemplate[]; setTemplates(templatesData); } catch (error) { console.error('Error loading email templates:', error); } finally { setLoading(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!user) return; try { const templateData = { ...form, userId: user.uid, updatedAt: new Date().toISOString() }; if (editingTemplate) { await updateDoc(doc(db, 'emailTemplates', editingTemplate.id), templateData); } else { await addDoc(collection(db, 'emailTemplates'), { ...templateData, createdAt: new Date().toISOString() }); } setForm({ name: '', subject: '', body: '', type: 'prayer_update' }); setShowForm(false); setEditingTemplate(null); loadTemplates(); } catch (error) { console.error('Error saving email template:', error); } }; const handleEdit = (template: EmailTemplate) => { setEditingTemplate(template); setForm({ name: template.name, subject: template.subject, body: template.body, type: template.type }); setShowForm(true); }; const handleDelete = async (templateId: string) => { if (!confirm('Are you sure you want to delete this email template?')) return; try { await deleteDoc(doc(db, 'emailTemplates', templateId)); loadTemplates(); } catch (error) { console.error('Error deleting email template:', error); } }; const handleCancel = () => { setShowForm(false); setEditingTemplate(null); setForm({ name: '', subject: '', body: '', type: 'prayer_update' }); }; const getTypeLabel = (type: EmailTemplate['type']) => { switch (type) { case 'prayer_update': return 'Prayer Update'; case 'reminder': return 'Reminder'; case 'summary': return 'Summary'; default: return type; } }; const getTypeColor = (type: EmailTemplate['type']) => { switch (type) { case 'prayer_update': return 'bg-blue-600'; case 'reminder': return 'bg-orange-600'; case 'summary': return 'bg-green-600'; default: return 'bg-gray-600'; } }; if (loading) { return
Loading email templates...
; } return (

📧 Email Templates

{showForm && (

{editingTemplate ? 'Edit Email Template' : 'Create New Email Template'}

setForm(prev => ({ ...prev, name: e.target.value }))} className="w-full p-3 bg-gray-600 border border-gray-500 rounded-lg text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="e.g., Weekly Prayer Update" required />
setForm(prev => ({ ...prev, subject: e.target.value }))} className="w-full p-3 bg-gray-600 border border-gray-500 rounded-lg text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="e.g., Your Weekly Prayer Update" required />