-
Notifications
You must be signed in to change notification settings - Fork 295
/
ChangeSpendingPasswordDialog.tsx
340 lines (320 loc) · 11.2 KB
/
ChangeSpendingPasswordDialog.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
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
// @flow
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import classnames from 'classnames';
import { Input } from 'react-polymorph/lib/components/Input';
import { defineMessages, FormattedHTMLMessage, intlShape } from 'react-intl';
import vjf from 'mobx-react-form/lib/validators/VJF';
import { PopOver } from 'react-polymorph/lib/components/PopOver';
import SVGInline from 'react-svg-inline';
import ReactToolboxMobxForm from '../../../utils/ReactToolboxMobxForm';
import DialogCloseButton from '../../widgets/DialogCloseButton';
import Dialog from '../../widgets/Dialog';
import {
isValidSpendingPassword,
isValidRepeatPassword,
} from '../../../utils/validations';
import globalMessages from '../../../i18n/global-messages';
import LocalizableError from '../../../i18n/LocalizableError';
import { PasswordInput } from '../../widgets/forms/PasswordInput';
import styles from './ChangeSpendingPasswordDialog.scss';
import { FORM_VALIDATION_DEBOUNCE_WAIT } from '../../../config/timingConfig';
import { submitOnEnter } from '../../../utils/form';
import infoIconInline from '../../../assets/images/info-icon.inline.svg';
const messages = defineMessages({
dialogTitleSetPassword: {
id: 'wallet.settings.changePassword.dialog.title.setPassword',
defaultMessage: '!!!Set a password for {walletName} wallet',
description:
'Title for the "Change wallet password" dialog when there is no password set.',
},
dialogTitleChangePassword: {
id: 'wallet.settings.changePassword.dialog.title.changePassword',
defaultMessage: '!!!Change password',
description:
'Title for the "Change wallet password" dialog when there is already password set.',
},
spendingPasswordLabel: {
id: 'wallet.settings.changePassword.dialog.spendingPasswordLabel',
defaultMessage: '!!!Spending password',
description:
'Label for the "Spending password" input in the change wallet password dialog.',
},
currentPasswordLabel: {
id: 'wallet.settings.changePassword.dialog.currentPasswordLabel',
defaultMessage: '!!!Current password',
description:
'Label for the "Current password" input in the change wallet password dialog.',
},
newPasswordLabel: {
id: 'wallet.settings.changePassword.dialog.newPasswordLabel',
defaultMessage: '!!!New password',
description:
'Label for the "New password" input in the change wallet password dialog.',
},
repeatPasswordLabel: {
id: 'wallet.settings.changePassword.dialog.repeatPasswordLabel',
defaultMessage: '!!!Repeat password',
description:
'Label for the "Repeat password" input in the change wallet password dialog.',
},
currentPasswordFieldPlaceholder: {
id: 'wallet.settings.changePassword.dialog.currentPasswordFieldPlaceholder',
defaultMessage: '!!!Type current password',
description:
'Placeholder for the "Current password" inputs in the change wallet password dialog.',
},
newPasswordFieldPlaceholder: {
id: 'wallet.settings.changePassword.dialog.newPasswordFieldPlaceholder',
defaultMessage: '!!!Type new password',
description:
'Placeholder for the "New password" inputs in the change wallet password dialog.',
},
repeatPasswordFieldPlaceholder: {
id: 'wallet.settings.changePassword.dialog.repeatPasswordFieldPlaceholder',
defaultMessage: '!!!Repeat new password',
description:
'Placeholder for the "Repeat password" inputs in the change wallet password dialog.',
},
passwordTooltip: {
id: 'wallet.dialog.passwordTooltip',
defaultMessage:
'We recommend using a password manager app to manage and store your spending password. Generate a unique password using a password manager and paste it here. Passwords should never be reused.',
description: 'Tooltip for the password input in the wallet dialog.',
},
});
type Props = {
currentPasswordValue: string,
newPasswordValue: string,
repeatedPasswordValue: string,
onSave: Function,
onCancel: Function,
onDataChange: Function,
isSubmitting: boolean,
error: ?LocalizableError,
isSpendingPasswordSet: boolean,
walletName: string,
currentLocale: string,
};
@observer
export default class ChangeSpendingPasswordDialog extends Component<Props> {
static defaultProps = {
currentPasswordValue: '',
newPasswordValue: '',
repeatedPasswordValue: '',
};
static contextTypes = {
intl: intlShape.isRequired,
};
form = new ReactToolboxMobxForm(
{
fields: {
currentPassword: {
type: 'password',
label: this.context.intl.formatMessage(messages.currentPasswordLabel),
placeholder: this.context.intl.formatMessage(
messages.currentPasswordFieldPlaceholder
),
value: '',
validators: [
({ form }) => {
if (this.props.isSpendingPasswordSet) {
const currentPasswordField = form.$('currentPassword');
return [
currentPasswordField.value.length > 0,
this.context.intl.formatMessage(
globalMessages.invalidSpendingPassword
),
];
}
return [true];
},
],
},
spendingPassword: {
type: 'password',
label: this.context.intl.formatMessage(
messages[
this.props.isSpendingPasswordSet
? 'newPasswordLabel'
: 'spendingPasswordLabel'
]
),
placeholder: this.context.intl.formatMessage(
messages.newPasswordFieldPlaceholder
),
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.repeatPasswordFieldPlaceholder
),
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,
showErrorsOnClear: true,
},
}
);
submit = () => {
this.form.submit({
onSuccess: (form) => {
const { currentPassword, spendingPassword } = form.values();
const passwordData = {
oldPassword: currentPassword,
newPassword: spendingPassword,
};
this.props.onSave(passwordData);
},
onError: () => {},
});
};
handleSubmitOnEnter = submitOnEnter.bind(this, this.submit);
handleDataChange = (key: string, value: string) => {
this.props.onDataChange({ [key]: value });
};
render() {
const { form } = this;
const { intl } = this.context;
const {
onCancel,
isSubmitting,
error,
isSpendingPasswordSet,
walletName,
currentLocale,
} = this.props;
const dialogClasses = classnames([
styles.dialog,
isSpendingPasswordSet ? 'changePasswordDialog' : 'createPasswordDialog',
]);
const confirmButtonClasses = classnames([
'confirmButton',
isSubmitting ? styles.isSubmitting : null,
]);
const spendingPasswordClasses = classnames([
styles.spendingPasswordField,
currentLocale === 'ja-JP' ? styles.jpLangTooltipIcon : '',
]);
const newPasswordClasses = classnames(['newPassword', styles.newPassword]);
const currentPasswordField = form.$('currentPassword');
const newPasswordField = form.$('spendingPassword');
const repeatedPasswordField = form.$('repeatPassword');
const canSubmit = !isSubmitting && form.isValid;
const currentPasswordError =
canSubmit && error && error.code === 'wrong_encryption_passphrase';
const actions = [
{
className: confirmButtonClasses,
disabled: !canSubmit,
label: intl.formatMessage(globalMessages.save),
onClick: this.submit,
primary: true,
},
];
return (
<Dialog
title={intl.formatMessage(
messages[
!isSpendingPasswordSet
? 'dialogTitleSetPassword'
: 'dialogTitleChangePassword'
],
{ walletName }
)}
subtitle={walletName}
actions={actions}
closeOnOverlayClick
onClose={!isSubmitting ? onCancel : () => {}}
className={dialogClasses}
closeButton={<DialogCloseButton onClose={onCancel} />}
>
<div className={styles.spendingPasswordFields}>
{isSpendingPasswordSet && (
<div className={spendingPasswordClasses}>
<Input
className={styles.currentPassword}
error={currentPasswordField.error || currentPasswordError}
{...currentPasswordField.bind()}
onKeyPress={this.handleSubmitOnEnter}
/>
<PopOver
content={<FormattedHTMLMessage {...messages.passwordTooltip} />}
key="tooltip"
>
<SVGInline svg={infoIconInline} className={styles.infoIcon} />
</PopOver>
</div>
)}
<div className={newPasswordClasses}>
<div className={spendingPasswordClasses}>
<PasswordInput
{...newPasswordField.bind()}
onKeyPress={this.handleSubmitOnEnter}
/>
{!isSpendingPasswordSet && (
<PopOver
content={
<FormattedHTMLMessage {...messages.passwordTooltip} />
}
key="tooltip"
>
<SVGInline svg={infoIconInline} className={styles.infoIcon} />
</PopOver>
)}
</div>
</div>
<div className={styles.repeatedPassword}>
<PasswordInput
{...repeatedPasswordField.bind()}
onKeyPress={this.handleSubmitOnEnter}
repeatPassword={newPasswordField.value}
isPasswordRepeat
/>
</div>
<p className={styles.passwordInstructions}>
<FormattedHTMLMessage {...globalMessages.passwordInstructions} />
</p>
</div>
{error ? (
<p className={styles.error}>{intl.formatMessage(error)}</p>
) : null}
</Dialog>
);
}
}