'use client'; import React, { useState, useEffect } from 'react'; import { useRouter, useParams } from 'next/navigation'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/Card'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { showToast } from '@/components/ui/Toast'; import { useCsrf } from '@/hooks/useCsrf'; import { Upload, FileText, HelpCircle } from 'lucide-react'; // Tooltip component for form field help const Tooltip = ({ text }: { text: string }) => (
{text}
); export default function EditQRPage() { const router = useRouter(); const params = useParams(); const qrId = params.id as string; const { fetchWithCsrf, loading: csrfLoading } = useCsrf(); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [uploading, setUploading] = useState(false); const [qrCode, setQrCode] = useState(null); const [title, setTitle] = useState(''); const [content, setContent] = useState({}); useEffect(() => { const fetchQRCode = async () => { try { const response = await fetch(`/api/qrs/${qrId}`); if (response.ok) { const data = await response.json(); setQrCode(data); setTitle(data.title); setContent(data.content || {}); } else { showToast('Failed to load QR code', 'error'); router.push('/dashboard'); } } catch (error) { console.error('Error fetching QR code:', error); showToast('Failed to load QR code', 'error'); router.push('/dashboard'); } finally { setLoading(false); } }; fetchQRCode(); }, [qrId, router]); const handleFileUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; // 10MB limit if (file.size > 10 * 1024 * 1024) { showToast('File size too large (max 10MB)', 'error'); return; } setUploading(true); const formData = new FormData(); formData.append('file', file); try { const response = await fetch('/api/upload', { method: 'POST', body: formData, }); const data = await response.json(); if (response.ok) { setContent({ ...content, fileUrl: data.url, fileName: data.filename }); showToast('File uploaded successfully!', 'success'); } else { showToast(data.error || 'Upload failed', 'error'); } } catch (error) { console.error('Upload error:', error); showToast('Error uploading file', 'error'); } finally { setUploading(false); } }; const handleSave = async () => { setSaving(true); try { const response = await fetchWithCsrf(`/api/qrs/${qrId}`, { method: 'PATCH', body: JSON.stringify({ title, content, }), }); if (response.ok) { showToast('QR code updated successfully!', 'success'); router.push('/dashboard'); } else { const error = await response.json(); showToast(error.error || 'Failed to update QR code', 'error'); } } catch (error) { console.error('Error updating QR code:', error); showToast('Failed to update QR code', 'error'); } finally { setSaving(false); } }; if (loading) { return (

Loading QR code...

); } if (!qrCode) { return null; } // Static QR codes cannot be edited if (qrCode.type === 'STATIC') { return (

Static QR Code

Static QR codes cannot be edited because their content is embedded directly in the QR code image.

); } return (

Edit QR Code

Update your dynamic QR code content

QR Code Details setTitle(e.target.value)} placeholder="Enter QR code title" required /> {qrCode.contentType === 'URL' && ( setContent({ ...content, url: e.target.value })} placeholder="https://example.com" required /> )} {qrCode.contentType === 'PHONE' && ( setContent({ ...content, phone: e.target.value })} placeholder="+1234567890" required /> )} {qrCode.contentType === 'VCARD' && ( <> setContent({ ...content, firstName: e.target.value })} placeholder="John" required /> setContent({ ...content, lastName: e.target.value })} placeholder="Doe" required /> setContent({ ...content, email: e.target.value })} placeholder="john@example.com" /> setContent({ ...content, phone: e.target.value })} placeholder="+1234567890" /> setContent({ ...content, organization: e.target.value })} placeholder="Company Name" /> setContent({ ...content, title: e.target.value })} placeholder="CEO" /> )} {qrCode.contentType === 'GEO' && ( <> setContent({ ...content, latitude: parseFloat(e.target.value) || 0 })} placeholder="37.7749" required /> setContent({ ...content, longitude: parseFloat(e.target.value) || 0 })} placeholder="-122.4194" required /> setContent({ ...content, label: e.target.value })} placeholder="Golden Gate Bridge" /> )} {qrCode.contentType === 'TEXT' && (