1+ import { useRef , useState } from 'react' ;
12import { useAccount } from 'wagmi' ;
23import { Button } from '@/components/ui/button' ;
34import type { Course } from '@/services/course.service' ;
45import { cn } from '@/lib/utils' ;
56import { ShoppingCart , Wallet , Link as LinkIcon } from 'lucide-react' ;
67import toast from 'react-hot-toast' ;
78import showToast from '@/utils/toast.util' ;
9+ import TransactionRetryNotice from '@/components/common/TransactionRetryNotice' ;
810
911interface CreatorCardProps {
1012 creator : Course ;
@@ -13,6 +15,32 @@ interface CreatorCardProps {
1315
1416const CreatorCard : React . FC < CreatorCardProps > = ( { creator, className } ) => {
1517 const { isConnected } = useAccount ( ) ;
18+ const [ transactionState , setTransactionState ] = useState <
19+ 'idle' | 'submitting' | 'failed'
20+ > ( 'idle' ) ;
21+ const hasFailedOnceRef = useRef ( false ) ;
22+
23+ const runPurchaseAttempt = ( ) => {
24+ setTransactionState ( 'submitting' ) ;
25+ showToast . loading ( `Purchasing keys for ${ creator . title } ...` ) ;
26+
27+ window . setTimeout ( ( ) => {
28+ toast . remove ( ) ;
29+
30+ if ( ! hasFailedOnceRef . current ) {
31+ hasFailedOnceRef . current = true ;
32+ setTransactionState ( 'failed' ) ;
33+ return ;
34+ }
35+
36+ hasFailedOnceRef . current = false ;
37+ setTransactionState ( 'idle' ) ;
38+ showToast . transactionSuccess (
39+ 'Purchase Successful!' ,
40+ `You successfully bought a key for ${ creator . title } `
41+ ) ;
42+ } , 1500 ) ;
43+ } ;
1644
1745 const handleBuy = ( ) => {
1846 if ( ! isConnected ) {
@@ -23,14 +51,7 @@ const CreatorCard: React.FC<CreatorCardProps> = ({ creator, className }) => {
2351 return ;
2452 }
2553
26- showToast . loading ( `Purchasing keys for ${ creator . title } ...` ) ;
27- // Implementation for contract interaction would go here
28- setTimeout ( ( ) => {
29- showToast . transactionSuccess (
30- 'Purchase Successful!' ,
31- `You successfully bought a key for ${ creator . title } `
32- ) ;
33- } , 1500 ) ;
54+ runPurchaseAttempt ( ) ;
3455 } ;
3556
3657 return (
@@ -82,14 +103,15 @@ const CreatorCard: React.FC<CreatorCardProps> = ({ creator, className }) => {
82103 onClick = { handleBuy }
83104 variant = { isConnected ? 'default' : 'outline' }
84105 size = "sm"
106+ disabled = { transactionState === 'submitting' }
85107 className = { cn (
86108 'rounded-xl font-bold' ,
87109 ! isConnected &&
88110 'border-white/10 text-white/60 hover:bg-white/5'
89111 ) }
90112 >
91113 < ShoppingCart className = "mr-2 size-4" />
92- Buy Key
114+ { transactionState === 'submitting' ? 'Processing...' : ' Buy Key' }
93115 </ Button >
94116 </ div >
95117
@@ -99,6 +121,15 @@ const CreatorCard: React.FC<CreatorCardProps> = ({ creator, className }) => {
99121 Wallet Required
100122 </ div >
101123 ) }
124+
125+ { transactionState === 'failed' && (
126+ < TransactionRetryNotice
127+ className = "mt-4"
128+ message = "The previous purchase attempt failed before confirmation. Retry the Stellar action to try again."
129+ retryLabel = "Retry Purchase"
130+ onRetry = { runPurchaseAttempt }
131+ />
132+ ) }
102133 </ div >
103134 ) ;
104135} ;
0 commit comments