-
Notifications
You must be signed in to change notification settings - Fork 97
/
AddMemoDialog.js
152 lines (137 loc) · 4.22 KB
/
AddMemoDialog.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
// @flow
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import classnames from 'classnames';
import { defineMessages, intlShape } from 'react-intl';
import Dialog from '../../widgets/Dialog';
import DialogCloseButton from '../../widgets/DialogCloseButton';
import ErrorBlock from '../../widgets/ErrorBlock';
import ReactToolboxMobxForm from '../../../utils/ReactToolboxMobxForm';
import vjf from 'mobx-react-form/lib/validators/VJF';
import { Input } from 'react-polymorph/lib/components/Input';
import { InputOwnSkin } from '../../../themes/skins/InputOwnSkin';
import { isValidMemo } from '../../../utils/validations';
import globalMessages from '../../../i18n/global-messages';
import LocalizableError from '../../../i18n/LocalizableError';
import WalletTransaction from '../../../domain/WalletTransaction';
import config from '../../../config';
import styles from './MemoDialogCommon.scss';
const messages = defineMessages({
addMemoTitle: {
id: 'wallet.transaction.memo.add.dialog.title',
defaultMessage: '!!!Add memo',
},
addMemoInputLabel: {
id: 'wallet.transaction.memo.add.dialog.input.label',
defaultMessage: '!!!Memo',
},
addMemoInputHint: {
id: 'wallet.transaction.memo.add.dialog.input.hint',
defaultMessage: '!!!Memo (optional)',
},
addMemoActionsSubmit: {
id: 'wallet.transaction.memo.add.dialog.actions.submit',
defaultMessage: '!!!Add',
},
});
type Props = {|
selectedTransaction: WalletTransaction,
error: ?LocalizableError,
onCancel: Function,
onSubmit: Function,
classicTheme: boolean,
|};
type State = {
isSubmitting: boolean,
};
@observer
export default class AddMemoDialog extends Component<Props, State> {
static contextTypes = {
intl: intlShape.isRequired,
};
state = {
isSubmitting: false,
};
memoContentInput: Input;
form = new ReactToolboxMobxForm({
fields: {
memoContent: {
label: this.context.intl.formatMessage(messages.addMemoInputLabel),
placeholder: this.props.classicTheme ?
this.context.intl.formatMessage(messages.addMemoInputHint) : '',
value: '',
validators: [({ field }) => (
[
isValidMemo(field.value),
this.context.intl.formatMessage(globalMessages.invalidMemo)
]
)],
},
}
}, {
options: {
validateOnChange: true,
validationDebounceWait: config.forms.FORM_VALIDATION_DEBOUNCE_WAIT,
},
plugins: {
vjf: vjf()
},
});
submit = () => {
this.form.submit({
onSuccess: (form) => {
this.setState({ isSubmitting: true });
const { memoContent } = form.values();
const memoData = {
memo: memoContent.replace(/ +/g, ' '),
tx: this.props.selectedTransaction.id,
lastUpdated: new Date()
};
this.props.onSubmit(memoData);
},
onError: () => {
this.setState({ isSubmitting: false });
},
});
};
render() {
const { intl } = this.context;
const { form } = this;
const { memoContent } = form.values();
const { isSubmitting } = this.state;
const { error, onCancel, classicTheme } = this.props;
const disabledCondition = !(
isValidMemo(memoContent)
);
const actions = [
{
className: isSubmitting ? styles.isSubmitting : null,
label: this.context.intl.formatMessage(messages.addMemoActionsSubmit),
primary: true,
onClick: this.submit,
disabled: isSubmitting || disabledCondition
},
];
const memoContentField = form.$('memoContent');
return (
<Dialog
className={classnames([styles.component])}
title={intl.formatMessage(messages.addMemoTitle)}
actions={actions}
closeOnOverlayClick={false}
closeButton={<DialogCloseButton />}
onClose={onCancel}
classicTheme={classicTheme}
>
<Input
className={styles.memoContent}
inputRef={(input) => { this.memoContentInput = input; }}
{...memoContentField.bind()}
done={isValidMemo(memoContent)}
error={memoContentField.error}
skin={InputOwnSkin}
/>
{ error ? (<ErrorBlock error={error} />) : null }
</Dialog>);
}
}