import React, { useState, useEffect } from 'react'; import { Eye, EyeOff } from 'lucide-react'; import { toast } from 'sonner'; import { api } from '@/api'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'; import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem, } from '@/components/ui/select'; import { SectionHeading, FieldRow, Toggle } from './adminShared'; export default function EmailNotifCard() { const DEFAULTS = { enabled: false, sender_name: '', sender_address: '', smtp_host: '', smtp_port: '587', smtp_encryption: 'starttls', smtp_self_signed: false, smtp_username: '', smtp_password: '', allow_user_config: false, global_recipient: '', }; const [cfg, setCfg] = useState(DEFAULTS); const [loading, setLoading] = useState(true); const [loadError, setLoadError] = useState(''); const [saving, setSaving] = useState(false); const [showPw, setShowPw] = useState(false); const [testEmail, setTestEmail] = useState(''); const [testing, setTesting] = useState(false); useEffect(() => { api.notifAdmin() .then(d => setCfg({ ...DEFAULTS, ...d })) .catch(err => setLoadError(err.message || 'Failed to load email settings')) .finally(() => setLoading(false)); }, []); // eslint-disable-line react-hooks/exhaustive-deps const set = (k, v) => setCfg(p => ({ ...p, [k]: v })); const handleSave = async () => { setSaving(true); try { await api.saveNotifAdmin(cfg); toast.success('Email settings saved.'); } catch (err) { toast.error(err.message || 'Failed to save.'); } finally { setSaving(false); } }; const handleTest = async () => { if (!testEmail) { toast.error('Enter a recipient email.'); return; } setTesting(true); try { await api.testEmail({ to: testEmail }); toast.success('Test email sent.'); } catch (err) { toast.error(err.message || 'Failed to send test email.'); } finally { setTesting(false); } }; if (loading) return Loading…; if (loadError) return {loadError}; return ( Email Notifications Enable email notifications Configure SMTP to send bill reminders set('enabled', v)} label="Enable email notifications" /> Sender set('sender_name', e.target.value)} placeholder="BillTracker" /> set('sender_address', e.target.value)} placeholder="no-reply@example.com" type="email" /> SMTP Server set('smtp_host', e.target.value)} placeholder="smtp.example.com" /> set('smtp_port', e.target.value)} placeholder="587" type="number" className="w-28" /> set('smtp_encryption', v)}> STARTTLS SSL/TLS None set('smtp_self_signed', e.target.checked)} className="h-4 w-4 rounded border-input bg-input accent-primary" /> Accept self-signed certificates set('smtp_username', e.target.value)} placeholder="user@example.com" /> set('smtp_password', e.target.value)} placeholder="••••••••" className="pr-9" /> setShowPw(p => !p)} className="absolute right-2.5 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors" > {showPw ? : } User Access set('allow_user_config', e.target.checked)} className="h-4 w-4 rounded border-input bg-input accent-primary" /> Let users configure their own notification preferences set('global_recipient', e.target.value)} placeholder="recipient@example.com" type="email" /> Test Email setTestEmail(e.target.value)} placeholder="you@example.com" type="email" /> {testing ? 'Sending…' : 'Send Test Email'} {saving ? 'Saving…' : 'Save Settings'} ); }
Enable email notifications
Configure SMTP to send bill reminders