From 3120f47b50442f25f9f150db4605d7caba5dcab4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 26 Oct 2025 23:43:02 -0500 Subject: [PATCH] Add printable QR codes to gift cards --- src/components/CreditsDialog.tsx | 189 ++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 2 deletions(-) diff --git a/src/components/CreditsDialog.tsx b/src/components/CreditsDialog.tsx index 1d6bb6b4..efdd7536 100644 --- a/src/components/CreditsDialog.tsx +++ b/src/components/CreditsDialog.tsx @@ -1,7 +1,7 @@ import { useState, useEffect } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import QRCode from 'qrcode'; -import { CreditCard, Zap, ExternalLink, RefreshCw, Check, X, Clock, AlertCircle, Copy, RotateCcw, ArrowLeft, Gift, Plus, History, DollarSign } from 'lucide-react'; +import { CreditCard, Zap, ExternalLink, RefreshCw, Check, X, Clock, AlertCircle, Copy, RotateCcw, ArrowLeft, Gift, Plus, History, DollarSign, Printer } from 'lucide-react'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; @@ -225,6 +225,7 @@ export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogPro const [giftcardQuantity, setGiftcardQuantity] = useState(1); const [redeemCode, setRedeemCode] = useState(''); const [copiedCode, setCopiedCode] = useState(null); + const [isPrintingQRCodes, setIsPrintingQRCodes] = useState(false); // Query for payment history const { data: payments, isLoading: isLoadingPayments } = useQuery({ @@ -500,6 +501,174 @@ export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogPro setTimeout(() => setCopiedCode(null), 2000); }; + const handlePrintGiftcards = async () => { + if (!giftcards || giftcards.length === 0) return; + + // Filter only active (non-redeemed) gift cards + const activeGiftcards = giftcards.filter(gc => !gc.redeemed); + if (activeGiftcards.length === 0) { + toast({ + title: 'No active gift cards', + description: 'You have no active gift cards to print.', + variant: 'destructive', + }); + return; + } + + setIsPrintingQRCodes(true); + + try { + // Generate QR codes for all active gift cards + const qrCodePromises = activeGiftcards.map(async (giftcard) => { + const url = `${window.location.origin}/giftcard#baseURL=${encodeURIComponent(provider.baseURL)}&code=${encodeURIComponent(giftcard.code)}`; + const qrDataUrl = await QRCode.toDataURL(url, { + width: 256, + margin: 2, + color: { + dark: '#000000', + light: '#ffffff', + }, + }); + return { + ...giftcard, + qrDataUrl, + }; + }); + + const giftcardsWithQR = await Promise.all(qrCodePromises); + + // Create a new window for printing + const printWindow = window.open('', '_blank'); + if (!printWindow) { + throw new Error('Failed to open print window. Please allow popups for this site.'); + } + + // Generate the print HTML + const printHTML = ` + + + + Shakespeare AI Gift Cards + + + +
+ ${giftcardsWithQR.map(gc => ` +
+
Shakespeare AI Gift Card
+ QR Code +
${formatCurrency(gc.amount)}
+
${gc.code}
+ +
+ `).join('')} +
+ + + `; + + printWindow.document.write(printHTML); + printWindow.document.close(); + + // Wait for images to load before printing + printWindow.onload = () => { + setTimeout(() => { + printWindow.print(); + }, 250); + }; + } catch (error) { + console.error('Failed to generate QR codes:', error); + toast({ + title: 'Print failed', + description: error instanceof Error ? error.message : 'Failed to generate QR codes for printing', + variant: 'destructive', + }); + } finally { + setIsPrintingQRCodes(false); + } + }; + const formatCurrency = (amount: number) => { return new Intl.NumberFormat('en-US', { style: 'currency', @@ -864,7 +1033,23 @@ export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogPro -
+
+ {giftcards && giftcards.filter(gc => !gc.redeemed).length > 0 && ( + + )} {isLoadingGiftcards ? (