Implement modular email template system with preview and testing
- Create modular EmailTemplateRenderer with master shell and content separation - Build reusable email components library (buttons, alerts, OTP boxes, etc.) - Add EmailTemplatePreview component with master/content preview modes - Implement test email functionality for each notification template - Update NotifikasiTab to use new preview system with shortcode processing - Add dummy shortcode data for testing (nama, email, order_id, etc.) - Maintain design consistency between web and email 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
164
src/components/admin/EmailTemplatePreview.tsx
Normal file
164
src/components/admin/EmailTemplatePreview.tsx
Normal file
@@ -0,0 +1,164 @@
|
||||
import { useState } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||
import { toast } from '@/hooks/use-toast';
|
||||
import { Eye, Send, Mail } from 'lucide-react';
|
||||
import { EmailTemplateRenderer, ShortcodeProcessor } from '@/lib/email-templates/master-template';
|
||||
|
||||
interface NotificationTemplate {
|
||||
id: string;
|
||||
key: string;
|
||||
name: string;
|
||||
is_active: boolean;
|
||||
email_subject: string;
|
||||
email_body_html: string;
|
||||
webhook_url: string;
|
||||
last_payload_example?: Record<string, unknown> | null;
|
||||
}
|
||||
|
||||
interface EmailTemplatePreviewProps {
|
||||
template: NotificationTemplate;
|
||||
onTest?: (template: NotificationTemplate) => void;
|
||||
isTestSending?: boolean;
|
||||
}
|
||||
|
||||
export function EmailTemplatePreview({ template, onTest, isTestSending = false }: EmailTemplatePreviewProps) {
|
||||
const [previewMode, setPreviewMode] = useState<'master' | 'content'>('master');
|
||||
const [testEmail, setTestEmail] = useState('');
|
||||
const [showTestForm, setShowTestForm] = useState(false);
|
||||
|
||||
// Generate preview with dummy shortcode data
|
||||
const generatePreview = () => {
|
||||
const processedSubject = ShortcodeProcessor.process(template.email_subject);
|
||||
const processedContent = ShortcodeProcessor.process(template.email_body_html);
|
||||
|
||||
if (previewMode === 'master') {
|
||||
const fullHtml = EmailTemplateRenderer.render({
|
||||
subject: processedSubject,
|
||||
content: processedContent,
|
||||
brandName: 'ACCESS HUB'
|
||||
});
|
||||
return fullHtml;
|
||||
} else {
|
||||
return processedContent;
|
||||
}
|
||||
};
|
||||
|
||||
const handleTestEmail = async () => {
|
||||
if (!testEmail) {
|
||||
toast({ title: 'Error', description: 'Masukkan email tujuan', variant: 'destructive' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (onTest) {
|
||||
await onTest({ ...template, test_email: testEmail });
|
||||
}
|
||||
};
|
||||
|
||||
const previewHtml = generatePreview();
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{/* Preview Controls */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<Label>Preview Mode:</Label>
|
||||
<select
|
||||
value={previewMode}
|
||||
onChange={(e) => setPreviewMode(e.target.value as 'master' | 'content')}
|
||||
className="border-2 px-3 py-1 rounded"
|
||||
>
|
||||
<option value="master">Master Template</option>
|
||||
<option value="content">Content Only</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setShowTestForm(!showTestForm)}
|
||||
>
|
||||
<Send className="w-4 h-4 mr-2" />
|
||||
{showTestForm ? 'Cancel' : 'Test Email'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Test Email Form */}
|
||||
{showTestForm && (
|
||||
<div className="p-4 border-2 border-gray-300 rounded-lg bg-gray-50">
|
||||
<div className="space-y-3">
|
||||
<Label htmlFor="test-email">Test Email Address:</Label>
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
id="test-email"
|
||||
type="email"
|
||||
value={testEmail}
|
||||
onChange={(e) => setTestEmail(e.target.value)}
|
||||
placeholder="test@example.com"
|
||||
className="flex-1"
|
||||
/>
|
||||
<Button
|
||||
onClick={handleTestEmail}
|
||||
disabled={!testEmail || isTestSending}
|
||||
>
|
||||
<Mail className="w-4 h-4 mr-2" />
|
||||
{isTestSending ? 'Sending...' : 'Send Test'}
|
||||
</Button>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
This will send a test email with dummy data for all available shortcodes.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Preview Info */}
|
||||
<Alert>
|
||||
<Eye className="w-4 h-4" />
|
||||
<AlertDescription>
|
||||
{previewMode === 'master'
|
||||
? 'Showing complete email template with header, footer, and styling applied.'
|
||||
: 'Showing only the content section without master template styling.'
|
||||
}
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
|
||||
{/* Email Preview */}
|
||||
<div className="border-2 border-gray-300 rounded-lg overflow-hidden">
|
||||
<div className="bg-gray-100 px-4 py-2 border-b border-gray-300">
|
||||
<span className="text-sm font-mono text-gray-600">
|
||||
{previewMode === 'master' ? 'Full Email Preview' : 'Content Preview'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="max-h-96 overflow-auto bg-white">
|
||||
<iframe
|
||||
srcDoc={previewHtml}
|
||||
className="w-full border-0"
|
||||
style={{ height: '500px' }}
|
||||
sandbox="allow-same-origin"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Shortcodes Used */}
|
||||
<div className="p-3 bg-blue-50 border border-blue-200 rounded">
|
||||
<h4 className="font-semibold text-sm mb-2">Shortcodes Used in This Template:</h4>
|
||||
<div className="grid grid-cols-2 gap-2 text-sm">
|
||||
{['{nama}', '{email}', '{order_id}', '{tanggal_pesanan}', '{total}', '{metode_pembayaran}',
|
||||
'{produk}', '{link_akses}', '{tanggal_konsultasi}', '{jam_konsultasi}', '{link_meet}',
|
||||
'{judul_event}', '{tanggal_event}', '{jam_event}', '{link_event}'].filter(shortcode =>
|
||||
template.email_subject.includes(shortcode) || template.email_body_html.includes(shortcode)
|
||||
).map(shortcode => (
|
||||
<code key={shortcode} className="bg-blue-100 px-2 py-1 rounded text-xs">
|
||||
{shortcode}
|
||||
</code>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import { RichTextEditor } from '@/components/RichTextEditor';
|
||||
import { toast } from '@/hooks/use-toast';
|
||||
import { Mail, ChevronDown, ChevronUp, Webhook } from 'lucide-react';
|
||||
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible';
|
||||
import { EmailTemplatePreview } from '@/components/admin/EmailTemplatePreview';
|
||||
|
||||
interface NotificationTemplate {
|
||||
id: string;
|
||||
@@ -79,6 +80,7 @@ export function NotifikasiTab() {
|
||||
const [templates, setTemplates] = useState<NotificationTemplate[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [expandedTemplates, setExpandedTemplates] = useState<Set<string>>(new Set());
|
||||
const [testingTemplate, setTestingTemplate] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
@@ -117,6 +119,55 @@ export function NotifikasiTab() {
|
||||
else toast({ title: 'Berhasil', description: `Template "${name}" disimpan` });
|
||||
};
|
||||
|
||||
const sendTestEmail = async (template: NotificationTemplate & { test_email?: string }) => {
|
||||
if (!template.test_email) {
|
||||
toast({ title: 'Error', description: 'Masukkan email tujuan', variant: 'destructive' });
|
||||
return;
|
||||
}
|
||||
|
||||
setTestingTemplate(template.id);
|
||||
try {
|
||||
// Fetch email settings from notification_settings
|
||||
const { data: emailData } = await supabase
|
||||
.from('notification_settings')
|
||||
.select('*')
|
||||
.single();
|
||||
|
||||
if (!emailData || !emailData.api_token || !emailData.from_email) {
|
||||
throw new Error('Konfigurasi email provider belum lengkap');
|
||||
}
|
||||
|
||||
// Send test email using send-email-v2
|
||||
const { data, error } = await supabase.functions.invoke('send-email-v2', {
|
||||
body: {
|
||||
to: template.test_email,
|
||||
api_token: emailData.api_token,
|
||||
from_name: emailData.from_name,
|
||||
from_email: emailData.from_email,
|
||||
subject: template.email_subject,
|
||||
html_body: template.email_body_html,
|
||||
},
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
if (data?.success) {
|
||||
toast({ title: 'Berhasil', description: `Email test "${template.name}" dikirim ke ${template.test_email}` });
|
||||
} else {
|
||||
throw new Error(data?.message || 'Failed to send test email');
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Test template email error:', error);
|
||||
toast({
|
||||
title: 'Error',
|
||||
description: error.message || 'Gagal mengirim email test template',
|
||||
variant: 'destructive'
|
||||
});
|
||||
} finally {
|
||||
setTestingTemplate(null);
|
||||
}
|
||||
};
|
||||
|
||||
const toggleExpand = (id: string) => {
|
||||
setExpandedTemplates(prev => {
|
||||
const next = new Set(prev);
|
||||
@@ -218,48 +269,60 @@ export function NotifikasiTab() {
|
||||
</CollapsibleTrigger>
|
||||
|
||||
<CollapsibleContent>
|
||||
<div className="p-4 pt-0 space-y-4 border-t">
|
||||
<div className="space-y-2">
|
||||
<Label>Subjek Email</Label>
|
||||
<Input
|
||||
value={template.email_subject}
|
||||
onChange={(e) => {
|
||||
setTemplates(templates.map(t =>
|
||||
t.id === template.id ? { ...t, email_subject: e.target.value } : t
|
||||
));
|
||||
}}
|
||||
placeholder="Subjek email..."
|
||||
className="border-2"
|
||||
/>
|
||||
<div className="p-4 pt-0 space-y-6 border-t">
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label>Subjek Email</Label>
|
||||
<Input
|
||||
value={template.email_subject}
|
||||
onChange={(e) => {
|
||||
setTemplates(templates.map(t =>
|
||||
t.id === template.id ? { ...t, email_subject: e.target.value } : t
|
||||
));
|
||||
}}
|
||||
placeholder="Subjek email..."
|
||||
className="border-2"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Isi Email (HTML)</Label>
|
||||
<RichTextEditor
|
||||
content={template.email_body_html}
|
||||
onChange={(html) => {
|
||||
setTemplates(templates.map(t =>
|
||||
t.id === template.id ? { ...t, email_body_html: html } : t
|
||||
));
|
||||
}}
|
||||
placeholder="Tulis isi email..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label className="flex items-center gap-2">
|
||||
<Webhook className="w-4 h-4" />
|
||||
Webhook URL (opsional, untuk n8n/Zapier)
|
||||
</Label>
|
||||
<Input
|
||||
value={template.webhook_url}
|
||||
onChange={(e) => {
|
||||
setTemplates(templates.map(t =>
|
||||
t.id === template.id ? { ...t, webhook_url: e.target.value } : t
|
||||
));
|
||||
}}
|
||||
placeholder="https://n8n.example.com/webhook/..."
|
||||
className="border-2"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label>Isi Email (HTML)</Label>
|
||||
<RichTextEditor
|
||||
content={template.email_body_html}
|
||||
onChange={(html) => {
|
||||
setTemplates(templates.map(t =>
|
||||
t.id === template.id ? { ...t, email_body_html: html } : t
|
||||
));
|
||||
}}
|
||||
placeholder="Tulis isi email..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label className="flex items-center gap-2">
|
||||
<Webhook className="w-4 h-4" />
|
||||
Webhook URL (opsional, untuk n8n/Zapier)
|
||||
</Label>
|
||||
<Input
|
||||
value={template.webhook_url}
|
||||
onChange={(e) => {
|
||||
setTemplates(templates.map(t =>
|
||||
t.id === template.id ? { ...t, webhook_url: e.target.value } : t
|
||||
));
|
||||
}}
|
||||
placeholder="https://n8n.example.com/webhook/..."
|
||||
className="border-2"
|
||||
{/* Email Preview */}
|
||||
<div className="space-y-4">
|
||||
<h4 className="font-semibold">Email Preview</h4>
|
||||
<EmailTemplatePreview
|
||||
template={template}
|
||||
onTest={sendTestEmail}
|
||||
isTestSending={testingTemplate === template.id}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -272,12 +335,14 @@ export function NotifikasiTab() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Button
|
||||
onClick={() => updateTemplate(template)}
|
||||
className="shadow-sm"
|
||||
>
|
||||
Simpan Template
|
||||
</Button>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={() => updateTemplate(template)}
|
||||
className="shadow-sm flex-1"
|
||||
>
|
||||
Simpan Template
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CollapsibleContent>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user