-
Notifications
You must be signed in to change notification settings - Fork 97
/
form.js
389 lines (353 loc) · 12.2 KB
/
form.js
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// eslint-disable-line max-lines
import React from 'react';
import ConverterV2 from '../../converterV2';
import { PrimaryButtonV2 } from '../../toolbox/buttons/button';
import { InputV2, AutoresizeTextarea } from '../../toolbox/inputsV2';
import Bookmark from '../../bookmarkV2';
import regex from '../../../utils/regex';
import SpinnerV2 from '../../spinnerV2/spinnerV2';
import svg from '../../../utils/svgIcons';
import Tooltip from '../../toolbox/tooltip/tooltip';
import links from '../../../constants/externalLinks';
import { fromRawLsk } from '../../../utils/lsk';
import fees from '../../../constants/fees';
import styles from './form.css';
import Piwik from '../../../utils/piwik';
class Form extends React.Component {
// eslint-disable-next-line max-statements
constructor(props) {
super(props);
this.state = {
isLoading: false,
fields: {
recipient: {
address: '',
balance: '',
error: false,
feedback: '',
name: 'recipient',
selected: false,
title: '',
value: '',
showSuggestions: false,
},
amount: {
error: false,
value: '',
feedback: '',
},
reference: {
error: false,
value: '',
feedback: '',
},
},
};
this.loaderTimeout = null;
this.getMaxAmount = this.getMaxAmount.bind(this);
this.ifDataFromPrevState = this.ifDataFromPrevState.bind(this);
this.ifDataFromUrl = this.ifDataFromUrl.bind(this);
this.onAmountOrReferenceChange = this.onAmountOrReferenceChange.bind(this);
this.onGoNext = this.onGoNext.bind(this);
this.onInputChange = this.onInputChange.bind(this);
this.onSelectedAccount = this.onSelectedAccount.bind(this);
this.validateAmountAndReference = this.validateAmountAndReference.bind(this);
this.validateBookmark = this.validateBookmark.bind(this);
}
componentDidMount() {
this.ifDataFromPrevState();
this.ifDataFromUrl();
}
ifDataFromPrevState() {
const { prevState } = this.props;
if (prevState.fields && Object.entries(prevState.fields).length > 0) {
this.setState({ fields: { ...prevState.fields } });
}
}
ifDataFromUrl() {
const { fields = {} } = this.props;
if (Object.entries(fields).length > 0) {
this.setState({
fields: {
...this.state.fields,
recipient: {
...this.state.fields.recipient,
address: fields.recipient.address,
},
amount: {
...this.state.fields.amount,
value: fields.amount.value,
},
reference: {
...this.state.fields.reference,
value: fields.reference.value,
},
},
});
}
}
onInputChange({ target }) {
this.setState({
fields: {
...this.state.fields,
[target.name]: {
...this.state.fields[target.name],
value: target.value,
},
},
});
}
// eslint-disable-next-line max-statements
validateBookmark() {
const { followedAccounts } = this.props;
let recipient = this.state.fields.recipient;
let isAccountValid = '';
let isAddressValid = '';
if (followedAccounts.length && recipient.value !== '') {
isAccountValid = followedAccounts
.find(account => account.title.toLowerCase() === recipient.value.toLowerCase()) || false;
isAddressValid = regex.address.test(recipient.value);
} else {
isAddressValid = recipient.value.match(regex.address);
}
// istanbul ignore else
if (isAddressValid) {
recipient = {
...this.state.recipient,
address: recipient.value,
selected: false,
error: false,
feedback: '',
showSuggestions: false,
};
}
// istanbul ignore else
if (isAccountValid) {
recipient = {
...this.state.recipient,
address: isAccountValid.address,
title: isAccountValid.title,
balance: isAccountValid.balance,
selected: true,
error: false,
feedback: '',
showSuggestions: false,
};
}
// istanbul ignore else
if (!isAccountValid && !isAddressValid && recipient.value) {
recipient = {
...this.state.recipient,
address: '',
balance: '',
error: true,
feedback: 'Provide a correct wallet address or a name of a followed account',
selected: false,
title: '',
showSuggestions: true,
};
}
// istanbul ignore else
if (recipient.value === '') {
recipient = {
...this.state.recipient,
address: '',
balance: '',
error: false,
feedback: '',
selected: false,
title: '',
showSuggestions: false,
};
}
this.setState({
fields: {
...this.state.fields,
recipient: {
...this.state.fields.recipient,
...recipient,
},
},
});
}
onSelectedAccount(account) {
this.setState({
fields: {
...this.state.fields,
recipient: {
...this.state.fields.recipient,
...account,
value: account.address,
selected: true,
error: '',
feedback: '',
showSuggestions: false,
},
},
});
}
getMaxAmount() {
return fromRawLsk(Math.max(0, this.props.account.balance - fees.send));
}
validateAmountField(value) {
if (/([^\d.])/g.test(value)) return this.props.t('Provide a correct amount of LSK');
if (/(\.)(.*\1){1}/g.test(value) || /\.$/.test(value)) return this.props.t('Invalid amount');
if (value > this.getMaxAmount()) return this.props.t('Provided amount is higher than your current balance.');
return false;
}
// eslint-disable-next-line max-statements
validateAmountAndReference(name, value) {
const { t } = this.props;
const messageMaxLength = 64;
let feedback = '';
let error = '';
// istanbul ignore else
if (name === 'amount') {
value = /^\./.test(value) ? `0${value}` : value;
error = this.validateAmountField(value);
feedback = error || feedback;
}
// istanbul ignore else
if (name === 'reference' && value.length > 0) {
const byteCount = encodeURI(value).split(/%..|./).length - 1;
error = byteCount > messageMaxLength;
feedback = error
? t('{{length}} extra characters', { length: byteCount - messageMaxLength })
: t('{{length}} out of {{total}} characters left', {
length: messageMaxLength - value.length,
total: messageMaxLength,
});
}
this.setState(prevState => ({
fields: {
...prevState.fields,
[name]: {
error: !!error,
value,
feedback,
},
},
}));
}
onAmountOrReferenceChange({ target }) {
clearTimeout(this.loaderTimeout);
this.setState({ isLoading: true });
this.loaderTimeout = setTimeout(() => {
this.setState({ isLoading: false });
this.validateAmountAndReference(target.name, target.value);
}, 300);
this.onInputChange({ target });
}
// istanbul ignore next
onGoNext() {
Piwik.trackingEvent('Send_Form', 'button', 'Next step');
this.props.nextStep({
fields: { ...this.state.fields },
});
}
// eslint-disable-next-line complexity
render() {
const { fields } = this.state;
const messageMaxLength = 64;
const byteCount = encodeURI(fields.reference.value).split(/%..|./).length - 1;
const isBtnDisabled =
fields.recipient.error || fields.amount.error || fields.reference.error ||
fields.recipient.value === '' || fields.amount.value === '';
return (
<div className={`${styles.wrapper}`}>
<header className={styles.header}>
<h1>{this.props.t('Send LSK')}</h1>
</header>
<div className={styles.formSection}>
<label className={`${styles.fieldGroup} recipient`}>
<span className={`${styles.fieldLabel}`}>{this.props.t('Recipient')}</span>
<Bookmark
validateBookmark={this.validateBookmark}
followedAccounts={this.props.followedAccounts}
onChange={this.onInputChange}
placeholder={this.props.t('e.g. 1234523423L or John Doe')}
recipient={fields.recipient}
showSuggestions={fields.recipient.showSuggestions}
onSelectedAccount={this.onSelectedAccount}
/>
</label>
<label className={`${styles.fieldGroup}`}>
<span className={`${styles.fieldLabel}`}>{this.props.t('Amount of transaction')}</span>
<span className={`${styles.amountField} amount`}>
<InputV2
autoComplete={'off'}
onChange={this.onAmountOrReferenceChange}
name='amount'
value={fields.amount.value}
placeholder={this.props.t('e.g. 12345.6')}
className={`${styles.input} ${fields.amount.error ? 'error' : ''}`} />
<ConverterV2
className={styles.converter}
value={fields.amount.value}
error={fields.amount.error} />
<SpinnerV2 className={`${styles.spinner} ${this.state.isLoading && fields.amount.value ? styles.show : styles.hide}`}/>
<img
className={`${styles.status} ${!this.state.isLoading && fields.amount.value ? styles.show : styles.hide}`}
src={ fields.amount.error ? svg.alert_icon : svg.ok_icon}
/>
</span>
<span className={`${styles.feedback} ${fields.amount.error ? 'error' : ''} ${fields.amount.feedback ? styles.show : ''}`}>
{fields.amount.feedback}
</span>
<span className={styles.amountHint}>
{this.props.t('+{{fee}} LSK transaction fee', { fee: fromRawLsk(fees.send) })}
<Tooltip
className={'showOnTop'}
title={this.props.t('Transaction fee')}
footer={
<a href={links.transactionFee}
rel="noopener noreferrer"
target="_blank">
{this.props.t('Read More')}
</a>
}
>
<p>
{
this.props.t(`Every transaction needs to be confirmed and forged into Lisks blockchain network.
Such operations require hardware resources and because of that we ask for a small fee for processing those.`)
}
</p>
</Tooltip>
</span>
</label>
<label className={`${styles.fieldGroup} reference`}>
<span className={`${styles.fieldLabel}`}>{this.props.t('Message (optional)')}</span>
<span className={styles.referenceField}>
<AutoresizeTextarea
maxLength={100}
spellCheck={false}
onChange={this.onAmountOrReferenceChange}
name='reference'
value={fields.reference.value}
placeholder={this.props.t('Write message')}
className={`${styles.textarea} ${fields.reference.error ? 'error' : ''}`} />
<SpinnerV2 className={`${styles.spinner} ${this.state.isLoading && fields.reference.value ? styles.show : styles.hide}`}/>
<img
className={`${styles.status} ${!this.state.isLoading && fields.reference.value ? styles.show : styles.hide}`}
src={ fields.reference.error ? svg.alert_icon : svg.ok_icon} />
</span>
<span className={`${styles.feedback} ${fields.reference.error || messageMaxLength - byteCount < 10 ? 'error' : ''} ${fields.reference.feedback ? styles.show : ''}`}>
{fields.reference.feedback}
</span>
</label>
</div>
<footer>
<PrimaryButtonV2
className={`${styles.confirmButton} btn-submit`}
disabled={isBtnDisabled}
onClick={this.onGoNext}
>
{this.props.t('Go to Confirmation')}
</PrimaryButtonV2>
</footer>
</div>
);
}
}
export default Form;