-
Notifications
You must be signed in to change notification settings - Fork 295
/
WalletFileImportDialog.tsx
270 lines (257 loc) · 8.85 KB
/
WalletFileImportDialog.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
// @flow
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import classnames from 'classnames';
import { defineMessages, intlShape } from 'react-intl';
import vjf from 'mobx-react-form/lib/validators/VJF';
// import { Input } from 'react-polymorph/lib/components/Input';
// import { InputSkin } from 'react-polymorph/lib/skins/simple/InputSkin';
import DialogCloseButton from '../../widgets/DialogCloseButton';
import Dialog from '../../widgets/Dialog';
import ReactToolboxMobxForm from '../../../utils/ReactToolboxMobxForm';
import FileUploadWidget from '../../widgets/forms/FileUploadWidget';
import {
isValidWalletName,
// isValidSpendingPassword,
// isValidRepeatPassword,
} from '../../../utils/validations';
import globalMessages from '../../../i18n/global-messages';
import LocalizableError from '../../../i18n/LocalizableError';
import styles from './WalletFileImportDialog.scss';
import { FORM_VALIDATION_DEBOUNCE_WAIT } from '../../../config/timingConfig';
const messages = defineMessages({
headline: {
id: 'wallet.file.import.dialog.headline',
defaultMessage: '!!!Import Wallet',
description: 'headline for "Import wallet from file" dialog.',
},
walletFileLabel: {
id: 'wallet.file.import.dialog.walletFileLabel',
defaultMessage: '!!!Import file',
description:
'Label "Import file" on the dialog for importing a wallet from a file.',
},
walletFileHint: {
id: 'wallet.file.import.dialog.walletFileHint',
defaultMessage: '!!!Drop file here or click to choose',
description:
'Hint for the file upload field on the dialog for importing a wallet from a file.',
},
walletNameInputLabel: {
id: 'wallet.file.import.dialog.wallet.name.input.label',
defaultMessage: '!!!Wallet name',
description:
'Label for the "wallet name" input in the wallet file import dialog.',
},
walletNameInputHint: {
id: 'wallet.file.import.dialog.wallet.name.input.hint',
defaultMessage: '!!!e.g: Shopping Wallet',
description: 'Hint for the "Wallet name" in the wallet file import dialog.',
},
submitLabel: {
id: 'wallet.file.import.dialog.submitLabel',
defaultMessage: '!!!Import wallet',
description:
'Label "Import wallet" submit button on the dialog for importing a wallet from a file.',
},
spendingPasswordLabel: {
id: 'wallet.file.import.dialog.spendingPasswordLabel',
defaultMessage: '!!!Wallet password',
description:
'Label for the "Wallet password" input in the wallet file import dialog.',
},
repeatPasswordLabel: {
id: 'wallet.file.import.dialog.repeatPasswordLabel',
defaultMessage: '!!!Repeat password',
description:
'Label for the "Repeat password" input in the wallet file import dialog.',
},
passwordFieldPlaceholder: {
id: 'wallet.file.import.dialog.passwordFieldPlaceholder',
defaultMessage: '!!!Password',
description:
'Placeholder for the "Password" inputs in the wallet file import dialog.',
},
});
type Props = {
onSubmit: Function,
onClose: Function,
isSubmitting: boolean,
error: ?LocalizableError,
};
@observer
export default class WalletFileImportDialog extends Component<Props> {
static contextTypes = {
intl: intlShape.isRequired,
};
form = new ReactToolboxMobxForm(
{
fields: {
walletFilePath: {
label: 'filePath',
placeholder: 'filePath',
type: 'hidden',
},
walletName: {
label: this.context.intl.formatMessage(messages.walletNameInputLabel),
placeholder: this.context.intl.formatMessage(
messages.walletNameInputHint
),
value: '',
validators: [
({ field }) => {
if (field.value.length === 0) return [true];
return [
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: [
() => {
// const repeatPasswordField = form.$('repeatPassword');
// if (repeatPasswordField.value.length > 0) {
// repeatPasswordField.validate({ showErrors: true });
// }
// return [
// isValidSpendingPassword(field.value),
// this.context.intl.formatMessage(
// globalMessages.invalidSpendingPassword
// ),
// ];
return [true]; // @API TODO - missing API v2 endpoint and password declaration
},
],
},
repeatPassword: {
type: 'password',
label: this.context.intl.formatMessage(messages.repeatPasswordLabel),
placeholder: this.context.intl.formatMessage(
messages.passwordFieldPlaceholder
),
value: '',
validators: [
() => {
// const spendingPassword = form.$('spendingPassword').value;
// if (spendingPassword.length === 0) return [true];
// return [
// isValidRepeatPassword(spendingPassword, field.value),
// this.context.intl.formatMessage(
// globalMessages.invalidRepeatPassword
// ),
// ];
return [true]; // @API TODO - missing API v2 endpoint and password declaration
},
],
},
},
},
{
plugins: { vjf: vjf() },
options: {
validateOnChange: true,
validationDebounceWait: FORM_VALIDATION_DEBOUNCE_WAIT,
},
}
);
submit = () => {
this.form.submit({
onSuccess: (form) => {
const { walletFilePath, spendingPassword, walletName } = form.values();
const walletData = {
filePath: walletFilePath,
spendingPassword,
walletName: walletName.length > 0 ? walletName : null,
};
this.props.onSubmit(walletData);
},
onError: () => {},
});
};
render() {
const { intl } = this.context;
const { form } = this;
const { isSubmitting, error, onClose } = this.props;
const walletFilePath = form.$('walletFilePath');
const dialogClasses = classnames([
styles.component,
'WalletFileImportDialog',
]);
const actions = [
{
className: isSubmitting ? styles.isSubmitting : null,
label: intl.formatMessage(messages.submitLabel),
primary: true,
disabled: isSubmitting || !walletFilePath.value,
onClick: this.submit,
},
];
// const walletNameField = form.$('walletName');
// const spendingPasswordField = form.$('spendingPassword');
// const repeatedPasswordField = form.$('repeatPassword');
return (
<Dialog
className={dialogClasses}
title={intl.formatMessage(messages.headline)}
actions={actions}
closeOnOverlayClick
onClose={onClose}
closeButton={<DialogCloseButton />}
>
<div className={styles.fileUpload}>
<FileUploadWidget
label={intl.formatMessage(messages.walletFileLabel)}
placeholder={intl.formatMessage(messages.walletFileHint)}
acceptedFileTypes={['*']}
selectedFile={walletFilePath.value}
onFileSelected={(filePath) => {
// "set(value)" is an unbound method and thus must be explicitly called
walletFilePath.set(filePath);
}}
/>
</div>
{/* TODO: re-enable when wallet-name
support is added to the API endpoint
<Input
className="walletName"
{...walletNameField.bind()}
error={walletNameField.error}
skin={InputSkin}
/>
<div className={styles.spendingPassword}>
<div className={styles.spendingPasswordFields}>
<Input
className="spendingPassword"
{...spendingPasswordField.bind()}
error={spendingPasswordField.error}
skin={InputSkin}
/>
<Input
className="repeatedPassword"
{...repeatedPasswordField.bind()}
error={repeatedPasswordField.error}
skin={InputSkin}
/>
<p className={styles.passwordInstructions}>
{intl.formatMessage(globalMessages.passwordInstructions)}
</p>
</div>
</div>
*/}
{error && <p className={styles.error}>{intl.formatMessage(error)}</p>}
</Dialog>
);
}
}