-
Notifications
You must be signed in to change notification settings - Fork 297
/
WalletSendForm.js
executable file
·398 lines (373 loc) · 13.4 KB
/
WalletSendForm.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
390
391
392
393
394
395
396
397
398
// @flow
import React, { Component } from 'react';
import type { Node } from 'react';
import { observer } from 'mobx-react';
import classnames from 'classnames';
import { Button } from 'react-polymorph/lib/components/Button';
import { Input } from 'react-polymorph/lib/components/Input';
import { NumericInput } from 'react-polymorph/lib/components/NumericInput';
import { ButtonSkin } from 'react-polymorph/lib/skins/simple/ButtonSkin';
import { InputSkin } from 'react-polymorph/lib/skins/simple/InputSkin';
import { defineMessages, intlShape } from 'react-intl';
import BigNumber from 'bignumber.js';
import ReactToolboxMobxForm from '../../utils/ReactToolboxMobxForm';
import { submitOnEnter } from '../../utils/form';
import AmountInputSkin from './skins/AmountInputSkin';
import BorderedBox from '../widgets/BorderedBox';
import LoadingSpinner from '../widgets/LoadingSpinner';
import styles from './WalletSendForm.scss';
import globalMessages from '../../i18n/global-messages';
import WalletSendConfirmationDialog from './WalletSendConfirmationDialog';
import WalletSendConfirmationDialogContainer from '../../containers/wallet/dialogs/WalletSendConfirmationDialogContainer';
import {
formattedAmountToNaturalUnits,
formattedAmountToLovelace,
} from '../../utils/formatters';
import { FORM_VALIDATION_DEBOUNCE_WAIT } from '../../config/timingConfig';
import { FormattedHTMLMessageWithLink } from '../widgets/FormattedHTMLMessageWithLink';
/* eslint-disable consistent-return */
export const messages = defineMessages({
titleLabel: {
id: 'wallet.send.form.title.label',
defaultMessage: '!!!Title',
description: 'Label for the "Title" text input in the wallet send form.',
},
titleHint: {
id: 'wallet.send.form.title.hint',
defaultMessage: '!!!E.g: Money for Frank',
description:
'Hint inside the "Receiver" text input in the wallet send form.',
},
receiverLabel: {
id: 'wallet.send.form.receiver.label',
defaultMessage: '!!!Receiver',
description: 'Label for the "Receiver" text input in the wallet send form.',
},
receiverHint: {
id: 'wallet.send.form.receiver.hint',
defaultMessage: '!!!Wallet Address',
description:
'Hint inside the "Receiver" text input in the wallet send form.',
},
amountLabel: {
id: 'wallet.send.form.amount.label',
defaultMessage: '!!!Amount',
description: 'Label for the "Amount" number input in the wallet send form.',
},
descriptionLabel: {
id: 'wallet.send.form.description.label',
defaultMessage: '!!!Description',
description:
'Label for the "description" text area in the wallet send form.',
},
descriptionHint: {
id: 'wallet.send.form.description.hint',
defaultMessage: '!!!You can add a message if you want',
description: 'Hint in the "description" text area in the wallet send form.',
},
nextButtonLabel: {
id: 'wallet.send.form.next',
defaultMessage: '!!!Next',
description: 'Label for the next button on the wallet send form.',
},
invalidAddress: {
id: 'wallet.send.form.errors.invalidAddress',
defaultMessage: '!!!Please enter a valid address.',
description: 'Error message shown when invalid address was entered.',
},
invalidAmount: {
id: 'wallet.send.form.errors.invalidAmount',
defaultMessage: '!!!Please enter a valid amount.',
description: 'Error message shown when invalid amount was entered.',
},
invalidTitle: {
id: 'wallet.send.form.errors.invalidTitle',
defaultMessage: '!!!Please enter a title with at least 3 characters.',
description:
'Error message shown when invalid transaction title was entered.',
},
syncingTransactionsMessage: {
id: 'wallet.send.form.syncingTransactionsMessage',
defaultMessage:
'!!!This wallet is currently being synced with the blockchain. While synchronisation is in progress transacting is not possible and transaction history is not complete.',
description:
'Syncing transactions message shown during async wallet restore in the wallet send form.',
},
});
messages.fieldIsRequired = globalMessages.fieldIsRequired;
type Props = {
currencyUnit: string,
currencyMaxIntegerDigits?: number,
currencyMaxFractionalDigits: number,
validateAmount: (amountInNaturalUnits: string) => Promise<boolean>,
calculateTransactionFee: (
address: string,
amount: number
) => Promise<BigNumber>,
addressValidator: Function,
openDialogAction: Function,
isDialogOpen: Function,
onExternalLinkClick?: Function,
isRestoreActive: boolean,
};
type State = {
isTransactionFeeCalculated: boolean,
transactionFee: BigNumber,
transactionFeeError: ?string | ?Node,
};
@observer
export default class WalletSendForm extends Component<Props, State> {
static contextTypes = {
intl: intlShape.isRequired,
};
state = {
isTransactionFeeCalculated: false,
transactionFee: new BigNumber(0),
transactionFeeError: null,
};
// We need to track the fee calculation state in order to disable
// the "Submit" button as soon as either receiver or amount field changes.
// This is required as we are using debounced validation and we need to
// disable the "Submit" button as soon as the value changes and then wait for
// the validation to end in order to see if the button should be enabled or not.
_isCalculatingFee = false;
// We need to track the mounted state in order to avoid calling
// setState promise handling code after the component was already unmounted:
// Read more: https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
_isMounted = false;
componentDidMount() {
this._isMounted = true;
}
componentWillUnmount() {
this._isMounted = false;
}
handleOnSubmit = () => {
if (this.isDisabled()) {
return false;
}
this.props.openDialogAction({
dialog: WalletSendConfirmationDialog,
});
};
handleSubmitOnEnter = submitOnEnter.bind(this, this.handleOnSubmit);
isDisabled = () =>
this._isCalculatingFee || !this.state.isTransactionFeeCalculated;
// FORM VALIDATION
form = new ReactToolboxMobxForm(
{
fields: {
receiver: {
label: this.context.intl.formatMessage(messages.receiverLabel),
placeholder: this.context.intl.formatMessage(messages.receiverHint),
value: '',
validators: [
async ({ field, form }) => {
const { value } = field;
if (value === '') {
this._resetTransactionFee();
return [
false,
this.context.intl.formatMessage(messages.fieldIsRequired),
];
}
const isValidAddress = await this.props.addressValidator(value);
const amountField = form.$('amount');
const amountValue = amountField.value;
const isAmountValid = amountField.isValid;
if (isValidAddress && isAmountValid) {
await this._calculateTransactionFee(value, amountValue);
} else {
this._resetTransactionFee();
}
return [
isValidAddress,
this.context.intl.formatMessage(messages.invalidAddress),
];
},
],
},
amount: {
label: this.context.intl.formatMessage(messages.amountLabel),
placeholder: `0.${'0'.repeat(
this.props.currencyMaxFractionalDigits
)}`,
value: null,
validators: [
async ({ field, form }) => {
if (field.value === null) {
this._resetTransactionFee();
return [
false,
this.context.intl.formatMessage(messages.fieldIsRequired),
];
}
const amountValue = field.value.toString();
const isValid = await this.props.validateAmount(
formattedAmountToNaturalUnits(amountValue)
);
const receiverField = form.$('receiver');
const receiverValue = receiverField.value;
const isReceiverValid = receiverField.isValid;
if (isValid && isReceiverValid) {
await this._calculateTransactionFee(receiverValue, amountValue);
} else {
this._resetTransactionFee();
}
return [
isValid,
this.context.intl.formatMessage(messages.invalidAmount),
];
},
],
},
},
},
{
options: {
validateOnBlur: false,
validateOnChange: true,
validationDebounceWait: FORM_VALIDATION_DEBOUNCE_WAIT,
},
}
);
render() {
const { form } = this;
const { intl } = this.context;
const {
currencyUnit,
currencyMaxFractionalDigits,
isDialogOpen,
isRestoreActive,
onExternalLinkClick,
} = this.props;
const {
isTransactionFeeCalculated,
transactionFee,
transactionFeeError,
} = this.state;
const amountField = form.$('amount');
const receiverField = form.$('receiver');
const receiverFieldProps = receiverField.bind();
const amountFieldProps = amountField.bind();
const amount = new BigNumber(amountFieldProps.value || 0);
let fees = null;
let total = null;
if (isTransactionFeeCalculated) {
fees = transactionFee.toFormat(currencyMaxFractionalDigits);
total = amount.add(transactionFee).toFormat(currencyMaxFractionalDigits);
}
const buttonClasses = classnames(['primary', styles.nextButton]);
return (
<div className={styles.component}>
{isRestoreActive ? (
<div className={styles.syncingTransactionsWrapper}>
<LoadingSpinner big />
<p className={styles.syncingTransactionsText}>
{intl.formatMessage(messages.syncingTransactionsMessage)}
</p>
</div>
) : (
<BorderedBox>
<div className="WalletSendForm">
<div className={styles.receiverInput}>
<Input
className="receiver"
label={intl.formatMessage(messages.receiverLabel)}
{...receiverField.bind()}
error={receiverField.error}
onChange={value => {
this._isCalculatingFee = true;
receiverField.onChange(value || '');
}}
skin={InputSkin}
onKeyPress={this.handleSubmitOnEnter}
/>
</div>
<div className={styles.amountInput}>
<NumericInput
{...amountFieldProps}
className="amount"
label={intl.formatMessage(messages.amountLabel)}
numberLocaleOptions={{
minimumFractionDigits: currencyMaxFractionalDigits,
}}
error={transactionFeeError || amountField.error}
onChange={value => {
this._isCalculatingFee = true;
amountField.onChange(value);
}}
// AmountInputSkin props
currency={currencyUnit}
fees={fees}
total={total}
skin={AmountInputSkin}
onKeyPress={this.handleSubmitOnEnter}
allowSigns={false}
/>
</div>
<Button
className={buttonClasses}
label={intl.formatMessage(messages.nextButtonLabel)}
onClick={this.handleOnSubmit}
skin={ButtonSkin}
disabled={this.isDisabled()}
/>
</div>
</BorderedBox>
)}
{isDialogOpen(WalletSendConfirmationDialog) ? (
<WalletSendConfirmationDialogContainer
amount={amount.toFormat(currencyMaxFractionalDigits)}
receiver={receiverFieldProps.value}
totalAmount={total}
transactionFee={fees}
amountToNaturalUnits={formattedAmountToNaturalUnits}
currencyUnit={currencyUnit}
onExternalLinkClick={onExternalLinkClick}
/>
) : null}
</div>
);
}
_resetTransactionFee() {
if (this._isMounted) {
this.setState({
isTransactionFeeCalculated: false,
transactionFee: new BigNumber(0),
transactionFeeError: null,
});
}
}
async _calculateTransactionFee(address: string, amountValue: string) {
const amount = formattedAmountToLovelace(amountValue);
try {
const fee = await this.props.calculateTransactionFee(address, amount);
if (this._isMounted) {
this._isCalculatingFee = false;
this.setState({
isTransactionFeeCalculated: true,
transactionFee: fee,
transactionFeeError: null,
});
}
} catch (error) {
const errorHasLink = !!error.values.linkLabel;
const transactionFeeError = errorHasLink ? (
<FormattedHTMLMessageWithLink
message={error}
onExternalLinkClick={this.props.onExternalLinkClick}
/>
) : (
this.context.intl.formatMessage(error)
);
if (this._isMounted) {
this._isCalculatingFee = false;
this.setState({
isTransactionFeeCalculated: false,
transactionFee: new BigNumber(0),
transactionFeeError,
});
}
}
}
}