-
Notifications
You must be signed in to change notification settings - Fork 569
/
QrcodeModal.jsx
160 lines (151 loc) · 6.01 KB
/
QrcodeModal.jsx
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
import React from "react";
import PropTypes from "prop-types";
import ZfApi from "react-foundation-apps/src/utils/foundation-api";
import BaseModal from "./BaseModal";
import Translate from "react-translate-component";
import QRCode from "qrcode.react";
import {Aes} from "bitsharesjs";
class QrcodeModal extends React.Component {
constructor(props) {
super(props);
this.state = this._getInitialState();
this.onPasswordEnter = this.onPasswordEnter.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.onCancel = this.onCancel.bind(this);
this.onClose = this.onClose.bind(this);
}
_getInitialState() {
return {
isShowQrcode: false,
keyString: null
};
}
show() {
ZfApi.publish(this.props.modalId, "open");
}
onCancel() {
ZfApi.publish(this.props.modalId, "close");
this.onClose();
}
onClose() {
if (this.refs.password_input) this.refs.password_input.value = "";
this.setState(this._getInitialState());
}
onPasswordEnter(e) {
e.preventDefault();
let pwd = this.refs.password_input.value;
let key = this.props.keyValue;
if (pwd != null && pwd != "") {
if (key !== undefined && key != null && key != "") {
let pwd_aes = Aes.fromSeed(pwd);
let qrkey = pwd_aes.encryptToHex(key);
this.setState({isShowQrcode: true, keyString: qrkey});
}
} else {
//notify.error("You'd better enter a password to encrypt the qr code");
this.setState({isShowQrcode: true, keyString: key});
}
}
onKeyDown(e) {
if (e.keyCode === 13) this.onPasswordEnter(e);
}
render() {
let pos = null;
if (this.state.isShowQrcode) pos = {textAlign: "center"};
return (
<BaseModal
onClose={this.onClose}
id={this.props.modalId}
ref="modal"
overlay={true}
overlayClose={false}
>
<div className="text-center">
<div style={{margin: "1.5rem 0"}}>
<Translate
component="h4"
content="modal.qrcode.title"
/>
</div>
<form
className="full-width"
style={{margin: "0 3.5rem"}}
onSubmit={this.onPasswordEnter}
noValidate
>
<div className="form-group">
{this.state.isShowQrcode ? (
<section style={pos}>
<span
style={{
background: "#fff",
padding: ".75rem",
display: "inline-block"
}}
>
<QRCode
size={256}
value={this.state.keyString}
/>
</span>
</section>
) : (
<section>
<label className="left-label">
<Translate
unsafe
content="modal.qrcode.input_message"
/>
</label>
<input
name="password"
type="text"
onFocus={() => {
this.refs.password_input.setAttribute(
"type",
"password"
);
}}
ref="password_input"
autoComplete="off"
onKeyDown={this.onKeyDown}
/>
</section>
)}
</div>
<div style={pos}>
<div className="button-group">
{this.state.isShowQrcode == false ? (
<button
className="button"
data-place="bottom"
data-html
onClick={this.onPasswordEnter}
>
<Translate content="modal.ok" />
</button>
) : null}
<button
className="button primary hollow"
data-place="bottom"
data-html
onClick={this.onCancel}
>
<Translate content="cancel" />
</button>
</div>
</div>
</form>
</div>
</BaseModal>
);
}
}
QrcodeModal.propTypes = {
modalId: PropTypes.string.isRequired,
keyValue: PropTypes.string
};
QrcodeModal.defaultProps = {
modalId: "qr_code_password_modal"
};
export default QrcodeModal;