Spaces:
Sleeping
Sleeping
File size: 15,500 Bytes
d19cc77 530365a 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 |
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 }) => (
<div className="bg-white border border-gray-200 p-5 rounded-lg shadow-sm">
<div className="flex items-center space-x-2 mb-4 text-blue-600">
<Icon className="text-lg" />
<span className="font-semibold text-sm uppercase tracking-wide">{label}</span>
</div>
{children}
</div>
);
return (
<div className="flex flex-col lg:flex-row gap-8">
<motion.div
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
className="w-full lg:w-1/3 space-y-6"
>
<div className="mb-6">
<h2 className="text-2xl font-bold text-gray-900">Customize Design</h2>
<p className="text-gray-600 text-sm mt-1">Fine-tune the appearance of your certificates.</p>
</div>
<InputGroup label="Data Mapping" icon={FaFont}>
<div className="space-y-3">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Name Column</label>
<select
className="w-full border border-gray-300 rounded-lg p-2.5 text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none bg-white"
value={mappings.name_column}
onChange={(e) => setMappings({ ...mappings, name_column: e.target.value })}
>
<option value="">Select Column</option>
{fileData.columns.map(col => <option key={col} value={col}>{col}</option>)}
</select>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Email Column</label>
<select
className="w-full border border-gray-300 rounded-lg p-2.5 text-gray-900 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none bg-white"
value={mappings.email_column}
onChange={(e) => setMappings({ ...mappings, email_column: e.target.value })}
>
<option value="">Select Column</option>
{fileData.columns.map(col => <option key={col} value={col}>{col}</option>)}
</select>
</div>
</div>
</InputGroup>
<InputGroup label="Typography & Color" icon={FaPalette}>
<div className="space-y-4">
<div>
<div className="flex justify-between text-xs font-medium text-gray-700 mb-1">
<span>Font Size</span>
<span className="text-blue-600">{designParams.font_size}px</span>
</div>
<input
type="range" min="10" max="200"
value={designParams.font_size}
onChange={(e) => setDesignParams({ ...designParams, font_size: parseInt(e.target.value) })}
className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
/>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Text Color</label>
<div className="flex items-center space-x-3">
<input
type="color"
value={designParams.name_color}
onChange={(e) => setDesignParams({ ...designParams, name_color: e.target.value })}
className="w-10 h-10 rounded-lg cursor-pointer border border-gray-300"
/>
<span className="text-gray-700 font-mono text-sm">{designParams.name_color}</span>
</div>
</div>
</div>
</InputGroup>
<InputGroup label="Positioning" icon={FaArrowsAlt}>
<div className="space-y-3">
<div className="bg-blue-50 border border-blue-200 rounded-lg p-3">
<div className="flex items-start space-x-2">
<FaMousePointer className="text-blue-600 mt-0.5 flex-shrink-0" />
<div className="text-xs text-blue-700">
<p className="font-semibold mb-1">Real-Time Positioning</p>
<ul className="space-y-1">
<li>• Click preview to set position</li>
<li>• Arrow ←→: 30px, ↑↓: 20px (Shift: 2x)</li>
<li>• Drag sliders for smooth control</li>
</ul>
</div>
</div>
</div>
<div className="space-y-3">
<div>
<div className="flex justify-between text-xs font-medium text-gray-700 mb-1">
<label>X Position</label>
<span className="text-blue-600">{designParams.x || 'Auto'}</span>
</div>
<input
type="range"
min="0"
max={pdfSize.width}
value={designParams.x || pdfSize.width / 2}
onChange={(e) => setDesignParams({ ...designParams, x: parseInt(e.target.value) })}
className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
/>
</div>
<div>
<div className="flex justify-between text-xs font-medium text-gray-700 mb-1">
<label>Y Position</label>
<span className="text-blue-600">{designParams.y || 'Auto'}</span>
</div>
<input
type="range"
min="0"
max={pdfSize.height}
value={designParams.y || pdfSize.height / 2}
onChange={(e) => setDesignParams({ ...designParams, y: parseInt(e.target.value) })}
className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
/>
</div>
</div>
<button
onClick={() => setDesignParams({ ...designParams, x: null, y: null })}
className="w-full text-xs text-blue-600 hover:text-blue-700 font-medium mt-2"
>
Reset to Auto Center
</button>
</div>
</InputGroup>
<div className="flex space-x-3 pt-4">
<button
onClick={onBack}
className="flex-1 py-3 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 transition-colors flex items-center justify-center space-x-2 font-medium"
>
<FaChevronLeft className="text-xs" />
<span>Back</span>
</button>
<button
onClick={onNext}
disabled={!mappings.name_column || !mappings.email_column}
className={`flex-1 py-3 rounded-lg text-white font-semibold shadow-md flex items-center justify-center space-x-2 transition-all ${!mappings.name_column || !mappings.email_column
? 'bg-gray-300 cursor-not-allowed text-gray-500'
: 'bg-blue-600 hover:bg-blue-700 hover:shadow-lg'
}`}
>
<span>Next Step</span>
<FaChevronRight className="text-xs" />
</button>
</div>
</motion.div>
<motion.div
initial={{ opacity: 0, scale: 0.98 }}
animate={{ opacity: 1, scale: 1 }}
className="w-full lg:w-2/3 bg-gray-50 rounded-lg border border-gray-200 p-6 flex items-center justify-center min-h-[600px]"
tabIndex={0}
onKeyDown={handleKeyboardMove}
>
{loadingPreview ? (
<div className="flex flex-col items-center space-y-4">
<div className="w-12 h-12 border-4 border-blue-600 border-t-transparent rounded-full animate-spin"></div>
<p className="text-gray-600 font-medium">Loading Preview...</p>
</div>
) : previewImage ? (
<div className="relative">
<img
ref={imageRef}
src={previewImage}
alt="Certificate Preview"
className="max-w-full max-h-full shadow-xl rounded border border-gray-300 cursor-crosshair"
onClick={handleImageClick}
onLoad={handleImageLoad}
/>
{/* Real-time Frontend Text Overlay */}
{designParams.x !== null && designParams.y !== null && imageRef.current && (
<div
className="absolute pointer-events-none flex items-center justify-center"
style={{
left: `${(designParams.x / pdfSize.width) * 100}%`,
top: `${(designParams.y / pdfSize.height) * 100}%`,
transform: 'translate(-50%, -50%)',
fontSize: `${(designParams.font_size / pdfSize.width) * imageRef.current.offsetWidth}px`,
color: designParams.name_color,
fontFamily: 'Helvetica, Arial, sans-serif', // Approximate PDF font
fontWeight: 'bold',
whiteSpace: 'nowrap',
lineHeight: 1,
// No transition for instant responsiveness
}}
>
{previewName}
</div>
)}
{designParams.x !== null && designParams.y !== null && (
<div className="absolute top-2 right-2 bg-black/70 text-white text-xs px-3 py-1 rounded">
Position: {Math.round(designParams.x)}, {Math.round(designParams.y)}
</div>
)}
</div>
) : (
<div className="text-gray-400 text-center">
<p className="text-lg font-medium">Preview Area</p>
<p className="text-sm">Upload files to see a preview</p>
</div>
)}
</motion.div>
</div>
);
};
export default DesignStep;
|