@@ -15,6 +15,8 @@ import { isUndefined } from "utils/index";
15
15
import { wrapWithToast } from "utils/wrapWithToast" ;
16
16
17
17
import { EnsureChain } from "components/EnsureChain" ;
18
+ import { ErrorButtonMessage } from "components/ErrorButtonMessage" ;
19
+ import ClosedCircleIcon from "components/StyledIcons/ClosedCircleIcon" ;
18
20
19
21
const Container = styled . div `
20
22
display: flex;
@@ -46,6 +48,7 @@ const StyledButton = styled(Button)`
46
48
const StyledLabel = styled . label `
47
49
align-self: flex-start;
48
50
` ;
51
+
49
52
const useNeedFund = ( ) => {
50
53
const { loserSideCountdown } = useCountdownContext ( ) ;
51
54
const { fundedChoices, winningChoice } = useFundingContext ( ) ;
@@ -59,20 +62,24 @@ const useNeedFund = () => {
59
62
return needFund ;
60
63
} ;
61
64
62
- const useFundAppeal = ( parsedAmount ) => {
65
+ const useFundAppeal = ( parsedAmount , insufficientBalance ) => {
63
66
const { id } = useParams ( ) ;
64
67
const { selectedOption } = useSelectedOptionContext ( ) ;
65
- const { data : fundAppealConfig , isError } = useSimulateDisputeKitClassicFundAppeal ( {
68
+ const {
69
+ data : fundAppealConfig ,
70
+ isLoading,
71
+ isError,
72
+ } = useSimulateDisputeKitClassicFundAppeal ( {
66
73
query : {
67
- enabled : ! isUndefined ( id ) && ! isUndefined ( selectedOption ) ,
74
+ enabled : ! isUndefined ( id ) && ! isUndefined ( selectedOption ) && ! insufficientBalance ,
68
75
} ,
69
76
args : [ BigInt ( id ?? 0 ) , BigInt ( selectedOption ?? 0 ) ] ,
70
77
value : parsedAmount ,
71
78
} ) ;
72
79
73
80
const { writeContractAsync : fundAppeal } = useWriteDisputeKitClassicFundAppeal ( ) ;
74
81
75
- return { fundAppeal, fundAppealConfig, isError } ;
82
+ return { fundAppeal, fundAppealConfig, isLoading , isError } ;
76
83
} ;
77
84
78
85
interface IFund {
@@ -98,12 +105,15 @@ const Fund: React.FC<IFund> = ({ amount, setAmount, setIsOpen }) => {
98
105
99
106
const parsedAmount = useParsedAmount ( debouncedAmount as `${number } `) ;
100
107
101
- const { fundAppealConfig, fundAppeal, isError } = useFundAppeal ( parsedAmount ) ;
108
+ const insufficientBalance = useMemo ( ( ) => {
109
+ return balance && balance . value < parsedAmount ;
110
+ } , [ balance , parsedAmount ] ) ;
111
+
112
+ const { fundAppealConfig, fundAppeal, isLoading, isError } = useFundAppeal ( parsedAmount , insufficientBalance ) ;
102
113
103
114
const isFundDisabled = useMemo (
104
- ( ) =>
105
- isDisconnected || isSending || ! balance || parsedAmount > balance . value || Number ( parsedAmount ) <= 0 || isError ,
106
- [ isDisconnected , isSending , balance , parsedAmount , isError ]
115
+ ( ) => isDisconnected || isSending || ! balance || insufficientBalance || Number ( parsedAmount ) <= 0 || isError ,
116
+ [ isDisconnected , isSending , balance , insufficientBalance , parsedAmount , isError ]
107
117
) ;
108
118
109
119
return needFund ? (
@@ -118,28 +128,33 @@ const Fund: React.FC<IFund> = ({ amount, setAmount, setIsOpen }) => {
118
128
placeholder = "Amount to fund"
119
129
/>
120
130
< EnsureChain >
121
- < StyledButton
122
- disabled = { isFundDisabled }
123
- isLoading = { isSending }
124
- text = { isDisconnected ? "Connect to Fund" : "Fund" }
125
- onClick = { ( ) => {
126
- if ( fundAppeal && fundAppealConfig && publicClient ) {
127
- setIsSending ( true ) ;
128
- wrapWithToast ( async ( ) => await fundAppeal ( fundAppealConfig . request ) , publicClient )
129
- . then ( ( res ) => {
130
- res . status && setIsOpen ( true ) ;
131
- } )
132
- . finally ( ( ) => {
133
- setIsSending ( false ) ;
134
- } ) ;
135
- }
136
- } }
137
- />
131
+ < div >
132
+ < StyledButton
133
+ disabled = { isFundDisabled }
134
+ isLoading = { ( isSending || isLoading ) && ! insufficientBalance }
135
+ text = { isDisconnected ? "Connect to Fund" : "Fund" }
136
+ onClick = { ( ) => {
137
+ if ( fundAppeal && fundAppealConfig && publicClient ) {
138
+ setIsSending ( true ) ;
139
+ wrapWithToast ( async ( ) => await fundAppeal ( fundAppealConfig . request ) , publicClient )
140
+ . then ( ( res ) => {
141
+ res . status && setIsOpen ( true ) ;
142
+ } )
143
+ . finally ( ( ) => {
144
+ setIsSending ( false ) ;
145
+ } ) ;
146
+ }
147
+ } }
148
+ />
149
+ { insufficientBalance && (
150
+ < ErrorButtonMessage >
151
+ < ClosedCircleIcon /> Insufficient balance
152
+ </ ErrorButtonMessage >
153
+ ) }
154
+ </ div >
138
155
</ EnsureChain >
139
156
</ Container >
140
- ) : (
141
- < > </ >
142
- ) ;
157
+ ) : null ;
143
158
} ;
144
159
145
160
export default Fund ;
0 commit comments