Spaces:
Sleeping
Sleeping
File size: 20,092 Bytes
d19cc77 493ea13 d19cc77 b55c72c d19cc77 530365a d19cc77 493ea13 d19cc77 530365a d19cc77 530365a d19cc77 b55c72c d19cc77 530365a d19cc77 493ea13 d19cc77 b55c72c d19cc77 b55c72c d19cc77 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { motion } from 'framer-motion';
import { FaPaperPlane, FaDownload, FaChevronLeft, FaEnvelope, FaKey } from 'react-icons/fa';
const ReviewSendStep = ({ fileData, designParams, mappings, onBack }) => {
const [eventName, setEventName] = useState("");
const [eventDate, setEventDate] = useState("");
const [clientCompany, setClientCompany] = useState("");
const [subject, setSubject] = useState("Your Attendance Certificate - {event_name}_{event_date}");
const [body, setBody] = useState("Dear Dr {first_name},\n\nThank you for attending {event_name}.\n\nYour certificate is attached.\n\nBest Regards,\n\nVolaris Team on behalf of {client_company}");
const [fromEmail, setFromEmail] = useState("");
const [senderName, setSenderName] = useState("Volaris Team");
const [sending, setSending] = useState(false);
const [status, setStatus] = useState(null);
const [progress, setProgress] = useState(null);
const [jobId, setJobId] = useState(null);
const [failedEmails, setFailedEmails] = useState([]); // Store failed email details
// Fetch email configuration on mount
useEffect(() => {
const fetchEmailConfig = async () => {
try {
const response = await axios.get("/api/email/config");
setFromEmail(response.data.from_email);
} catch (error) {
console.error("Failed to fetch email config:", error);
}
};
fetchEmailConfig();
}, []);
const handleSend = async () => {
if (!eventName || !eventDate || !clientCompany) {
alert("Please fill in all required fields: Event Name, Event Date, and Client Company");
return;
}
setSending(true);
const formData = new FormData();
formData.append("subject", subject);
formData.append("body", body);
formData.append("name_column", mappings.name_column);
formData.append("email_column", mappings.email_column);
formData.append("event_name", eventName);
formData.append("event_date", eventDate);
formData.append("client_company", clientCompany);
formData.append("sender_name", senderName);
formData.append("name_color", designParams.name_color);
formData.append("font_size", designParams.font_size);
formData.append("fontname", designParams.fontname);
if (designParams.x !== null) formData.append("x", designParams.x);
if (designParams.y !== null) formData.append("y", designParams.y);
try {
const response = await axios.post("/api/email/send", formData);
const newJobId = response.data.job_id;
setJobId(newJobId);
setProgress({ sent: 0, total: fileData.total_rows, failed: 0 });
// Start polling for progress
const pollInterval = setInterval(async () => {
try {
const progressResponse = await axios.get(`/api/email/progress/${newJobId}`);
const progressData = progressResponse.data;
setProgress(progressData);
// Stop polling when all emails are sent
if (progressData.sent >= progressData.total) {
clearInterval(pollInterval);
setSending(false);
// Store failed emails for display
if (progressData.failed_emails && progressData.failed_emails.length > 0) {
setFailedEmails(progressData.failed_emails);
}
const successCount = progressData.sent - progressData.failed;
setStatus({
type: progressData.failed > 0 ? 'warning' : 'success',
msg: `Successfully sent ${successCount} emails!${progressData.failed > 0 ? ` (${progressData.failed} failed)` : ''}`
});
setProgress(null);
}
} catch (pollError) {
console.error("Progress polling error:", pollError);
}
}, 500); // Poll every 500ms
} catch (error) {
setStatus({ type: 'error', msg: "Failed to start sending process." });
console.error(error);
setSending(false);
}
};
const handleDownload = async () => {
const formData = new FormData();
formData.append("name_column", mappings.name_column);
formData.append("email_column", mappings.email_column);
formData.append("name_color", designParams.name_color);
formData.append("font_size", designParams.font_size);
formData.append("fontname", designParams.fontname);
if (designParams.x !== null) formData.append("x", designParams.x);
if (designParams.y !== null) formData.append("y", designParams.y);
try {
const response = await axios.post("/api/certificates/generate", formData, {
responseType: 'blob'
});
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'certificates.zip');
document.body.appendChild(link);
link.click();
} catch (error) {
console.error("Download failed", error);
}
};
return (
<div className="max-w-5xl mx-auto">
<div className="text-center mb-8">
<h2 className="text-2xl font-bold text-gray-900 mb-2">Review & Send</h2>
<p className="text-gray-600">Configure your email settings and launch the campaign.</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-8">
{/* Email Configuration */}
<div className="space-y-6">
<div className="bg-white border border-gray-200 p-6 rounded-lg shadow-sm">
<h3 className="text-lg font-semibold text-gray-900 mb-4 flex items-center">
Event Details
</h3>
<div className="space-y-4">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Event Name *</label>
<input
type="text"
className="w-full border border-gray-300 rounded-lg p-3 text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"
placeholder="e.g., Annual Medical Conference"
value={eventName}
onChange={(e) => setEventName(e.target.value)}
/>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Event Date *</label>
<input
type="text"
className="w-full border border-gray-300 rounded-lg p-3 text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"
placeholder="e.g., November 2024"
value={eventDate}
onChange={(e) => setEventDate(e.target.value)}
/>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Client Company *</label>
<input
type="text"
className="w-full border border-gray-300 rounded-lg p-3 text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"
placeholder="e.g., Pharmaceutical Corp"
value={clientCompany}
onChange={(e) => setClientCompany(e.target.value)}
/>
</div>
</div>
</div>
<div className="bg-white border border-gray-200 p-6 rounded-lg shadow-sm">
<h3 className="text-lg font-semibold text-gray-900 mb-4 flex items-center">
<FaEnvelope className="mr-2 text-blue-600" /> Email Configuration
</h3>
<div className="space-y-4">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Sender Name</label>
<input
type="text"
className="w-full border border-gray-300 rounded-lg p-3 text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"
placeholder="e.g., Volaris Team"
value={senderName}
onChange={(e) => setSenderName(e.target.value)}
/>
<p className="text-xs text-gray-500 mt-1">This name appears in the recipient's inbox</p>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Sender Email</label>
<div className="w-full border border-gray-200 rounded-lg p-3 bg-gray-50 text-gray-700 font-medium">
{fromEmail || 'Loading...'}
</div>
<p className="text-xs text-gray-500 mt-1">Configured in environment settings</p>
</div>
</div>
</div>
<div className="bg-white border border-gray-200 p-6 rounded-lg shadow-sm">
<h3 className="text-lg font-semibold text-gray-900 mb-4 flex items-center">
<FaEnvelope className="mr-2 text-blue-600" /> Email Content
</h3>
<div className="space-y-4">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Subject Line</label>
<input
type="text"
className="w-full border border-gray-300 rounded-lg p-3 text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"
value={subject}
onChange={(e) => setSubject(e.target.value)}
/>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Email Body</label>
<textarea
rows={6}
className="w-full border border-gray-300 rounded-lg p-3 text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none font-mono text-sm"
value={body}
onChange={(e) => setBody(e.target.value)}
/>
<p className="text-xs text-gray-500 mt-2">
Variables: <span className="text-blue-600 font-mono">{'{first_name}'}</span>, <span className="text-blue-600 font-mono">{'{name}'}</span>, <span className="text-blue-600 font-mono">{'{event_name}'}</span>, <span className="text-blue-600 font-mono">{'{event_date}'}</span>, <span className="text-blue-600 font-mono">{'{client_company}'}</span>
</p>
</div>
</div>
</div>
</div>
{/* Summary & Actions */}
<div className="space-y-6">
<div className="bg-gradient-to-br from-blue-50 to-blue-100 border border-blue-200 p-6 rounded-lg">
<h3 className="text-lg font-semibold text-gray-900 mb-4">Campaign Summary</h3>
<ul className="space-y-3 text-sm text-gray-700">
<li className="flex justify-between border-b border-blue-200 pb-2">
<span>Total Recipients</span>
<span className="font-bold text-gray-900">{fileData.total_rows}</span>
</li>
<li className="flex justify-between border-b border-blue-200 pb-2">
<span>Name Column</span>
<span className="font-mono text-blue-700">{mappings.name_column}</span>
</li>
<li className="flex justify-between border-b border-blue-200 pb-2">
<span>Email Column</span>
<span className="font-mono text-blue-700">{mappings.email_column}</span>
</li>
</ul>
</div>
<div className="space-y-4">
<motion.button
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
onClick={handleDownload}
className="w-full py-4 px-6 border border-blue-600 text-blue-600 rounded-lg hover:bg-blue-50 font-medium flex items-center justify-center space-x-2 transition-colors"
>
<FaDownload />
<span>Download All Certificates (ZIP)</span>
</motion.button>
<motion.button
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
onClick={handleSend}
disabled={sending}
className={`w-full py-4 px-6 rounded-lg text-white font-bold shadow-md flex items-center justify-center space-x-2 transition-all ${sending
? 'bg-gray-400 cursor-not-allowed'
: 'bg-green-600 hover:bg-green-700 hover:shadow-lg'
}`}
>
{sending ? (
<span>Sending Campaign...</span>
) : (
<>
<FaPaperPlane />
<span>Send Certificates Now</span>
</>
)}
</motion.button>
</div>
{progress && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
className="bg-white border border-blue-200 p-6 rounded-lg shadow-sm"
>
<h4 className="text-sm font-semibold text-gray-900 mb-3">Sending Progress</h4>
<div className="space-y-3">
<div className="flex justify-between text-sm text-gray-700 mb-2">
<span className="font-medium">{progress.sent} / {progress.total} emails sent</span>
<span className="text-blue-600 font-bold">{Math.round((progress.sent / progress.total) * 100)}%</span>
</div>
<div className="w-full bg-gray-200 rounded-full h-3 overflow-hidden">
<motion.div
className="bg-gradient-to-r from-blue-500 to-green-500 h-full rounded-full"
initial={{ width: 0 }}
animate={{ width: `${(progress.sent / progress.total) * 100}%` }}
transition={{ duration: 0.3 }}
/>
</div>
{progress.failed > 0 && (
<p className="text-xs text-red-600">⚠️ {progress.failed} failed</p>
)}
</div>
</motion.div>
)}
{status && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
className={`p-4 rounded-lg border text-center ${status.type === 'success'
? 'bg-green-50 border-green-200 text-green-700'
: status.type === 'warning'
? 'bg-yellow-50 border-yellow-200 text-yellow-700'
: 'bg-red-50 border-red-200 text-red-700'
}`}
>
{status.msg}
</motion.div>
)}
{/* Failed Emails Details */}
{failedEmails.length > 0 && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
className="bg-red-50 border border-red-200 p-6 rounded-lg"
>
<h4 className="text-sm font-semibold text-red-800 mb-3 flex items-center">
⚠️ Failed Emails ({failedEmails.length})
</h4>
<div className="max-h-48 overflow-y-auto space-y-2">
{failedEmails.map((failed, index) => (
<div key={index} className="bg-white rounded p-3 border border-red-100">
<div className="flex justify-between items-start">
<div>
<p className="text-sm font-medium text-gray-900">{failed.name}</p>
<p className="text-xs text-gray-600">{failed.email}</p>
</div>
</div>
<p className="text-xs text-red-600 mt-1 font-mono">{failed.error}</p>
</div>
))}
</div>
</motion.div>
)}
</div>
</div>
<div className="mt-8">
<button
onClick={onBack}
className="text-gray-600 hover:text-gray-900 flex items-center space-x-2 transition-colors font-medium"
>
<FaChevronLeft />
<span>Back to Design</span>
</button>
</div>
</div>
);
};
export default ReviewSendStep;
|