-
Notifications
You must be signed in to change notification settings - Fork 295
/
WalletCreateDialog.js
270 lines (252 loc) · 8.38 KB
/
WalletCreateDialog.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
// @flow
// TODO: Remove once the new wallet creation process is ready
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import classnames from 'classnames';
import { Input } from 'react-polymorph/lib/components/Input';
import { InputSkin } from 'react-polymorph/lib/skins/simple/InputSkin';
import { defineMessages, intlShape, FormattedHTMLMessage } from 'react-intl';
import vjf from 'mobx-react-form/lib/validators/VJF';
import ReactToolboxMobxForm, {
handleFormErrors,
} from '../../utils/ReactToolboxMobxForm';
import DialogCloseButton from '../widgets/DialogCloseButton';
import Dialog from '../widgets/Dialog';
import {
isValidWalletName,
isValidSpendingPassword,
isValidRepeatPassword,
} from '../../utils/validations';
import globalMessages from '../../i18n/global-messages';
import { PasswordInput } from '../widgets/forms/PasswordInput';
import styles from './WalletCreateDialog.scss';
import { FORM_VALIDATION_DEBOUNCE_WAIT } from '../../config/timingConfig';
import { submitOnEnter } from '../../utils/form';
const messages = defineMessages({
dialogTitle: {
id: 'wallet.create.dialog.title',
defaultMessage: '!!!Create a new wallet',
description: 'Title "Create a new wallet" in the wallet create form.',
},
walletName: {
id: 'wallet.create.dialog.name.label',
defaultMessage: '!!!Wallet name',
description:
'Label for the "Wallet Name" text input in the wallet create form.',
},
walletNameHint: {
id: 'wallet.create.dialog.walletNameHint',
defaultMessage: '!!!Enter wallet name',
description:
'Hint for the "Wallet Name" text input in the wallet create form.',
},
createPersonalWallet: {
id: 'wallet.create.dialog.create.personal.wallet.button.label',
defaultMessage: '!!!Create Shelley wallet',
description:
'Label for the "Create Shelley wallet" button on create wallet dialog.',
},
passwordSectionLabel: {
id: 'wallet.create.dialog.passwordSectionLabel',
defaultMessage: '!!!Spending password',
description: 'Password creation label.',
},
passwordSectionDescription: {
id: 'wallet.create.dialog.passwordSectionDescription',
defaultMessage: '!!!Keep your wallet secure by setting a spending password',
description: 'Password creation description.',
},
spendingPasswordLabel: {
id: 'wallet.create.dialog.spendingPasswordLabel',
defaultMessage: '!!!Enter password',
description:
'Label for the "Wallet password" input in the create wallet dialog.',
},
repeatPasswordLabel: {
id: 'wallet.create.dialog.repeatPasswordLabel',
defaultMessage: '!!!Repeat password',
description:
'Label for the "Repeat password" input in the create wallet dialog.',
},
passwordFieldPlaceholder: {
id: 'wallet.create.dialog.passwordFieldPlaceholder',
defaultMessage: '!!!Password',
description:
'Placeholder for the "Password" inputs in the create wallet dialog.',
},
});
type Props = {
onSubmit: Function,
onCancel: Function,
};
type State = {
isSubmitting: boolean,
};
@observer
export default class WalletCreateDialog extends Component<Props, State> {
static contextTypes = {
intl: intlShape.isRequired,
};
state = {
isSubmitting: false,
};
componentDidMount() {
setTimeout(() => {
this.walletNameInput.focus();
});
}
walletNameInput: Input;
form = new ReactToolboxMobxForm(
{
fields: {
walletName: {
label: this.context.intl.formatMessage(messages.walletName),
placeholder: this.context.intl.formatMessage(messages.walletNameHint),
value: '',
validators: [
({ field }) => [
isValidWalletName(field.value),
this.context.intl.formatMessage(globalMessages.invalidWalletName),
],
],
},
spendingPassword: {
type: 'password',
label: this.context.intl.formatMessage(
messages.spendingPasswordLabel
),
placeholder: this.context.intl.formatMessage(
messages.passwordFieldPlaceholder
),
value: '',
validators: [
({ field, form }) => {
const repeatPasswordField = form.$('repeatPassword');
const isRepeatPasswordFieldSet =
repeatPasswordField.value.length > 0;
repeatPasswordField.validate({
showErrors: isRepeatPasswordFieldSet,
});
return [
isValidSpendingPassword(field.value),
this.context.intl.formatMessage(
globalMessages.invalidSpendingPassword
),
];
},
],
},
repeatPassword: {
type: 'password',
label: this.context.intl.formatMessage(messages.repeatPasswordLabel),
placeholder: this.context.intl.formatMessage(
messages.passwordFieldPlaceholder
),
value: '',
validators: [
({ field, form }) => {
const spendingPassword = form.$('spendingPassword').value;
return [
isValidRepeatPassword(spendingPassword, field.value),
this.context.intl.formatMessage(
globalMessages.invalidRepeatPassword
),
];
},
],
},
},
},
{
plugins: { vjf: vjf() },
options: {
validateOnChange: true,
validationDebounceWait: FORM_VALIDATION_DEBOUNCE_WAIT,
},
}
);
submit = () => {
this.form.submit({
onSuccess: form => {
this.setState({ isSubmitting: true });
const { walletName, spendingPassword } = form.values();
const walletData = {
name: walletName,
spendingPassword,
};
this.props.onSubmit(walletData);
},
onError: () => {
handleFormErrors('.SimpleFormField_error', { focusElement: true });
this.setState({ isSubmitting: false });
},
});
};
handleSubmitOnEnter = submitOnEnter.bind(this, this.submit);
render() {
const { form } = this;
const { intl } = this.context;
const { onCancel } = this.props;
const { isSubmitting } = this.state;
const dialogClasses = classnames([styles.component, 'WalletCreateDialog']);
const walletNameField = form.$('walletName');
const spendingPasswordField = form.$('spendingPassword');
const repeatedPasswordField = form.$('repeatPassword');
const canSubmit = !isSubmitting && form.isValid;
const actions = [
{
className: isSubmitting ? styles.isSubmitting : null,
disabled: !canSubmit,
label: this.context.intl.formatMessage(messages.createPersonalWallet),
primary: true,
onClick: this.submit,
},
];
return (
<Dialog
className={dialogClasses}
title={intl.formatMessage(messages.dialogTitle)}
actions={actions}
closeOnOverlayClick
onClose={!isSubmitting ? onCancel : () => {}}
closeButton={<DialogCloseButton />}
>
<Input
className="walletName"
onKeyPress={this.handleSubmitOnEnter}
ref={input => {
this.walletNameInput = input;
}}
{...walletNameField.bind()}
error={walletNameField.error}
skin={InputSkin}
/>
<div className={styles.spendingPasswordWrapper}>
<div className={styles.passwordSectionLabel}>
{intl.formatMessage(messages.passwordSectionLabel)}
</div>
<div className={styles.passwordSectionDescription}>
{intl.formatMessage(messages.passwordSectionDescription)}
</div>
<div className={styles.spendingPasswordFields}>
<PasswordInput
className="spendingPassword"
onKeyPress={this.handleSubmitOnEnter}
{...spendingPasswordField.bind()}
/>
<PasswordInput
className="repeatedPassword"
onKeyPress={this.handleSubmitOnEnter}
{...repeatedPasswordField.bind()}
repeatPassword={spendingPasswordField.value}
isPasswordRepeat
/>
<p className={styles.passwordInstructions}>
<FormattedHTMLMessage {...globalMessages.passwordInstructions} />
</p>
</div>
</div>
</Dialog>
);
}
}