@@ -26,14 +26,20 @@ const RaiseDisputeButton: React.FC<IRaiseDisputeButton> = ({ toggleModal, button
26
26
const isBuyer = useMemo ( ( ) => address ?. toLowerCase ( ) === buyer ?. toLowerCase ( ) , [ address , buyer ] ) ;
27
27
const refetchQuery = useQueryRefetch ( ) ;
28
28
29
- const { data : payArbitrationFeeByBuyerConfig , error : simulateBuyerError } =
29
+ const { data : payArbitrationFeeByBuyerConfig , isLoading : isPreparingBuyerConfig } =
30
30
useSimulateEscrowUniversalPayArbitrationFeeByBuyer ( {
31
+ query : {
32
+ enabled : isBuyer ,
33
+ } ,
31
34
args : [ BigInt ( id ) ] ,
32
35
value : arbitrationCost ,
33
36
} ) ;
34
37
35
- const { data : payArbitrationFeeBySellerConfig , error : simulateSellerError } =
38
+ const { data : payArbitrationFeeBySellerConfig , isLoading : isPreparingSellerConfig } =
36
39
useSimulateEscrowUniversalPayArbitrationFeeBySeller ( {
40
+ query : {
41
+ enabled : ! isBuyer ,
42
+ } ,
37
43
args : [ BigInt ( id ) ] ,
38
44
value : arbitrationCost ,
39
45
} ) ;
@@ -45,76 +51,47 @@ const RaiseDisputeButton: React.FC<IRaiseDisputeButton> = ({ toggleModal, button
45
51
) ;
46
52
47
53
const handleRaiseDispute = ( ) => {
48
- if ( isBuyer ) {
49
- if ( simulateBuyerError ) {
50
- console . log ( { simulateBuyerError} ) ;
51
- let errorMessage = "An error occurred during simulation." ;
52
- if ( simulateBuyerError . message . includes ( "insufficient funds" ) ) {
53
- errorMessage = "You don't have enough balance to raise a dispute." ;
54
- }
55
- // Use wrapWithToast to display the error
56
- wrapWithToast ( ( ) => Promise . reject ( new Error ( errorMessage ) ) , publicClient ) ;
57
- return ;
58
- }
59
-
60
- if ( isUndefined ( payArbitrationFeeByBuyerConfig ) ) {
61
- console . log ( { payArbitrationFeeByBuyerConfig } ) ;
62
- console . log ( { simulateBuyerError } ) ;
63
- wrapWithToast ( ( ) => Promise . reject ( new Error ( "Unable to prepare transaction." ) ) , publicClient ) ;
64
- return ;
65
- }
66
-
67
- if ( ! isUndefined ( payArbitrationFeeByBuyer ) ) {
68
- setIsSending ( true ) ;
69
- wrapWithToast ( ( ) => payArbitrationFeeByBuyer ( payArbitrationFeeByBuyerConfig . request ) , publicClient )
70
- . then ( ( wrapResult ) => {
54
+ if ( isBuyer && ! isUndefined ( payArbitrationFeeByBuyer ) ) {
55
+ setIsSending ( true ) ;
56
+ wrapWithToast ( async ( ) => await payArbitrationFeeByBuyer ( payArbitrationFeeByBuyerConfig . request ) , publicClient )
57
+ . then ( ( wrapResult ) => {
58
+ if ( wrapResult . status ) {
59
+ toggleModal && toggleModal ( ) ;
60
+ refetchQuery ( [ [ "refetchOnBlock" , "useTransactionDetailsQuery" ] ] ) ;
61
+ } else {
71
62
setIsSending ( false ) ;
72
- if ( wrapResult . status ) {
73
- toggleModal && toggleModal ( ) ;
74
- refetchQuery ( [ [ "refetchOnBlock" , "useTransactionDetailsQuery" ] ] ) ;
75
- }
76
- } )
77
- . catch ( ( error ) => {
78
- console . error ( "Error raising dispute as buyer:" , error ) ;
63
+ }
64
+ } )
65
+ . catch ( ( error ) => {
66
+ console . error ( "Error raising dispute as buyer:" , error ) ;
67
+ setIsSending ( false ) ;
68
+ } ) ;
69
+ } else if ( ! isBuyer && ! isUndefined ( payArbitrationFeeBySeller ) ) {
70
+ setIsSending ( true ) ;
71
+ wrapWithToast ( async ( ) => await payArbitrationFeeBySeller ( payArbitrationFeeBySellerConfig . request ) , publicClient )
72
+ . then ( ( wrapResult ) => {
73
+ if ( wrapResult . status ) {
74
+ toggleModal && toggleModal ( ) ;
75
+ refetchQuery ( [ [ "refetchOnBlock" , "useTransactionDetailsQuery" ] ] ) ;
76
+ } else {
79
77
setIsSending ( false ) ;
80
- } ) ;
81
- }
82
- } else {
83
- if ( simulateSellerError ) {
84
- let errorMessage = "An error occurred during simulation." ;
85
- if ( simulateSellerError . message . includes ( "insufficient funds" ) ) {
86
- errorMessage = "You don't have enough balance to raise a dispute." ;
87
- }
88
- wrapWithToast ( ( ) => Promise . reject ( new Error ( errorMessage ) ) , publicClient ) ;
89
- return ;
90
- }
91
-
92
- if ( isUndefined ( payArbitrationFeeBySellerConfig ) ) {
93
- console . log ( { payArbitrationFeeBySellerConfig } ) ;
94
- console . log ( { simulateSellerError } ) ;
95
- wrapWithToast ( ( ) => Promise . reject ( new Error ( "Unable to prepare transaction." ) ) , publicClient ) ;
96
- return ;
97
- }
98
-
99
- if ( ! isUndefined ( payArbitrationFeeBySeller ) ) {
100
- setIsSending ( true ) ;
101
- wrapWithToast ( ( ) => payArbitrationFeeBySeller ( payArbitrationFeeBySellerConfig . request ) , publicClient )
102
- . then ( ( wrapResult ) => {
103
- setIsSending ( false ) ;
104
- if ( wrapResult . status ) {
105
- toggleModal && toggleModal ( ) ;
106
- refetchQuery ( [ [ "refetchOnBlock" , "useTransactionDetailsQuery" ] ] ) ;
107
- }
108
- } )
109
- . catch ( ( error ) => {
110
- console . error ( "Error raising dispute as seller:" , error ) ;
111
- setIsSending ( false ) ;
112
- } ) ;
113
- }
78
+ }
79
+ } )
80
+ . catch ( ( error ) => {
81
+ console . error ( "Error raising dispute as seller:" , error ) ;
82
+ setIsSending ( false ) ;
83
+ } ) ;
114
84
}
115
85
} ;
116
86
117
- return < Button isLoading = { isSending } disabled = { isSending } text = { buttonText } onClick = { handleRaiseDispute } /> ;
87
+ return (
88
+ < Button
89
+ isLoading = { isSending || isPreparingBuyerConfig || isPreparingSellerConfig }
90
+ disabled = { isSending || isPreparingBuyerConfig || isPreparingSellerConfig }
91
+ text = { buttonText }
92
+ onClick = { handleRaiseDispute }
93
+ />
94
+ ) ;
118
95
} ;
119
96
120
97
export default RaiseDisputeButton ;
0 commit comments