@@ -4,17 +4,18 @@ import { useParams } from "react-router-dom";
4
4
import { useAccount , useBalance } from "wagmi" ;
5
5
import { useDebounce } from "react-use" ;
6
6
import { Field , Button } from "@kleros/ui-components-library" ;
7
- import { usePrepareDisputeKitClassicFundAppeal , useDisputeKitClassicFundAppeal } from "hooks/contracts/generated" ;
8
7
import { wrapWithToast } from "utils/wrapWithToast" ;
8
+ import { isUndefined } from "utils/index" ;
9
+ import { EnsureChain } from "components/EnsureChain" ;
10
+ import { usePrepareDisputeKitClassicFundAppeal , useDisputeKitClassicFundAppeal } from "hooks/contracts/generated" ;
9
11
import { useParsedAmount } from "hooks/useParsedAmount" ;
10
12
import {
11
13
useLoserSideCountdownContext ,
12
14
useSelectedOptionContext ,
13
15
useFundingContext ,
14
16
} from "hooks/useClassicAppealContext" ;
15
- import { isUndefined } from "utils/index" ;
16
17
17
- const Fund : React . FC = ( ) => {
18
+ const useNeedFund = ( ) => {
18
19
const loserSideCountdown = useLoserSideCountdownContext ( ) ;
19
20
const { fundedChoices, winningChoice } = useFundingContext ( ) ;
20
21
const needFund =
@@ -23,24 +24,41 @@ const Fund: React.FC = () => {
23
24
! isUndefined ( winningChoice ) &&
24
25
fundedChoices . length > 0 &&
25
26
! fundedChoices . includes ( winningChoice ) ) ;
27
+
28
+ return needFund ;
29
+ } ;
30
+
31
+ const useFundAppeal = ( parsedAmount ) => {
26
32
const { id } = useParams ( ) ;
33
+ const { selectedOption } = useSelectedOptionContext ( ) ;
34
+ const { config : fundAppealConfig } = usePrepareDisputeKitClassicFundAppeal ( {
35
+ enabled : ! isUndefined ( id ) && ! isUndefined ( selectedOption ) ,
36
+ args : [ BigInt ( id ?? 0 ) , BigInt ( selectedOption ?? 0 ) ] ,
37
+ value : parsedAmount ,
38
+ } ) ;
39
+
40
+ const { writeAsync : fundAppeal } = useDisputeKitClassicFundAppeal ( fundAppealConfig ) ;
41
+
42
+ return fundAppeal ;
43
+ } ;
44
+
45
+ const Fund : React . FC = ( ) => {
46
+ const needFund = useNeedFund ( ) ;
27
47
const { address, isDisconnected } = useAccount ( ) ;
28
48
const { data : balance } = useBalance ( {
29
49
address,
30
50
watch : true ,
31
51
} ) ;
52
+
32
53
const [ amount , setAmount ] = useState ( "" ) ;
33
54
const [ debouncedAmount , setDebouncedAmount ] = useState ( "" ) ;
34
55
useDebounce ( ( ) => setDebouncedAmount ( amount ) , 500 , [ amount ] ) ;
56
+
35
57
const parsedAmount = useParsedAmount ( debouncedAmount ) ;
58
+
36
59
const [ isSending , setIsSending ] = useState ( false ) ;
37
- const { selectedOption } = useSelectedOptionContext ( ) ;
38
- const { config : fundAppealConfig } = usePrepareDisputeKitClassicFundAppeal ( {
39
- enabled : ! isUndefined ( id ) && ! isUndefined ( selectedOption ) ,
40
- args : [ BigInt ( id ?? 0 ) , BigInt ( selectedOption ?? 0 ) ] ,
41
- value : parsedAmount ,
42
- } ) ;
43
- const { writeAsync : fundAppeal } = useDisputeKitClassicFundAppeal ( fundAppealConfig ) ;
60
+ const fundAppeal = useFundAppeal ( parsedAmount ) ;
61
+
44
62
return needFund ? (
45
63
< div >
46
64
< label > How much ETH do you want to contribute?</ label >
@@ -53,21 +71,23 @@ const Fund: React.FC = () => {
53
71
} }
54
72
placeholder = "Amount to fund"
55
73
/>
56
- < StyledButton
57
- disabled = { isDisconnected || isSending || ! balance || parsedAmount > balance . value }
58
- text = { isDisconnected ? "Connect to Fund" : "Fund" }
59
- onClick = { ( ) => {
60
- if ( fundAppeal ) {
61
- setIsSending ( true ) ;
62
- wrapWithToast ( fundAppeal ! ( ) )
63
- . then ( ( ) => {
64
- setAmount ( "" ) ;
65
- close ( ) ;
66
- } )
67
- . finally ( ( ) => setIsSending ( false ) ) ;
68
- }
69
- } }
70
- />
74
+ < EnsureChain >
75
+ < StyledButton
76
+ disabled = { isDisconnected || isSending || ! balance || parsedAmount > balance . value }
77
+ text = { isDisconnected ? "Connect to Fund" : "Fund" }
78
+ onClick = { ( ) => {
79
+ if ( fundAppeal ) {
80
+ setIsSending ( true ) ;
81
+ wrapWithToast ( fundAppeal ( ) )
82
+ . then ( ( ) => {
83
+ setAmount ( "" ) ;
84
+ close ( ) ;
85
+ } )
86
+ . finally ( ( ) => setIsSending ( false ) ) ;
87
+ }
88
+ } }
89
+ />
90
+ </ EnsureChain >
71
91
</ div >
72
92
</ div >
73
93
) : (
0 commit comments