Skip to content

Commit 71e30dc

Browse files
committed
feat: 🎸 add change wallet; fix NaN with new version of extensio
1 parent 6d46715 commit 71e30dc

File tree

6 files changed

+101
-41
lines changed

6 files changed

+101
-41
lines changed

src/pages/Resource/Resource.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class Resource extends Component {
4747
}
4848

4949
componentDidMount() {
50-
let httpProvider = DEFAUTRPCSERVER;
5150
getContractAddress().then(result => {
5251
this.setState({
5352
contracts: result
@@ -81,21 +80,8 @@ class Resource extends Component {
8180
NightElfCheck.getInstance()
8281
.check.then(item => {
8382
if (item) {
84-
nightElf = new window.NightElf.AElf({
85-
httpProvider: [
86-
httpProvider,
87-
null,
88-
null,
89-
null,
90-
[
91-
{
92-
name: 'Accept',
93-
value: 'text/plain;v=1.0'
94-
}
95-
]
96-
],
97-
appName // TODO: 这个需要content.js 主动获取
98-
});
83+
const nightElf = NightElfCheck.getAelfInstanceByExtension();
84+
9985
if (nightElf) {
10086
this.setState({
10187
nightElf

src/pages/Resource/components/ResourceAElfWallet/ResourceAElfWallet.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import {
1010
Col,
1111
Spin,
1212
Button,
13-
Icon
13+
Icon, message
1414
} from 'antd';
1515
import {ADDRESS_INFO} from '../../../../../config/config';
1616
import { SYMBOL, ELF_DECIMAL } from '@src/constants';
1717
import { thousandsCommaWithDecimal } from '@utils/formater';
18+
import { APPNAME } from '@config/config';
1819
import './ResourceAElfWallet.less';
20+
import NightElfCheck from "../../../../utils/NightElfCheck";
1921

2022
export default class ResourceAElfWallet extends PureComponent {
2123

@@ -34,6 +36,7 @@ export default class ResourceAElfWallet extends PureComponent {
3436
loading: false
3537
};
3638
this.refreshWalletInfo = this.refreshWalletInfo.bind(this);
39+
this.extensionLogout = this.extensionLogout.bind(this);
3740
}
3841

3942
componentDidMount() {
@@ -77,6 +80,26 @@ export default class ResourceAElfWallet extends PureComponent {
7780
}
7881
}
7982

83+
extensionLogout() {
84+
const nightElf = NightElfCheck.getAelfInstanceByExtension();
85+
const {
86+
currentWallet,
87+
} = this.props;
88+
nightElf.logout({
89+
appName: APPNAME,
90+
address: currentWallet.address
91+
}, (error, result) => {
92+
localStorage.removeItem('currentWallet');
93+
this.refreshWalletInfo();
94+
// TODO: more refactor actions for login and logout; repeated code, MyWalletCard.js
95+
message.success('Logout successful, refresh after 3s.', 3, () => {
96+
window.location.reload();
97+
});
98+
}).catch(error => {
99+
message.error('logout failed');
100+
});
101+
}
102+
80103
getCurrentWalletBalance = async () => {
81104
const {
82105
tokenContract,
@@ -159,13 +182,24 @@ export default class ResourceAElfWallet extends PureComponent {
159182
Transaction Details
160183
</Link> }
161184
</span>
162-
<Button
163-
className='resource-wallet-address-update update-btn'
185+
186+
<div>
187+
<Button
188+
className='resource-wallet-address-update update-btn'
189+
disabled={!(currentWallet && currentWallet.address && tokenContract)}
190+
onClick={this.refreshWalletInfo}
191+
>
192+
<Icon type='sync' spin={loading} />
193+
</Button>
194+
195+
{ currentWallet && currentWallet.name && <Button
196+
className="resource-wallet-address-update update-btn"
164197
disabled={!(currentWallet && currentWallet.address && tokenContract)}
165-
onClick={this.refreshWalletInfo}
166-
>
167-
<Icon type='sync' spin={loading} />
168-
</Button>
198+
onClick={this.extensionLogout}
199+
>
200+
change wallet<Icon type="logout"/>
201+
</Button>}
202+
</div>
169203
</div>
170204

171205
<div className='resource-wallet-info'>

src/pages/Vote/ElectionNotification/MyWalletCard/MyWalletCard.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { thousandsCommaWithDecimal } from '@utils/formater';
1616
import getCurrentWallet from '@utils/getCurrentWallet';
1717
import { ELF_DECIMAL, SYMBOL } from '@src/constants';
1818
import { APPNAME } from '@config/config';
19+
import NightElfCheck from "../../../../utils/NightElfCheck";
1920

2021
// @inject('contractsStore') @observer
2122
// todo: move the code fetch data on the upper component
@@ -38,6 +39,7 @@ export default class MyWalletCard extends PureComponent {
3839
// this.handleClaimDividendClick = this.handleClaimDividendClick.bind(this);
3940
// this.updateWallet = this.updateWallet.bind(this);
4041
this.handleUpdateWalletClick = this.handleUpdateWalletClick.bind(this);
42+
this.extensionLogout = this.extensionLogout.bind(this);
4143

4244
this.hasRun = false;
4345
}
@@ -249,6 +251,24 @@ export default class MyWalletCard extends PureComponent {
249251
});
250252
}
251253

254+
extensionLogout() {
255+
const nightElf = NightElfCheck.getAelfInstanceByExtension();
256+
const currentWallet = getCurrentWallet();
257+
nightElf.logout({
258+
appName: APPNAME,
259+
address: currentWallet.address
260+
}, (error, result) => {
261+
localStorage.removeItem('currentWallet');
262+
this.handleUpdateWalletClick();
263+
// TODO: more refactor actions for login and logout
264+
message.success('Logout successful, refresh after 3s.', 3, () => {
265+
window.location.reload();
266+
});
267+
}).catch(error => {
268+
message.error('logout failed');
269+
});
270+
}
271+
252272
render() {
253273
const { handleDividendClick, dividends } = this.props;
254274
const {
@@ -312,8 +332,16 @@ export default class MyWalletCard extends PureComponent {
312332
/>
313333
My Wallet
314334
</h2>
335+
{ currentWallet && currentWallet.name && <Button
336+
className="my-wallet-card-header-sync-btn update-btn"
337+
disabled={!(currentWallet && currentWallet.address)}
338+
onClick={this.extensionLogout}
339+
>
340+
change wallet<Icon type="logout"/>
341+
</Button>}
315342
<Button
316343
className="my-wallet-card-header-sync-btn update-btn"
344+
disabled={!(currentWallet && currentWallet.address)}
317345
onClick={this.handleUpdateWalletClick}
318346
>
319347
<Icon type="sync" spin={loading} />

src/pages/Vote/ElectionNotification/MyWalletCard/MyWalletCard.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
}
2828
&-sync-btn {
2929
float: right;
30+
color: @linkMain !important;
31+
&:hover {
32+
color: @linkHover;
33+
}
3034
}
3135
}
3236
&-body {

src/pages/Vote/Vote.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -390,26 +390,11 @@ class VoteContainer extends Component {
390390
}
391391

392392
getExtensionKeypairList() {
393-
const httpProvider = DEFAUT_RPC_SERVER;
394-
395393
NightElfCheck.getInstance()
396394
.check.then(item => {
397395
if (item) {
398-
const nightElf = new window.NightElf.AElf({
399-
httpProvider: [
400-
httpProvider,
401-
null,
402-
null,
403-
null,
404-
[
405-
{
406-
name: 'Accept',
407-
value: 'text/plain;v=1.0'
408-
}
409-
]
410-
],
411-
APPNAME // TODO: 这个需要content.js 主动获取
412-
});
396+
const nightElf = NightElfCheck.getAelfInstanceByExtension();
397+
413398
if (nightElf) {
414399
this.setState({
415400
nightElf
@@ -1304,7 +1289,7 @@ class VoteContainer extends Component {
13041289
if (profitItem.error) {
13051290
amount = 0;
13061291
} else {
1307-
amount = +profitItem.value;
1292+
amount = isNaN(+profitItem.value) ? 0 : +profitItem.value;
13081293
}
13091294
// todo: remove the judge when need
13101295
amount = amount === undefined ? 0 : +amount / ELF_DECIMAL;

src/utils/NightElfCheck.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
* @file NightElfCheck
33
* @author zhouminghui
44
*/
5+
import config, {
6+
DEFAUTRPCSERVER,
7+
APPNAME,
8+
} from '@config/config';
59

610
let nightElfInstance = null;
11+
let aelfInstanceByExtension = null;
712
export default class NightElfCheck {
813
constructor() {
914
let resovleTemp = null;
@@ -31,4 +36,22 @@ export default class NightElfCheck {
3136
}
3237
return nightElfInstance;
3338
}
39+
40+
// For extension users
41+
static getAelfInstanceByExtension() {
42+
if (!aelfInstanceByExtension) {
43+
NightElfCheck.initAelfInstanceByExtension();
44+
}
45+
return aelfInstanceByExtension;
46+
}
47+
48+
static initAelfInstanceByExtension() {
49+
aelfInstanceByExtension = new window.NightElf.AElf({
50+
httpProvider: [
51+
DEFAUTRPCSERVER
52+
],
53+
APPNAME
54+
});
55+
return aelfInstanceByExtension;
56+
}
3457
}

0 commit comments

Comments
 (0)