1
- import React , { useState } from "react" ;
1
+ import React , { useCallback , useMemo , useState } from "react" ;
2
2
import styled from "styled-components" ;
3
3
4
+ import { RULING_MODE } from "consts" ;
5
+ import { usePublicClient } from "wagmi" ;
6
+
4
7
import { Button } from "@kleros/ui-components-library" ;
5
8
9
+ import { useRulerContext } from "context/RulerContext" ;
10
+ import {
11
+ useSimulateKlerosCoreRulerChangeRulingModeToManual ,
12
+ useSimulateKlerosCoreRulerExecuteRuling ,
13
+ useWriteKlerosCoreRulerChangeRulingModeToManual ,
14
+ useWriteKlerosCoreRulerExecuteRuling ,
15
+ } from "hooks/contracts/generated" ;
16
+ import { isUndefined } from "utils/isUndefined" ;
17
+ import { wrapWithToast } from "utils/wrapWithToast" ;
18
+
6
19
import LabeledInput from "components/LabeledInput" ;
7
20
8
21
import Header from "./Header" ;
@@ -22,10 +35,58 @@ const SelectContainer = styled.div`
22
35
` ;
23
36
24
37
const ManualRuling : React . FC = ( ) => {
25
- const [ tie , setTie ] = useState < boolean > ( false ) ;
26
- const [ overriden , setOverriden ] = useState < boolean > ( false ) ;
38
+ const [ isSending , setIsSending ] = useState < boolean > ( false ) ;
39
+ const { arbitrable, arbitrableSettings } = useRulerContext ( ) ;
40
+ const [ tie , setTie ] = useState ( arbitrableSettings ?. tied ?? false ) ;
41
+ const [ overriden , setOverriden ] = useState ( arbitrableSettings ?. overidden ?? false ) ;
42
+ const [ ruling , setRuling ] = useState ( arbitrableSettings ?. ruling ) ;
27
43
const [ disputeId , setDisputeId ] = useState < number > ( ) ;
28
- const [ ruling , setRuling ] = useState < number > ( ) ;
44
+
45
+ const publicClient = usePublicClient ( ) ;
46
+
47
+ const { data : manualModeConfig } = useSimulateKlerosCoreRulerChangeRulingModeToManual ( {
48
+ query : {
49
+ enabled : arbitrableSettings ?. rulingMode !== RULING_MODE . Manual && ! isUndefined ( arbitrable ) ,
50
+ } ,
51
+ args : [ arbitrable as `0x${string } `] ,
52
+ } ) ;
53
+ const { writeContractAsync : changeToManualMode } = useWriteKlerosCoreRulerChangeRulingModeToManual ( ) ;
54
+
55
+ const isDisabled = useMemo ( ( ) => {
56
+ return isUndefined ( disputeId ) || isUndefined ( ruling ) || isUndefined ( arbitrable ) ;
57
+ } , [ disputeId , ruling , arbitrable ] ) ;
58
+
59
+ const {
60
+ data : executeConfig ,
61
+ isLoading : isLoadingExecuteConfig ,
62
+ isError,
63
+ } = useSimulateKlerosCoreRulerExecuteRuling ( {
64
+ query : {
65
+ enabled : arbitrableSettings ?. rulingMode === RULING_MODE . Manual && ! isUndefined ( arbitrable ) && ! isDisabled ,
66
+ } ,
67
+ args : [ BigInt ( disputeId ?? 0 ) , BigInt ( ruling ?? 0 ) , tie , overriden ] ,
68
+ } ) ;
69
+
70
+ const { writeContractAsync : executeRuling } = useWriteKlerosCoreRulerExecuteRuling ( ) ;
71
+
72
+ const handleRuling = useCallback ( async ( ) => {
73
+ if ( ! publicClient ) return ;
74
+ setIsSending ( true ) ;
75
+ if ( arbitrableSettings ?. rulingMode !== RULING_MODE . Manual ) {
76
+ if ( ! manualModeConfig ) return ;
77
+ wrapWithToast ( async ( ) => await changeToManualMode ( manualModeConfig . request ) , publicClient )
78
+ . then ( async ( res ) => {
79
+ if ( res . status && executeConfig ) {
80
+ wrapWithToast ( async ( ) => await executeRuling ( executeConfig . request ) , publicClient ) ;
81
+ }
82
+ } )
83
+ . finally ( ( ) => setIsSending ( false ) ) ;
84
+ } else if ( executeConfig ) {
85
+ wrapWithToast ( async ( ) => await executeRuling ( executeConfig . request ) , publicClient ) . finally ( ( ) =>
86
+ setIsSending ( false )
87
+ ) ;
88
+ }
89
+ } , [ publicClient , executeConfig , manualModeConfig , arbitrableSettings , changeToManualMode , executeRuling ] ) ;
29
90
30
91
return (
31
92
< Container >
@@ -48,7 +109,12 @@ const ManualRuling: React.FC = () => {
48
109
/>
49
110
</ SelectContainer >
50
111
51
- < Button text = "Rule" />
112
+ < Button
113
+ text = "Rule"
114
+ onClick = { handleRuling }
115
+ isLoading = { isLoadingExecuteConfig || isSending }
116
+ disabled = { isDisabled || isError || isSending }
117
+ />
52
118
</ Container >
53
119
) ;
54
120
} ;
0 commit comments