-
Notifications
You must be signed in to change notification settings - Fork 295
/
VotingRegistrationStepsRegister.tsx
258 lines (242 loc) · 8.49 KB
/
VotingRegistrationStepsRegister.tsx
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
// @flow
import React, { Component } from 'react';
import type { Node } from 'react';
import { get } from 'lodash';
import { defineMessages, intlShape, FormattedHTMLMessage } from 'react-intl';
import { Input } from 'react-polymorph/lib/components/Input';
import { InputSkin } from 'react-polymorph/lib/skins/simple/InputSkin';
import vjf from 'mobx-react-form/lib/validators/VJF';
import BigNumber from 'bignumber.js';
import { observer } from 'mobx-react';
import { Link } from 'react-polymorph/lib/components/Link';
import { LinkSkin } from 'react-polymorph/lib/skins/simple/LinkSkin';
import { submitOnEnter } from '../../../utils/form';
import globalMessages from '../../../i18n/global-messages';
import ReactToolboxMobxForm from '../../../utils/ReactToolboxMobxForm';
import { formattedWalletAmount } from '../../../utils/formatters';
import { FORM_VALIDATION_DEBOUNCE_WAIT } from '../../../config/timingConfig';
import LocalizableError from '../../../i18n/LocalizableError';
import styles from './VotingRegistrationStepsRegister.scss';
import VotingRegistrationDialog from './widgets/VotingRegistrationDialog';
import Wallet, { HwDeviceStatuses } from '../../../domains/Wallet';
import HardwareWalletStatus from '../../hardware-wallet/HardwareWalletStatus';
import { NEXT_VOTING_FUND_NUMBER } from '../../../config/votingConfig';
import type { HwDeviceStatus } from '../../../domains/Wallet';
const messages = defineMessages({
description: {
id: 'voting.votingRegistration.register.step.description',
defaultMessage:
'!!!Please sign the voting registration transaction. This transaction links your wallet balance with your Fund{nextVotingFundNumber} voting registration, as a proof of your voting power. Funds will not leave your wallet, but registration requires paying transaction fees, as displayed on-screen.',
description: 'Description on the voting registration "sign" step.',
},
continueButtonLabel: {
id: 'voting.votingRegistration.register.step.continueButtonLabel',
defaultMessage: '!!!Submit registration transaction',
description:
'Label for continue button on the voting registration "sign" step.',
},
feesLabel: {
id: 'voting.votingRegistration.register.step.feesLabel',
defaultMessage: '!!!Fees',
description: 'Fees label on the voting registration "sign" step.',
},
spendingPasswordPlaceholder: {
id: 'voting.votingRegistration.register.step.spendingPasswordPlaceholder',
defaultMessage: '!!!Spending password',
description: 'Placeholder for "spending password"',
},
spendingPasswordLabel: {
id: 'voting.votingRegistration.register.step.spendingPasswordLabel',
defaultMessage: '!!!Spending password',
description: 'Label for "spending password"',
},
calculatingFees: {
id: 'voting.votingRegistration.register.step.calculatingFees',
defaultMessage: '!!!Calculating fees',
description: '"Calculating fees" message in the "sign" step.',
},
learnMoreLink: {
id: 'voting.votingRegistration.register.step.learnMoreLink',
defaultMessage: '!!!Learn more',
description: '"Learn more" link on the "sign" step.',
},
learntMoreLinkUrl: {
id: 'voting.votingRegistration.register.step.learntMoreLinkUrl',
defaultMessage:
'!!!https://iohk.zendesk.com/hc/en-us/articles/900006490763',
description: 'Learn more" link URL on the "sign" step.',
},
});
messages.fieldIsRequired = globalMessages.fieldIsRequired;
type Props = {
stepsList: Array<string>,
activeStep: number,
transactionFee: ?BigNumber,
transactionFeeError?: string | Node | null,
transactionError?: ?LocalizableError,
hwDeviceStatus: HwDeviceStatus,
selectedWallet: ?Wallet,
isTrezor: boolean,
isHardwareWallet: boolean,
isSubmitting: boolean,
onConfirm: Function,
onClose: Function,
onBack: Function,
onExternalLinkClick: Function,
};
@observer
export default class VotingRegistrationStepsRegister extends Component<Props> {
static contextTypes = {
intl: intlShape.isRequired,
};
form = new ReactToolboxMobxForm(
{
fields: {
spendingPassword: {
type: 'password',
label: this.context.intl.formatMessage(
messages.spendingPasswordLabel
),
placeholder: this.context.intl.formatMessage(
messages.spendingPasswordPlaceholder
),
value: '',
validators: [
({ field }) => {
const password = field.value;
if (password === '') {
return [
false,
this.context.intl.formatMessage(messages.fieldIsRequired),
];
}
return [true];
},
],
},
},
},
{
plugins: { vjf: vjf() },
options: {
validateOnChange: true,
validationDebounceWait: FORM_VALIDATION_DEBOUNCE_WAIT,
},
}
);
submit = () => {
const { spendingPassword } = this.form.values();
this.props.onConfirm(spendingPassword);
};
handleSubmitOnEnter = submitOnEnter.bind(this, this.submit);
render() {
const { form } = this;
const { intl } = this.context;
const {
transactionFee,
transactionFeeError,
transactionError,
isSubmitting,
onExternalLinkClick,
onClose,
onBack,
stepsList,
activeStep,
hwDeviceStatus,
selectedWallet,
isTrezor,
isHardwareWallet,
} = this.props;
const spendingPasswordField = form.$('spendingPassword');
const buttonLabel = intl.formatMessage(messages.continueButtonLabel);
const learnMoreLinkUrl = intl.formatMessage(messages.learntMoreLinkUrl);
const selectedWalletName = get(selectedWallet, 'name', '');
const actions = [
{
className: isSubmitting ? styles.isSubmitting : null,
label: buttonLabel,
onClick: this.submit,
disabled:
(!isHardwareWallet && !spendingPasswordField.isValid) ||
(isHardwareWallet &&
hwDeviceStatus !==
HwDeviceStatuses.VERIFYING_TRANSACTION_SUCCEEDED) ||
isSubmitting ||
!transactionFee,
primary: true,
},
];
return (
<VotingRegistrationDialog
onClose={!isSubmitting ? onClose : () => {}}
stepsList={stepsList}
activeStep={activeStep}
actions={actions}
onBack={onBack}
containerClassName={styles.component}
>
<p className={styles.description}>
<FormattedHTMLMessage
{...messages.description}
values={{ nextVotingFundNumber: NEXT_VOTING_FUND_NUMBER }}
/>
</p>
<div className={styles.learnMoreWrapper}>
<Link
className={styles.externalLink}
onClick={(event) => onExternalLinkClick(learnMoreLinkUrl, event)}
label={intl.formatMessage(messages.learnMoreLink)}
skin={LinkSkin}
/>
</div>
<div className={styles.feesWrapper}>
<p className={styles.feesLabel}>
{intl.formatMessage(messages.feesLabel)}
</p>
<p className={styles.feesAmount}>
{!transactionFee ? (
<span className={styles.calculatingFeesLabel}>
{intl.formatMessage(messages.calculatingFees)}
</span>
) : (
<>
<span>{formattedWalletAmount(transactionFee, false)}</span>
<span className={styles.feesAmountLabel}>
{intl.formatMessage(globalMessages.adaUnit)}
</span>
</>
)}
</p>
</div>
{isHardwareWallet ? (
<div className={styles.hardwareWalletStatusWrapper}>
<HardwareWalletStatus
hwDeviceStatus={hwDeviceStatus}
walletName={selectedWalletName}
isTrezor={isTrezor}
onExternalLinkClick={onExternalLinkClick}
/>
</div>
) : (
<Input
{...spendingPasswordField.bind()}
autoFocus
skin={InputSkin}
error={spendingPasswordField.error}
onKeyPress={this.handleSubmitOnEnter}
/>
)}
{transactionFeeError ? (
<div className={styles.errorMessage}>
<p>{transactionFeeError}</p>
</div>
) : null}
{transactionError ? (
<div className={styles.errorMessage}>
<p>{intl.formatMessage(transactionError)}</p>
</div>
) : null}
</VotingRegistrationDialog>
);
}
}