| 1 |
import React, { useState, useEffect } from 'react'; |
| 2 |
import EmailTemplateList from './components/email-builder/EmailTemplateList'; |
| 3 |
import EmailDesigner from './components/email-builder/EmailDesigner'; |
| 4 |
import { Loader2 } from 'lucide-react'; |
| 5 |
|
| 6 |
const FREE_TEMPLATE_LIMIT = 3; |
| 7 |
|
| 8 |
const EmailTemplatesApp = () => { |
| 9 |
const [view, setView] = useState('list'); // 'list' | 'designer' |
| 10 |
const [templates, setTemplates] = useState([]); |
| 11 |
const [loading, setLoading] = useState(true); |
| 12 |
const [selectedTemplate, setSelectedTemplate] = useState(null); |
| 13 |
|
| 14 |
const isProActive = !! window.wpbotAutomator?.isProActive; |
| 15 |
|
| 16 |
useEffect(() => { |
| 17 |
fetchTemplates(); |
| 18 |
}, []); |
| 19 |
|
| 20 |
const fetchTemplates = async () => { |
| 21 |
setLoading(true); |
| 22 |
try { |
| 23 |
const baseUrl = window.wpbotAutomator.apiUrl.replace(/\/$/, ''); |
| 24 |
const response = await fetch(`${baseUrl}/email-templates`, { |
| 25 |
headers: { |
| 26 |
'X-WP-Nonce': window.wpbotAutomator.nonce, |
| 27 |
}, |
| 28 |
}); |
| 29 |
const data = await response.json(); |
| 30 |
setTemplates(Array.isArray(data) ? data : []); |
| 31 |
} catch (error) { |
| 32 |
console.error('Error fetching templates:', error); |
| 33 |
} finally { |
| 34 |
setLoading(false); |
| 35 |
} |
| 36 |
}; |
| 37 |
|
| 38 |
const handleCreateNew = () => { |
| 39 |
if (!isProActive && templates.length >= FREE_TEMPLATE_LIMIT) { |
| 40 |
return; // blocked — list UI shows the upgrade prompt |
| 41 |
} |
| 42 |
setSelectedTemplate(null); |
| 43 |
setView('designer'); |
| 44 |
}; |
| 45 |
|
| 46 |
const handleEdit = async (template) => { |
| 47 |
setLoading(true); |
| 48 |
try { |
| 49 |
const baseUrl = window.wpbotAutomator.apiUrl.replace(/\/$/, ''); |
| 50 |
const response = await fetch(`${baseUrl}/email-templates/${template.id}`, { |
| 51 |
headers: { |
| 52 |
'X-WP-Nonce': window.wpbotAutomator.nonce, |
| 53 |
}, |
| 54 |
}); |
| 55 |
const fullTemplate = await response.json(); |
| 56 |
setSelectedTemplate(fullTemplate); |
| 57 |
setView('designer'); |
| 58 |
} catch (error) { |
| 59 |
console.error('Error fetching template details:', error); |
| 60 |
alert('Failed to load template data.'); |
| 61 |
} finally { |
| 62 |
setLoading(false); |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
const handleBack = () => { |
| 67 |
setView('list'); |
| 68 |
fetchTemplates(); |
| 69 |
}; |
| 70 |
|
| 71 |
if (loading && view === 'list') { |
| 72 |
return ( |
| 73 |
<div className="flex items-center justify-center min-h-[400px]"> |
| 74 |
<Loader2 className="w-8 h-8 animate-spin text-primary" /> |
| 75 |
</div> |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
return ( |
| 80 |
<div className="p-8 bg-background min-h-screen text-foreground font-sans"> |
| 81 |
<div className="max-w-6xl mx-auto"> |
| 82 |
{view === 'list' ? ( |
| 83 |
<EmailTemplateList |
| 84 |
templates={templates} |
| 85 |
onCreateNew={handleCreateNew} |
| 86 |
onEdit={handleEdit} |
| 87 |
onDelete={fetchTemplates} |
| 88 |
isProActive={isProActive} |
| 89 |
freeLimit={FREE_TEMPLATE_LIMIT} |
| 90 |
/> |
| 91 |
) : ( |
| 92 |
<EmailDesigner |
| 93 |
template={selectedTemplate} |
| 94 |
onBack={handleBack} |
| 95 |
/> |
| 96 |
)} |
| 97 |
</div> |
| 98 |
</div> |
| 99 |
); |
| 100 |
}; |
| 101 |
|
| 102 |
export default EmailTemplatesApp; |
| 103 |
|