forked from Gatheraa/Gatherraa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionRetryHandler.tsx
More file actions
63 lines (53 loc) · 1.56 KB
/
Copy pathTransactionRetryHandler.tsx
File metadata and controls
63 lines (53 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use client';
import React, { useState } from 'react';
interface TransactionRetryHandlerProps {
transactionStatus: 'pending' | 'failed' | 'success';
errorMessage?: string;
onRetry: () => Promise<void>;
}
const TransactionRetryHandler: React.FC<TransactionRetryHandlerProps> = ({
transactionStatus,
errorMessage,
onRetry,
}) => {
const [retryCount, setRetryCount] = useState(0);
const [isRetrying, setIsRetrying] = useState(false);
const maxRetries = 3;
const handleRetry = async () => {
setIsRetrying(true);
try {
await onRetry();
setRetryCount(prevCount => prevCount + 1);
setIsRetrying(false);
} catch (error: any) {
setIsRetrying(false);
console.error('Retry failed:', error);
}
};
if (transactionStatus === 'success') {
return <p>Transaction successful!</p>;
}
if (transactionStatus === 'pending') {
return <p>Transaction is still pending...</p>;
}
if (retryCount >= maxRetries) {
return <p>Maximum retries reached. Please contact support.</p>;
}
return (
<div>
<p>Transaction failed: {errorMessage || 'Unknown error'}</p>
<button
onClick={handleRetry}
disabled={isRetrying}
className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded"
>
{isRetrying ? 'Retrying...' : 'Retry Transaction'}
</button>
</div>
);
};
export default TransactionRetryHandler;
/*
Example Usage:
<TransactionRetryHandler transactionStatus="failed" errorMessage="Insufficient funds" onRetry={() => console.log('Retrying...')} />
*/