1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import validUrl from 'valid-url'
import PageContainerFooter from '../../../../components/ui/page-container/page-container-footer'
import TextField from '../../../../components/ui/text-field'
export default class NetworksTab extends PureComponent {
static contextTypes = {
t: PropTypes.func.isRequired,
metricsEvent: PropTypes.func.isRequired,
}
static propTypes = {
editRpc: PropTypes.func.isRequired,
rpcUrl: PropTypes.string,
chainId: PropTypes.string,
ticker: PropTypes.string,
viewOnly: PropTypes.bool,
networkName: PropTypes.string,
onClear: PropTypes.func.isRequired,
setRpcTarget: PropTypes.func.isRequired,
networksTabIsInAddMode: PropTypes.bool,
blockExplorerUrl: PropTypes.string,
rpcPrefs: PropTypes.object,
}
state = {
rpcUrl: this.props.rpcUrl,
chainId: this.props.chainId,
ticker: this.props.ticker,
networkName: this.props.networkName,
blockExplorerUrl: this.props.blockExplorerUrl,
errors: {},
}
componentDidUpdate (prevProps) {
const { rpcUrl: prevRpcUrl, networksTabIsInAddMode: prevAddMode } = prevProps
const {
rpcUrl,
chainId,
ticker,
networkName,
networksTabIsInAddMode,
blockExplorerUrl,
} = this.props
if (!prevAddMode && networksTabIsInAddMode) {
this.setState({
rpcUrl: '',
chainId: '',
ticker: '',
networkName: '',
blockExplorerUrl: '',
errors: {},
})
} else if (prevRpcUrl !== rpcUrl) {
this.setState({ rpcUrl, chainId, ticker, networkName, blockExplorerUrl, errors: {} })
}
}
componentWillUnmount () {
this.props.onClear()
this.setState({
rpcUrl: '',
chainId: '',
ticker: '',
networkName: '',
blockExplorerUrl: '',
errors: {},
})
}
stateIsUnchanged () {
const {
rpcUrl,
chainId,
ticker,
networkName,
blockExplorerUrl,
} = this.props
const {
rpcUrl: stateRpcUrl,
chainId: stateChainId,
ticker: stateTicker,
networkName: stateNetworkName,
blockExplorerUrl: stateBlockExplorerUrl,
} = this.state
return (
stateRpcUrl === rpcUrl &&
stateChainId === chainId &&
stateTicker === ticker &&
stateNetworkName === networkName &&
stateBlockExplorerUrl === blockExplorerUrl
)
}
renderFormTextField (fieldKey, textFieldId, onChange, value, optionalTextFieldKey) {
const { errors } = this.state
const { viewOnly } = this.props
return (
<div className="networks-tab__network-form-row">
<div className="networks-tab__network-form-label">{this.context.t(optionalTextFieldKey || fieldKey)}</div>
<TextField
type="text"
id={textFieldId}
onChange={onChange}
fullWidth
margin="dense"
value={value}
disabled={viewOnly}
error={errors[fieldKey]}
/>
</div>
)
}
setStateWithValue = (stateKey, validator) => {
return (e) => {
validator && validator(e.target.value, stateKey)
this.setState({ [stateKey]: e.target.value })
}
}
setErrorTo = (errorKey, errorVal) => {
this.setState({
errors: {
...this.state.errors,
[errorKey]: errorVal,
},
})
}
validateChainId = (chainId) => {
this.setErrorTo('chainId', !!chainId && Number.isNaN(parseInt(chainId))
? `${this.context.t('invalidInput')} chainId`
: ''
)
}
validateUrl = (url, stateKey) => {
if (validUrl.isWebUri(url)) {
this.setErrorTo(stateKey, '')
} else {
const appendedRpc = `http://${url}`
const validWhenAppended = validUrl.isWebUri(appendedRpc) && !url.match(/^https?:\/\/$/)
this.setErrorTo(stateKey, this.context.t(validWhenAppended ? 'uriErrorMsg' : 'invalidRPC'))
}
}
render () {
const { setRpcTarget, viewOnly, rpcUrl: propsRpcUrl, editRpc, rpcPrefs = {} } = this.props
const {
networkName,
rpcUrl,
chainId,
ticker,
blockExplorerUrl,
errors,
} = this.state
return (
<div className="networks-tab__network-form">
{this.renderFormTextField(
'networkName',
'network-name',
this.setStateWithValue('networkName'),
networkName,
)}
{this.renderFormTextField(
'rpcUrl',
'rpc-url',
this.setStateWithValue('rpcUrl', this.validateUrl),
rpcUrl,
)}
{this.renderFormTextField(
'chainId',
'chainId',
this.setStateWithValue('chainId', this.validateChainId),
chainId,
'optionalChainId',
)}
{this.renderFormTextField(
'symbol',
'network-ticker',
this.setStateWithValue('ticker'),
ticker,
'optionalSymbol',
)}
{this.renderFormTextField(
'blockExplorerUrl',
'block-explorer-url',
this.setStateWithValue('blockExplorerUrl', this.validateUrl),
blockExplorerUrl,
'optionalBlockExplorerUrl',
)}
<PageContainerFooter
cancelText={this.context.t('cancel')}
hideCancel={true}
onSubmit={() => {
if (propsRpcUrl && rpcUrl !== propsRpcUrl) {
editRpc(propsRpcUrl, rpcUrl, chainId, ticker, networkName, {
blockExplorerUrl: blockExplorerUrl || rpcPrefs.blockExplorerUrl,
...rpcPrefs,
})
} else {
setRpcTarget(rpcUrl, chainId, ticker, networkName, {
blockExplorerUrl: blockExplorerUrl || rpcPrefs.blockExplorerUrl,
...rpcPrefs,
})
}
}}
submitText={this.context.t('save')}
submitButtonType={'confirm'}
disabled={viewOnly || this.stateIsUnchanged() || Object.values(errors).some(x => x) || !rpcUrl}
/>
</div>
)
}
}
|