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:
@@ -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