import { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import apiClient from '../api/client';
import WhitelistModal from '../components/WhitelistModal';
import TestSmsModal from '../components/TestSmsModal';
const STATUS_CONFIG = {
generated: { label: 'Generated', bg: 'bg-page-bg', text: 'text-text-muted', border: 'border-border-main' },
pending_whitelisting: { label: 'Pending Whitelisting', bg: 'bg-tags-bg', text: 'text-tags-text', border: 'border-tags-border' },
whitelisted: { label: 'Published', bg: 'bg-badge-bg', text: 'text-badge-text', border: 'border-badge-border' },
};
export default function Templates() {
const { businessId } = useParams();
const [templates, setTemplates] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
const [whitelistTarget, setWhitelistTarget] = useState(null);
const [testTarget, setTestTarget] = useState(null);
const [activeTab, setActiveTab] = useState('published'); // 'published' | 'pending'
async function loadTemplates() {
setLoading(true);
try {
const res = await apiClient.get(`/api/businesses/${businessId}/templates`);
// Show all templates that have a selected template (status != generated or status exists)
const all = (res.data.templates || []).filter(t => t.selectedTemplate);
setTemplates(all);
} catch {
setError('Failed to load templates');
} finally {
setLoading(false);
}
}
useEffect(() => { loadTemplates(); }, [businessId]);
function handleWhitelistSuccess(slug, templateId) {
setTemplates(ts => ts.map(t =>
t.eventSlug === slug ? { ...t, status: 'whitelisted', templateId } : t
));
setWhitelistTarget(null);
}
if (loading) {
return (
);
}
return (
{/* Header */}
Templates
Track whitelisting status and test your SMS templates.
{error && (
{error}
)}
{/* Tabs */}
{templates.length === 0 ? (
No Templates Yet
Generate and select templates in the Events section first.
) : (() => {
const publishedTabs = templates.filter(t => t.status === 'whitelisted');
const pendingTabs = templates.filter(t => t.status === 'pending_whitelisting');
const visibleTemplates = activeTab === 'published' ? publishedTabs : pendingTabs;
if (visibleTemplates.length === 0) {
return (
No templates in {activeTab === 'published' ? 'Published' : 'Pending'}.
);
}
return (
{visibleTemplates.map(tmpl => {
const statusCfg = STATUS_CONFIG[tmpl.status] || STATUS_CONFIG.generated;
return (
{/* Card header */}
{tmpl.eventLabel || tmpl.eventSlug.replace(/_/g, ' ')}
{tmpl.eventSlug}
{statusCfg.label}
{/* Template text */}
{tmpl.selectedTemplate}
{/* Template ID (if whitelisted) */}
{tmpl.templateId && (
{tmpl.templateId}
)}
{/* Variable map */}
{tmpl.variableMap && Object.keys(tmpl.variableMap).length > 0 && (
{Object.entries(tmpl.variableMap).map(([key, val]) => (
{key}
→
{val}
))}
)}
{/* Actions */}
{tmpl.status === 'pending_whitelisting' && (
)}
{tmpl.status === 'whitelisted' && (
)}
{tmpl.status === 'pending_whitelisting' && (
Submit to DLT portal, then enter your Template ID here.
)}
);
})}
);
})()}
{whitelistTarget && (
setWhitelistTarget(null)}
onSuccess={handleWhitelistSuccess}
/>
)}
{testTarget && (
setTestTarget(null)}
/>
)}
);
}