'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'; export default function EditQRPage() { const router = useRouter(); const params = useParams(); const qrId = params.id as string; const [loading, setLoading] = useState(true); const [saving, setSaving] = 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 handleSave = async () => { setSaving(true); try { const response = await fetch(`/api/qrs/${qrId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', }, 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' && (