mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add gift card support
This commit is contained in:
1 file changed
+501
-145
+501
-145
@@ -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 } from 'lucide-react';
|
||||
import { CreditCard, Zap, ExternalLink, RefreshCw, Check, X, Clock, AlertCircle, Copy, RotateCcw, ArrowLeft, Gift } from 'lucide-react';
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
@@ -9,7 +9,7 @@ import { Label } from '@/components/ui/label';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { createAIClient } from '@/lib/ai-client';
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
@@ -50,6 +50,35 @@ interface AddCreditsRequest {
|
||||
redirect_url?: string;
|
||||
}
|
||||
|
||||
interface Giftcard {
|
||||
object: 'giftcard';
|
||||
id: string;
|
||||
code: string;
|
||||
amount: number;
|
||||
redeemed: boolean;
|
||||
created_at?: number;
|
||||
}
|
||||
|
||||
interface GiftcardsResponse {
|
||||
object: 'list';
|
||||
data: Giftcard[];
|
||||
has_more: boolean;
|
||||
}
|
||||
|
||||
interface CreateGiftcardRequest {
|
||||
amount: number;
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
interface RedeemGiftcardRequest {
|
||||
code: string;
|
||||
}
|
||||
|
||||
interface GiftcardRedemption {
|
||||
object: 'giftcard_redemption';
|
||||
amount: number;
|
||||
}
|
||||
|
||||
const PRESET_AMOUNTS = [5, 10, 25, 50, 100];
|
||||
|
||||
interface LightningPaymentProps {
|
||||
@@ -192,6 +221,11 @@ export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogPro
|
||||
const [paymentMethod, setPaymentMethod] = useState<'stripe' | 'lightning'>('stripe');
|
||||
const [lightningInvoice, setLightningInvoice] = useState<string | null>(null);
|
||||
const [refreshingPayments, setRefreshingPayments] = useState<Set<string>>(new Set());
|
||||
const [activeTab, setActiveTab] = useState<'buy' | 'giftcards'>('buy');
|
||||
const [giftcardAmount, setGiftcardAmount] = useState<number>(10);
|
||||
const [giftcardQuantity, setGiftcardQuantity] = useState<number>(1);
|
||||
const [redeemCode, setRedeemCode] = useState<string>('');
|
||||
const [copiedCode, setCopiedCode] = useState<string | null>(null);
|
||||
|
||||
// Query for payment history
|
||||
const { data: payments, isLoading: isLoadingPayments } = useQuery({
|
||||
@@ -206,6 +240,19 @@ export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogPro
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
|
||||
// Query for gift cards
|
||||
const { data: giftcards, isLoading: isLoadingGiftcards } = useQuery({
|
||||
queryKey: ['ai-giftcards', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
|
||||
queryFn: async (): Promise<Giftcard[]> => {
|
||||
const ai = createAIClient(provider, user, config.corsProxy);
|
||||
const data = await ai.get('/credits/giftcards?limit=50') as GiftcardsResponse;
|
||||
return data.data;
|
||||
},
|
||||
enabled: open && activeTab === 'giftcards',
|
||||
retry: false,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
|
||||
// Mutation for adding credits
|
||||
const addCreditsMutation = useMutation({
|
||||
mutationFn: async (request: AddCreditsRequest): Promise<Payment> => {
|
||||
@@ -316,6 +363,77 @@ export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogPro
|
||||
},
|
||||
});
|
||||
|
||||
// Mutation for creating gift cards
|
||||
const createGiftcardMutation = useMutation({
|
||||
mutationFn: async (request: CreateGiftcardRequest): Promise<Giftcard[]> => {
|
||||
const ai = createAIClient(provider, user, config.corsProxy);
|
||||
const response = await ai.post('/credits/giftcards', {
|
||||
body: request,
|
||||
}) as GiftcardsResponse;
|
||||
return response.data;
|
||||
},
|
||||
onSuccess: (giftcards) => {
|
||||
toast({
|
||||
title: 'Gift Cards created!',
|
||||
description: `Successfully created ${giftcards.length} gift card${giftcards.length > 1 ? 's' : ''}.`,
|
||||
});
|
||||
|
||||
// Refresh gift cards list
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['ai-giftcards', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
|
||||
});
|
||||
|
||||
// Refresh credits balance
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['ai-credits', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
|
||||
});
|
||||
|
||||
// Reset form
|
||||
setGiftcardAmount(10);
|
||||
setGiftcardQuantity(1);
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
const errorMessage = error?.message || 'Failed to create gift cards';
|
||||
toast({
|
||||
title: 'Creation failed',
|
||||
description: errorMessage,
|
||||
variant: 'destructive',
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
// Mutation for redeeming gift cards
|
||||
const redeemGiftcardMutation = useMutation({
|
||||
mutationFn: async (request: RedeemGiftcardRequest): Promise<GiftcardRedemption> => {
|
||||
const ai = createAIClient(provider, user, config.corsProxy);
|
||||
return await ai.post('/credits/giftcards/redeem', {
|
||||
body: request,
|
||||
}) as GiftcardRedemption;
|
||||
},
|
||||
onSuccess: (redemption) => {
|
||||
toast({
|
||||
title: 'Gift Card redeemed!',
|
||||
description: `${formatCurrency(redemption.amount)} has been added to your account.`,
|
||||
});
|
||||
|
||||
// Refresh credits balance
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['ai-credits', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
|
||||
});
|
||||
|
||||
// Reset redeem code
|
||||
setRedeemCode('');
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
const errorMessage = error?.message || 'Failed to redeem gift card';
|
||||
toast({
|
||||
title: 'Redemption failed',
|
||||
description: errorMessage,
|
||||
variant: 'destructive',
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const handleAddCredits = () => {
|
||||
if (amount <= 0) {
|
||||
toast({
|
||||
@@ -338,6 +456,51 @@ export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogPro
|
||||
addCreditsMutation.mutate(request);
|
||||
};
|
||||
|
||||
const handleCreateGiftcard = () => {
|
||||
if (giftcardAmount <= 0) {
|
||||
toast({
|
||||
title: 'Invalid amount',
|
||||
description: 'Please enter a valid amount.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (giftcardQuantity <= 0 || giftcardQuantity > 100) {
|
||||
toast({
|
||||
title: 'Invalid quantity',
|
||||
description: 'Quantity must be between 1 and 100.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
createGiftcardMutation.mutate({
|
||||
amount: giftcardAmount,
|
||||
quantity: giftcardQuantity,
|
||||
});
|
||||
};
|
||||
|
||||
const handleRedeemGiftcard = () => {
|
||||
const trimmedCode = redeemCode.trim();
|
||||
if (!trimmedCode) {
|
||||
toast({
|
||||
title: 'Invalid code',
|
||||
description: 'Please enter a gift card code.',
|
||||
variant: 'destructive',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
redeemGiftcardMutation.mutate({ code: trimmedCode });
|
||||
};
|
||||
|
||||
const handleCopyCode = (code: string) => {
|
||||
navigator.clipboard.writeText(code);
|
||||
setCopiedCode(code);
|
||||
setTimeout(() => setCopiedCode(null), 2000);
|
||||
};
|
||||
|
||||
const formatCurrency = (amount: number) => {
|
||||
return new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
@@ -434,159 +597,352 @@ export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogPro
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex-1 flex flex-col min-h-0">
|
||||
{/* Add Credits Form - Fixed size section */}
|
||||
<div className="flex-shrink-0 space-y-4 px-1 -mx-1">
|
||||
<div className="space-y-3">
|
||||
<Label htmlFor="amount" className="text-sm font-medium">Amount (USD)</Label>
|
||||
<div className="space-y-3">
|
||||
<Input
|
||||
id="amount"
|
||||
type="number"
|
||||
step="0.01"
|
||||
value={amount}
|
||||
onChange={(e) => setAmount(Number(e.target.value))}
|
||||
placeholder="Enter amount"
|
||||
className="text-center text-lg font-medium"
|
||||
/>
|
||||
<div className="grid grid-cols-5 gap-2">
|
||||
{PRESET_AMOUNTS.map((preset) => (
|
||||
<Button
|
||||
key={preset}
|
||||
variant={amount === preset ? "default" : "outline"}
|
||||
size="sm"
|
||||
onClick={() => setAmount(preset)}
|
||||
className="text-xs"
|
||||
>
|
||||
${preset}
|
||||
</Button>
|
||||
))}
|
||||
{/* Main Tabs */}
|
||||
<Tabs value={activeTab} onValueChange={(value: 'buy' | 'giftcards') => setActiveTab(value)} className="flex-1 flex flex-col min-h-0">
|
||||
<TabsList className="grid w-full grid-cols-2 flex-shrink-0 mb-4">
|
||||
<TabsTrigger value="buy" className="flex items-center gap-2">
|
||||
<CreditCard className="h-4 w-4" />
|
||||
Buy Credits
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="giftcards" className="flex items-center gap-2">
|
||||
<Gift className="h-4 w-4" />
|
||||
Gift Cards
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
{/* Buy Credits Tab */}
|
||||
<TabsContent value="buy" className="flex-1 flex flex-col min-h-0 mt-0">
|
||||
{/* Add Credits Form - Fixed size section */}
|
||||
<div className="flex-shrink-0 space-y-4 px-1 -mx-1">
|
||||
<div className="space-y-3">
|
||||
<Label htmlFor="amount" className="text-sm font-medium">Amount (USD)</Label>
|
||||
<div className="space-y-3">
|
||||
<Input
|
||||
id="amount"
|
||||
type="number"
|
||||
step="0.01"
|
||||
value={amount}
|
||||
onChange={(e) => setAmount(Number(e.target.value))}
|
||||
placeholder="Enter amount"
|
||||
className="text-center text-lg font-medium"
|
||||
/>
|
||||
<div className="grid grid-cols-5 gap-2">
|
||||
{PRESET_AMOUNTS.map((preset) => (
|
||||
<Button
|
||||
key={preset}
|
||||
variant={amount === preset ? "default" : "outline"}
|
||||
size="sm"
|
||||
onClick={() => setAmount(preset)}
|
||||
className="text-xs"
|
||||
>
|
||||
${preset}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
<Label className="text-sm font-medium">Payment Method</Label>
|
||||
<Tabs value={paymentMethod} onValueChange={(value: 'stripe' | 'lightning') => setPaymentMethod(value)} className="w-full">
|
||||
<TabsList className="grid w-full grid-cols-2">
|
||||
<TabsTrigger value="stripe" className="flex items-center gap-2">
|
||||
<CreditCard className="h-4 w-4" />
|
||||
Credit Card
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="lightning" className="flex items-center gap-2">
|
||||
<Zap className="h-4 w-4" />
|
||||
Lightning
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
</Tabs>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={handleAddCredits}
|
||||
disabled={amount <= 0 || addCreditsMutation.isPending}
|
||||
className="w-full h-12 text-base font-medium"
|
||||
size="lg"
|
||||
>
|
||||
{addCreditsMutation.isPending && <RefreshCw className="h-4 w-4 mr-2 animate-spin" />}
|
||||
{paymentMethod === 'stripe' ? 'Pay with Card' : 'Generate Invoice'}
|
||||
{amount > 0 && ` - ${formatCurrency(amount)}`}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
<Label className="text-sm font-medium">Payment Method</Label>
|
||||
<Tabs value={paymentMethod} onValueChange={(value: 'stripe' | 'lightning') => setPaymentMethod(value)} className="w-full">
|
||||
<TabsList className="grid w-full grid-cols-2">
|
||||
<TabsTrigger value="stripe" className="flex items-center gap-2">
|
||||
<CreditCard className="h-4 w-4" />
|
||||
Credit Card
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="lightning" className="flex items-center gap-2">
|
||||
<Zap className="h-4 w-4" />
|
||||
Lightning
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
</Tabs>
|
||||
</div>
|
||||
<div className="flex-shrink-0 px-1 -mx-1 py-4">
|
||||
<Separator />
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={handleAddCredits}
|
||||
disabled={amount <= 0 || addCreditsMutation.isPending}
|
||||
className="w-full h-12 text-base font-medium"
|
||||
size="lg"
|
||||
>
|
||||
{addCreditsMutation.isPending && <RefreshCw className="h-4 w-4 mr-2 animate-spin" />}
|
||||
{paymentMethod === 'stripe' ? 'Pay with Card' : 'Generate Invoice'}
|
||||
{amount > 0 && ` - ${formatCurrency(amount)}`}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex-shrink-0 px-1 -mx-1 py-4">
|
||||
<Separator />
|
||||
</div>
|
||||
|
||||
{/* Transaction History - Flexible section */}
|
||||
<div className="flex-1 min-h-0 flex flex-col px-1 -mx-1">
|
||||
<div className="flex-shrink-0 mb-4">
|
||||
<h3 className="text-base font-semibold">Recent Transactions</h3>
|
||||
</div>
|
||||
<div className="flex-1 min-h-0 overflow-y-auto">
|
||||
{isLoadingPayments ? (
|
||||
<div className="p-3 border rounded-lg space-y-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Skeleton className="h-4 w-4 rounded" />
|
||||
<Skeleton className="h-4 w-24" />
|
||||
<Skeleton className="h-4 w-16 ml-auto" />
|
||||
</div>
|
||||
<Skeleton className="h-3 w-32" />
|
||||
{/* Transaction History - Flexible section */}
|
||||
<div className="flex-1 min-h-0 flex flex-col px-1 -mx-1">
|
||||
<div className="flex-shrink-0 mb-4">
|
||||
<h3 className="text-base font-semibold">Recent Transactions</h3>
|
||||
</div>
|
||||
) : !payments || payments.length === 0 ? (
|
||||
<div className="flex-1 flex items-center justify-center">
|
||||
<div className="text-center text-muted-foreground">
|
||||
<CreditCard className="h-10 w-10 mx-auto mb-3 opacity-50" />
|
||||
<p className="text-sm">No transactions yet</p>
|
||||
<p className="text-xs">Your payment history will appear here</p>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-2 pb-2">
|
||||
{payments.map((payment) => (
|
||||
<div
|
||||
key={payment.id}
|
||||
className="p-3 border rounded-lg space-y-2 hover:bg-muted/30 transition-colors"
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2 min-w-0 flex-1">
|
||||
{getStatusIcon(payment.status)}
|
||||
<span className="text-sm font-medium truncate">
|
||||
{payment.method === 'stripe' ? 'Credit Card' : 'Lightning'}
|
||||
</span>
|
||||
<Badge variant={getStatusVariant(payment.status)} className="text-xs flex-shrink-0">
|
||||
{payment.status}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="text-right flex-shrink-0">
|
||||
<p className="text-sm font-medium">{formatCurrency(payment.amount)}</p>
|
||||
{payment.fee > 0 && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
+{formatCurrency(payment.fee)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex-1 min-h-0 overflow-y-auto">
|
||||
{isLoadingPayments ? (
|
||||
<div className="p-3 border rounded-lg space-y-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Skeleton className="h-4 w-4 rounded" />
|
||||
<Skeleton className="h-4 w-24" />
|
||||
<Skeleton className="h-4 w-16 ml-auto" />
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between text-xs text-muted-foreground">
|
||||
<span>{formatDate(payment.created_at)}</span>
|
||||
{payment.status === 'pending' && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => checkPaymentMutation.mutate(payment.id)}
|
||||
disabled={refreshingPayments.has(payment.id)}
|
||||
className="h-6 px-2 text-xs"
|
||||
title="Refresh payment status"
|
||||
>
|
||||
<RotateCcw className={`h-3 w-3 ${refreshingPayments.has(payment.id) ? 'animate-spin' : ''}`} />
|
||||
</Button>
|
||||
{payment.url && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
if (payment.method === 'lightning') {
|
||||
setLightningInvoice(payment.url);
|
||||
} else {
|
||||
window.open(payment.url, '_blank');
|
||||
}
|
||||
}}
|
||||
className="h-6 px-2 text-xs"
|
||||
>
|
||||
<ExternalLink className="h-3 w-3 mr-1" />
|
||||
{payment.method === 'lightning' ? 'Pay' : 'Pay'}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<Skeleton className="h-3 w-32" />
|
||||
</div>
|
||||
) : !payments || payments.length === 0 ? (
|
||||
<div className="flex-1 flex items-center justify-center">
|
||||
<div className="text-center text-muted-foreground">
|
||||
<CreditCard className="h-10 w-10 mx-auto mb-3 opacity-50" />
|
||||
<p className="text-sm">No transactions yet</p>
|
||||
<p className="text-xs">Your payment history will appear here</p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
) : (
|
||||
<div className="space-y-2 pb-2">
|
||||
{payments.map((payment) => (
|
||||
<div
|
||||
key={payment.id}
|
||||
className="p-3 border rounded-lg space-y-2 hover:bg-muted/30 transition-colors"
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2 min-w-0 flex-1">
|
||||
{getStatusIcon(payment.status)}
|
||||
<span className="text-sm font-medium truncate">
|
||||
{payment.method === 'stripe' ? 'Credit Card' : 'Lightning'}
|
||||
</span>
|
||||
<Badge variant={getStatusVariant(payment.status)} className="text-xs flex-shrink-0">
|
||||
{payment.status}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="text-right flex-shrink-0">
|
||||
<p className="text-sm font-medium">{formatCurrency(payment.amount)}</p>
|
||||
{payment.fee > 0 && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
+{formatCurrency(payment.fee)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between text-xs text-muted-foreground">
|
||||
<span>{formatDate(payment.created_at)}</span>
|
||||
{payment.status === 'pending' && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => checkPaymentMutation.mutate(payment.id)}
|
||||
disabled={refreshingPayments.has(payment.id)}
|
||||
className="h-6 px-2 text-xs"
|
||||
title="Refresh payment status"
|
||||
>
|
||||
<RotateCcw className={`h-3 w-3 ${refreshingPayments.has(payment.id) ? 'animate-spin' : ''}`} />
|
||||
</Button>
|
||||
{payment.url && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
if (payment.method === 'lightning') {
|
||||
setLightningInvoice(payment.url);
|
||||
} else {
|
||||
window.open(payment.url, '_blank');
|
||||
}
|
||||
}}
|
||||
className="h-6 px-2 text-xs"
|
||||
>
|
||||
<ExternalLink className="h-3 w-3 mr-1" />
|
||||
{payment.method === 'lightning' ? 'Pay' : 'Pay'}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</TabsContent>
|
||||
|
||||
{/* Gift Cards Tab */}
|
||||
<TabsContent value="giftcards" className="flex-1 flex flex-col min-h-0 mt-0">
|
||||
<div className="flex-1 flex flex-col min-h-0">
|
||||
{/* Redeem Gift Card Section */}
|
||||
<div className="flex-shrink-0 space-y-4 px-1 -mx-1 mb-4">
|
||||
<div className="space-y-3">
|
||||
<Label htmlFor="redeem-code" className="text-sm font-medium">Redeem Gift Card</Label>
|
||||
<div className="flex gap-2">
|
||||
<Input
|
||||
id="redeem-code"
|
||||
type="text"
|
||||
value={redeemCode}
|
||||
onChange={(e) => setRedeemCode(e.target.value)}
|
||||
placeholder="Enter gift card code"
|
||||
className="flex-1"
|
||||
/>
|
||||
<Button
|
||||
onClick={handleRedeemGiftcard}
|
||||
disabled={!redeemCode.trim() || redeemGiftcardMutation.isPending}
|
||||
size="lg"
|
||||
>
|
||||
{redeemGiftcardMutation.isPending && <RefreshCw className="h-4 w-4 mr-2 animate-spin" />}
|
||||
Redeem
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex-shrink-0 px-1 -mx-1 pb-4">
|
||||
<Separator />
|
||||
</div>
|
||||
|
||||
{/* Create Gift Card Section */}
|
||||
<div className="flex-shrink-0 space-y-4 px-1 -mx-1">
|
||||
<div className="space-y-3">
|
||||
<Label htmlFor="giftcard-amount" className="text-sm font-medium">Create Gift Card</Label>
|
||||
<div className="space-y-3">
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="giftcard-amount" className="text-xs text-muted-foreground">Amount (USD)</Label>
|
||||
<Input
|
||||
id="giftcard-amount"
|
||||
type="number"
|
||||
step="0.01"
|
||||
value={giftcardAmount}
|
||||
onChange={(e) => setGiftcardAmount(Number(e.target.value))}
|
||||
placeholder="Amount"
|
||||
className="text-center"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="giftcard-quantity" className="text-xs text-muted-foreground">Quantity</Label>
|
||||
<Input
|
||||
id="giftcard-quantity"
|
||||
type="number"
|
||||
min="1"
|
||||
max="100"
|
||||
value={giftcardQuantity}
|
||||
onChange={(e) => setGiftcardQuantity(Number(e.target.value))}
|
||||
placeholder="Qty"
|
||||
className="text-center"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="grid grid-cols-5 gap-2">
|
||||
{PRESET_AMOUNTS.map((preset) => (
|
||||
<Button
|
||||
key={preset}
|
||||
variant={giftcardAmount === preset ? "default" : "outline"}
|
||||
size="sm"
|
||||
onClick={() => setGiftcardAmount(preset)}
|
||||
className="text-xs"
|
||||
>
|
||||
${preset}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={handleCreateGiftcard}
|
||||
disabled={giftcardAmount <= 0 || giftcardQuantity <= 0 || createGiftcardMutation.isPending}
|
||||
className="w-full h-12 text-base font-medium"
|
||||
size="lg"
|
||||
>
|
||||
{createGiftcardMutation.isPending && <RefreshCw className="h-4 w-4 mr-2 animate-spin" />}
|
||||
Create {giftcardQuantity > 1 ? `${giftcardQuantity} ` : ''}Gift Card{giftcardQuantity > 1 ? 's' : ''}
|
||||
{giftcardAmount > 0 && giftcardQuantity > 0 && ` - ${formatCurrency(giftcardAmount * giftcardQuantity)}`}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="flex-shrink-0 px-1 -mx-1 py-4">
|
||||
<Separator />
|
||||
</div>
|
||||
|
||||
{/* Gift Cards List */}
|
||||
<div className="flex-1 min-h-0 flex flex-col px-1 -mx-1">
|
||||
<div className="flex-shrink-0 mb-4">
|
||||
<h3 className="text-base font-semibold">Your Gift Cards</h3>
|
||||
</div>
|
||||
<div className="flex-1 min-h-0 overflow-y-auto">
|
||||
{isLoadingGiftcards ? (
|
||||
<div className="p-3 border rounded-lg space-y-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Skeleton className="h-4 w-4 rounded" />
|
||||
<Skeleton className="h-4 w-32" />
|
||||
<Skeleton className="h-4 w-16 ml-auto" />
|
||||
</div>
|
||||
<Skeleton className="h-3 w-48" />
|
||||
</div>
|
||||
) : !giftcards || giftcards.length === 0 ? (
|
||||
<div className="flex-1 flex items-center justify-center">
|
||||
<div className="text-center text-muted-foreground">
|
||||
<Gift className="h-10 w-10 mx-auto mb-3 opacity-50" />
|
||||
<p className="text-sm">No gift cards yet</p>
|
||||
<p className="text-xs">Create gift cards to share with others</p>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-2 pb-2">
|
||||
{giftcards.map((giftcard) => (
|
||||
<div
|
||||
key={giftcard.id}
|
||||
className="p-3 border rounded-lg space-y-2 hover:bg-muted/30 transition-colors"
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2 min-w-0 flex-1">
|
||||
<Gift className="h-4 w-4 flex-shrink-0" />
|
||||
<span className="text-sm font-medium truncate">
|
||||
Gift Card
|
||||
</span>
|
||||
<Badge variant={giftcard.redeemed ? "secondary" : "default"} className="text-xs flex-shrink-0">
|
||||
{giftcard.redeemed ? 'Redeemed' : 'Active'}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="text-right flex-shrink-0">
|
||||
<p className="text-sm font-medium">{formatCurrency(giftcard.amount)}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<code className="text-xs bg-muted px-2 py-1 rounded font-mono">
|
||||
{giftcard.code}
|
||||
</code>
|
||||
<div className="relative">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => handleCopyCode(giftcard.code)}
|
||||
className="h-6 px-2 text-xs"
|
||||
>
|
||||
<Copy className="h-3 w-3 mr-1" />
|
||||
Copy
|
||||
</Button>
|
||||
{copiedCode === giftcard.code && (
|
||||
<div className="absolute -top-8 right-0 bg-foreground text-background text-xs px-2 py-1 rounded shadow-lg z-10 animate-in fade-in-0 duration-200 whitespace-nowrap">
|
||||
Copied!
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{giftcard.created_at && (
|
||||
<div className="text-xs text-muted-foreground">
|
||||
Created {formatDate(giftcard.created_at)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
)}
|
||||
</DialogContent>
|
||||
|
||||
Reference in new issue
Block a user