1
- import React , { useState } from "react" ;
1
+ import React , { useMemo , useState } from "react" ;
2
2
import styled from "styled-components" ;
3
3
import { useParams } from "react-router-dom" ;
4
4
import { useAccount , useBalance , usePublicClient } from "wagmi" ;
@@ -9,15 +9,12 @@ import { isUndefined } from "utils/index";
9
9
import { EnsureChain } from "components/EnsureChain" ;
10
10
import { usePrepareDisputeKitClassicFundAppeal , useDisputeKitClassicFundAppeal } from "hooks/contracts/generated" ;
11
11
import { useParsedAmount } from "hooks/useParsedAmount" ;
12
- import {
13
- useLoserSideCountdownContext ,
14
- useSelectedOptionContext ,
15
- useFundingContext ,
16
- } from "hooks/useClassicAppealContext" ;
12
+ import { useSelectedOptionContext , useFundingContext , useCountdownContext } from "hooks/useClassicAppealContext" ;
17
13
18
14
const Container = styled . div `
19
15
display: flex;
20
16
flex-direction: column;
17
+ align-items: center;
21
18
gap: 8px;
22
19
` ;
23
20
@@ -38,11 +35,14 @@ const StyledField = styled(Field)`
38
35
39
36
const StyledButton = styled ( Button ) `
40
37
margin: auto;
41
- margin-top: 12px ;
38
+ margin-top: 4px ;
42
39
` ;
43
40
41
+ const StyledLabel = styled . label `
42
+ align-self: flex-start;
43
+ ` ;
44
44
const useNeedFund = ( ) => {
45
- const loserSideCountdown = useLoserSideCountdownContext ( ) ;
45
+ const { loserSideCountdown } = useCountdownContext ( ) ;
46
46
const { fundedChoices, winningChoice } = useFundingContext ( ) ;
47
47
const needFund =
48
48
( loserSideCountdown ?? 0 ) > 0 ||
@@ -57,15 +57,15 @@ const useNeedFund = () => {
57
57
const useFundAppeal = ( parsedAmount ) => {
58
58
const { id } = useParams ( ) ;
59
59
const { selectedOption } = useSelectedOptionContext ( ) ;
60
- const { config : fundAppealConfig } = usePrepareDisputeKitClassicFundAppeal ( {
60
+ const { config : fundAppealConfig , isError } = usePrepareDisputeKitClassicFundAppeal ( {
61
61
enabled : ! isUndefined ( id ) && ! isUndefined ( selectedOption ) ,
62
62
args : [ BigInt ( id ?? 0 ) , BigInt ( selectedOption ?? 0 ) ] ,
63
63
value : parsedAmount ,
64
64
} ) ;
65
65
66
66
const { writeAsync : fundAppeal } = useDisputeKitClassicFundAppeal ( fundAppealConfig ) ;
67
67
68
- return fundAppeal ;
68
+ return { fundAppeal, isError } ;
69
69
} ;
70
70
71
71
interface IFund {
@@ -89,39 +89,44 @@ const Fund: React.FC<IFund> = ({ amount, setAmount, setIsOpen }) => {
89
89
const parsedAmount = useParsedAmount ( debouncedAmount ) ;
90
90
91
91
const [ isSending , setIsSending ] = useState ( false ) ;
92
- const fundAppeal = useFundAppeal ( parsedAmount ) ;
92
+ const { fundAppeal, isError } = useFundAppeal ( parsedAmount ) ;
93
+
94
+ const isFundDisabled = useMemo (
95
+ ( ) =>
96
+ isDisconnected || isSending || ! balance || parsedAmount > balance . value || Number ( parsedAmount ) <= 0 || isError ,
97
+ [ isDisconnected , isSending , balance , parsedAmount , isError ]
98
+ ) ;
93
99
94
100
return needFund ? (
95
101
< Container >
96
- < label > How much ETH do you want to contribute?</ label >
97
- < div >
98
- < StyledField
99
- type = "number"
100
- value = { amount }
101
- onChange = { ( e ) => {
102
- setAmount ( e . target . value ) ;
102
+ < StyledLabel > How much ETH do you want to contribute?</ StyledLabel >
103
+ < StyledField
104
+ type = "number"
105
+ value = { amount }
106
+ onChange = { ( e ) => {
107
+ setAmount ( e . target . value ) ;
108
+ } }
109
+ placeholder = "Amount to fund"
110
+ />
111
+ < EnsureChain >
112
+ < StyledButton
113
+ disabled = { isFundDisabled }
114
+ isLoading = { isSending }
115
+ text = { isDisconnected ? "Connect to Fund" : "Fund" }
116
+ onClick = { ( ) => {
117
+ if ( fundAppeal ) {
118
+ setIsSending ( true ) ;
119
+ wrapWithToast ( async ( ) => await fundAppeal ( ) . then ( ( response ) => response . hash ) , publicClient )
120
+ . then ( ( res ) => {
121
+ res . status && setIsOpen ( true ) ;
122
+ } )
123
+ . finally ( ( ) => {
124
+ setIsSending ( false ) ;
125
+ } ) ;
126
+ }
103
127
} }
104
- placeholder = "Amount to fund"
105
128
/>
106
- < EnsureChain >
107
- < StyledButton
108
- disabled = { isDisconnected || isSending || ! balance || parsedAmount > balance . value }
109
- text = { isDisconnected ? "Connect to Fund" : "Fund" }
110
- onClick = { ( ) => {
111
- if ( fundAppeal ) {
112
- setIsSending ( true ) ;
113
- wrapWithToast ( async ( ) => await fundAppeal ( ) . then ( ( response ) => response . hash ) , publicClient )
114
- . then ( ( ) => {
115
- setIsOpen ( true ) ;
116
- } )
117
- . finally ( ( ) => {
118
- setIsSending ( false ) ;
119
- } ) ;
120
- }
121
- } }
122
- />
123
- </ EnsureChain >
124
- </ div >
129
+ </ EnsureChain >
125
130
</ Container >
126
131
) : (
127
132
< > </ >
0 commit comments