1
+
2
+ import React , { useEffect , useState } from 'react'
3
+ import { txnListType } from '../types' ;
4
+ import { IconButton , TextField , Select , MenuItem , InputAdornment , makeStyles } from '@material-ui/core' ;
5
+ import SearchIcon from '@material-ui/icons/Search' ;
6
+ import { withRouter } from 'react-router-dom' ;
7
+ import Dialog from '@material-ui/core/Dialog' ;
8
+ import TransactionView from '../View/TransactionView' ;
9
+ import BlockView from '../View/BlockView' ;
10
+
11
+ const useStyles = makeStyles ( ( theme ) => ( {
12
+ searchField : {
13
+ display : 'flex' ,
14
+ alignItems : 'center' ,
15
+ justifyContent : 'center' ,
16
+ '& .MuiOutlinedInput-input' : {
17
+ padding : '16px 14px'
18
+ }
19
+ } ,
20
+ searchInput : {
21
+ marginRight : theme . spacing ( 0 ) ,
22
+ '& > div' : {
23
+ paddingRight : '24px !important' ,
24
+ }
25
+ } ,
26
+ iconButton : {
27
+ height : 40 ,
28
+ width : 40 ,
29
+ color : '#21295c' ,
30
+ backgroundColor : '#b9d6e1' ,
31
+ borderRadius : 15
32
+ }
33
+ } ) ) ;
34
+
35
+
36
+ const SearchByQuery = ( props ) => {
37
+ let { txnList } = props ;
38
+ let { blockSearch } = props ;
39
+ const classes = useStyles ( ) ;
40
+ const options = [ "Txn Hash" , "Block No" ]
41
+ const [ search , setSearch ] = useState ( "" )
42
+ const [ selectedOption , setSelectedOption ] = useState ( "Txn Hash" )
43
+ const [ dialogOpen , setDialogOpen ] = useState ( false )
44
+ const [ error , setError ] = useState ( props . searchError )
45
+ const [ searchClick , setSearchClick ] = useState ( false ) ;
46
+
47
+ useEffect ( ( ) => {
48
+ if ( props . searchError || searchClick ) {
49
+ setSearchClick ( false ) ; setError ( props . searchError ) }
50
+ } , [ props . searchError , searchClick ] )
51
+
52
+ const searchData = async ( ) => {
53
+ if ( selectedOption === "Txn Hash" ) {
54
+ await props . getTxnList ( props . currentChannel , search )
55
+ } else if ( selectedOption === "Block No" ) {
56
+ await props . getBlockSearch ( props . currentChannel , search )
57
+ }
58
+ handleDialogOpen ( ) ;
59
+ setSearchClick ( true )
60
+ }
61
+
62
+ const handleSubmit = async ( e ) => {
63
+ e . preventDefault ( ) ;
64
+ if ( ! search || ( selectedOption === "Block No" && ( isNaN ( search ) || search . length > 9 ) ) ) {
65
+ setError ( "Please enter valid txn hash/block no" )
66
+ return
67
+ }
68
+ searchData ( ) ;
69
+
70
+ }
71
+
72
+ const handleDialogOpen = ( ) => {
73
+ setDialogOpen ( true )
74
+
75
+ }
76
+
77
+ const handleDialogClose = ( ) => {
78
+ setDialogOpen ( false )
79
+ }
80
+
81
+ return (
82
+ < div className = { classes . searchField } >
83
+ < form onSubmit = { handleSubmit } >
84
+ < Select
85
+ value = { selectedOption }
86
+ onChange = { ( e ) => { setSelectedOption ( e . target . value ) ; if ( error ) { setDialogOpen ( false ) ; setError ( '' ) } } }
87
+ className = { classes . searchInput }
88
+ displayEmpty
89
+ variant = 'outlined'
90
+ style = { { width : 110 } }
91
+ MenuProps = { {
92
+ anchorOrigin : {
93
+ vertical : "bottom" ,
94
+ horizontal : "left"
95
+ } ,
96
+ getContentAnchorEl : null
97
+ } }
98
+ >
99
+ { options . map ( ( option ) => (
100
+ < MenuItem key = { option } value = { option } >
101
+ { option }
102
+ </ MenuItem >
103
+
104
+ ) ) }
105
+ </ Select >
106
+ < TextField
107
+ value = { search }
108
+ onChange = { ( e ) => { setSearch ( e . target . value ) ; if ( error ) { setDialogOpen ( false ) ; setError ( '' ) ; } } }
109
+ onKeyPress = { ( e ) => e . key === 'Enter' && handleSubmit ( e ) }
110
+ label = " Search by Txn Hash / Block"
111
+ variant = 'outlined'
112
+ style = { { width : 550 } }
113
+ error = { error }
114
+ helperText = { error }
115
+ InputProps = { {
116
+ endAdornment : (
117
+ < InputAdornment position = 'end' >
118
+ < IconButton onClick = { handleSubmit } className = { classes . iconButton } >
119
+ < SearchIcon />
120
+ </ IconButton >
121
+ </ InputAdornment >
122
+ )
123
+ } }
124
+ />
125
+ </ form >
126
+ < Dialog
127
+ open = { dialogOpen && ! error }
128
+ onClose = { handleDialogClose }
129
+ fullWidth
130
+ maxWidth = "md"
131
+ >
132
+ { ! error && selectedOption === 'Block No' ? < BlockView blockHash = { blockSearch } onClose = { handleDialogClose } />
133
+ : < TransactionView transaction = { txnList } onClose = { handleDialogClose } />
134
+ }
135
+ </ Dialog >
136
+ </ div >
137
+ )
138
+ }
139
+ SearchByQuery . propTypes = {
140
+ txnList : txnListType . isRequired
141
+ } ;
142
+
143
+
144
+ export default withRouter ( SearchByQuery )
0 commit comments