import React, { useState, useEffect, useRef } from 'react'; import axios from 'axios'; import { motion } from 'framer-motion'; import { FaPalette, FaFont, FaArrowsAlt, FaChevronLeft, FaChevronRight, FaMousePointer } from 'react-icons/fa'; const DesignStep = ({ fileData, designParams, setDesignParams, mappings, setMappings, onNext, onBack }) => { const [previewImage, setPreviewImage] = useState(null); const [loadingPreview, setLoadingPreview] = useState(false); const [previewName, setPreviewName] = useState("Sample Name"); const [pdfSize, setPdfSize] = useState({ width: 595, height: 842 }); const imageRef = useRef(null); // Fetch clean preview once on mount useEffect(() => { fetchCleanPreview(); }, []); const fetchCleanPreview = async () => { setLoadingPreview(true); const formData = new FormData(); formData.append("name", " "); // Space for clean background (avoids empty string issues) formData.append("name_color", "#000000"); formData.append("font_size", "60"); formData.append("fontname", "helv"); try { const response = await axios.post("/api/certificates/preview", formData, { responseType: 'blob' }); const imageUrl = URL.createObjectURL(response.data); setPreviewImage(imageUrl); } catch (error) { console.error("Error fetching preview:", error); if (error.response) { console.error("Response data:", error.response.data); console.error("Response status:", error.response.status); } } finally { setLoadingPreview(false); } }; const handleImageLoad = (e) => { const img = e.target; setPdfSize({ width: img.naturalWidth, height: img.naturalHeight }); }; const handleImageClick = (e) => { if (!imageRef.current) return; const rect = imageRef.current.getBoundingClientRect(); const clickX = e.clientX - rect.left; const clickY = e.clientY - rect.top; const scaleX = pdfSize.width / rect.width; const scaleY = pdfSize.height / rect.height; const pdfX = clickX * scaleX; const pdfY = clickY * scaleY; setDesignParams({ ...designParams, x: Math.round(pdfX), y: Math.round(pdfY) }); }; const handleKeyboardMove = (e) => { if (!designParams.x && !designParams.y) return; const stepX = e.shiftKey ? 60 : 30; const stepY = e.shiftKey ? 40 : 20; let newX = designParams.x || pdfSize.width / 2; let newY = designParams.y || pdfSize.height / 2; switch (e.key) { case 'ArrowLeft': newX -= stepX; break; case 'ArrowRight': newX += stepX; break; case 'ArrowUp': newY -= stepY; break; case 'ArrowDown': newY += stepY; break; default: return; } e.preventDefault(); setDesignParams({ ...designParams, x: Math.round(newX), y: Math.round(newY) }); }; const InputGroup = ({ label, icon: Icon, children }) => (
Fine-tune the appearance of your certificates.
Real-Time Positioning
Loading Preview...
Preview Area
Upload files to see a preview