PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.1
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.1
8.8.1 8.8.0 8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 All 537 releases
chatbot / addons / automator / src / EmailTemplatesApp.jsx

EmailTemplatesApp.jsx in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.8.1, at addons/automator/src/EmailTemplatesApp.jsx

103 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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