Skip to content

Commit 4274411

Browse files
author
Gilyoung
committed
Clear stored data when logout
1 parent 12da571 commit 4274411

File tree

10 files changed

+145
-77
lines changed

10 files changed

+145
-77
lines changed
Lines changed: 89 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,105 @@
11
import * as React from "react";
2+
import { connect } from "react-redux";
23
import { RouteComponentProps, withRouter } from "react-router-dom";
3-
import { AddressType, WalletAddress } from "../../../model/address";
4+
import { Action } from "redux";
5+
import { ThunkDispatch } from "redux-thunk";
6+
import {
7+
AddressType,
8+
PlatformAccount,
9+
WalletAddress
10+
} from "../../../model/address";
11+
import { ReducerConfigure } from "../../../redux";
12+
import walletActions from "../../../redux/wallet/walletActions";
413
import { getNetworkNameById } from "../../../utils/network";
14+
import { changeQuarkToCCCString } from "../../../utils/unit";
515
import "./AddressItem.css";
616

717
interface OwnProps {
8-
address: WalletAddress;
18+
walletAddress: WalletAddress;
919
className?: string | null;
20+
account?: PlatformAccount | null;
21+
}
22+
interface DispatchProps {
23+
fetchAccountIfNeed: (address: string) => void;
1024
}
1125

12-
type Props = RouteComponentProps & OwnProps;
26+
type Props = RouteComponentProps & OwnProps & DispatchProps;
1327

14-
const AddressItem = (props: Props) => {
15-
const { address, className } = props;
16-
const handleClick = () => {
17-
if (address.type === AddressType.Platform) {
18-
props.history.push(`/${address.address}/account`);
19-
} else {
20-
props.history.push(`/${address.address}/assets`);
21-
}
22-
};
23-
return (
24-
<div className={`Address-item mb-3 ${className}`} onClick={handleClick}>
28+
class AddressItem extends React.Component<Props> {
29+
public componentDidMount() {
30+
const { walletAddress, fetchAccountIfNeed } = this.props;
31+
fetchAccountIfNeed(walletAddress.address);
32+
}
33+
public render() {
34+
const { walletAddress, className, account } = this.props;
35+
return (
2536
<div
26-
className={`item-body ${
27-
address.type === AddressType.Platform
28-
? "platform-type"
29-
: "asset-type"
30-
}`}
37+
className={`Address-item mb-3 ${className}`}
38+
onClick={this.handleClick}
3139
>
32-
<div className="d-flex network-text-container">
33-
<span className="network-text ml-auto">
34-
{getNetworkNameById(address.networkId)}
40+
<div
41+
className={`item-body ${
42+
walletAddress.type === AddressType.Platform
43+
? "platform-type"
44+
: "asset-type"
45+
}`}
46+
>
47+
<div className="d-flex network-text-container">
48+
<span className="network-text ml-auto">
49+
{getNetworkNameById(walletAddress.networkId)}
50+
</span>
51+
</div>
52+
<div>
53+
<p className="address-name mb-0 mono">
54+
{walletAddress.name}
55+
</p>
56+
</div>
57+
<span className="address-text mono">
58+
{walletAddress.address.slice(0, 15)}
59+
...
60+
{walletAddress.address.slice(
61+
walletAddress.address.length - 15,
62+
walletAddress.address.length
63+
)}
3564
</span>
3665
</div>
37-
<div>
38-
<p className="address-name mb-0 mono">{address.name}</p>
39-
</div>
40-
<span className="address-text mono">
41-
{address.address.slice(0, 15)}
42-
...
43-
{address.address.slice(
44-
address.address.length - 15,
45-
address.address.length
46-
)}
47-
</span>
66+
{walletAddress.type === AddressType.Platform && (
67+
<div className="platform-account">
68+
{account && (
69+
<span className="mono balance">
70+
{changeQuarkToCCCString(account.balance)} CCC
71+
</span>
72+
)}
73+
</div>
74+
)}
4875
</div>
49-
{address.type === AddressType.Platform && (
50-
<div className="platform-account">
51-
<span className="mono balance">873.4311 CCC</span>
52-
</div>
53-
)}
54-
</div>
55-
);
56-
};
76+
);
77+
}
78+
private handleClick = () => {
79+
const { walletAddress, history } = this.props;
80+
if (walletAddress.type === AddressType.Platform) {
81+
history.push(`/${walletAddress.address}/account`);
82+
} else {
83+
history.push(`/${walletAddress.address}/assets`);
84+
}
85+
};
86+
}
5787

58-
export default withRouter(AddressItem);
88+
const mapStateToProps = (state: ReducerConfigure, props: OwnProps) => {
89+
const { walletAddress } = props;
90+
const account = state.walletReducer.accounts[walletAddress.address];
91+
return {
92+
account: account && account.data
93+
};
94+
};
95+
const mapDispatchToProps = (
96+
dispatch: ThunkDispatch<ReducerConfigure, void, Action>
97+
) => ({
98+
fetchAccountIfNeed: (address: string) => {
99+
dispatch(walletActions.fetchAccountIfNeed(address));
100+
}
101+
});
102+
export default connect(
103+
mapStateToProps,
104+
mapDispatchToProps
105+
)(withRouter(AddressItem));

src/components/AddressList/AddressList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AddressList extends React.Component<Props, any> {
4141
) => (
4242
<AddressItem
4343
key={index}
44-
address={dummyAddress}
44+
walletAddress={dummyAddress}
4545
/>
4646
)
4747
)}
@@ -60,7 +60,7 @@ class AddressList extends React.Component<Props, any> {
6060
) => (
6161
<AddressItem
6262
key={index}
63-
address={dummyAddress}
63+
walletAddress={dummyAddress}
6464
/>
6565
)
6666
)}

src/components/SideMenu/SideMenu.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import "./SideMenu.css";
1111
interface StateProps {
1212
isAuthenticated: boolean;
1313
walletName?: string | null;
14-
assetAddresses: WalletAddress[];
15-
platformAddresses: WalletAddress[];
14+
assetAddresses?: WalletAddress[] | null;
15+
platformAddresses?: WalletAddress[] | null;
1616
}
1717
interface DispatchProps {
1818
logout: () => void;
@@ -41,10 +41,11 @@ class SideMenu extends React.Component<Props, any> {
4141
<h3 className="mono mb-4">{walletName}</h3>
4242
<hr />
4343
<p className="mb-0 mono grey">
44-
{platformAddresses.length} Platform address
44+
{platformAddresses && platformAddresses.length} Platform
45+
address
4546
</p>
4647
<p className="mono grey">
47-
{assetAddresses.length} Asset address
48+
{assetAddresses && assetAddresses.length} Asset address
4849
</p>
4950
<div className="mt-5">
5051
<button className="btn btn-primary w-100 text-center">

src/redux/asset/assetReducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ export interface AssetState {
4141
};
4242
}
4343

44-
const initialState: AssetState = {
44+
export const assetInitState: AssetState = {
4545
assetScheme: {},
4646
aggsUTXOList: {},
4747
UTXOList: {},
4848
availableAssets: {}
4949
};
5050

51-
export const assetReducer = (state = initialState, action: Action) => {
51+
export const assetReducer = (state = assetInitState, action: Action) => {
5252
switch (action.type) {
5353
case ActionType.CacheAssetScheme: {
5454
const assetType = action.data.assetType;

src/redux/global/globalActions.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ import { ThunkDispatch } from "redux-thunk";
22
import { ReducerConfigure } from "..";
33
import { clearWallet } from "../../model/wallet";
44

5-
export type Action = Login | Logout | ToggleMenu;
5+
export type Action = Login | ToggleMenu | ClearData;
66

77
export enum ActionType {
88
Login = "login",
9-
Logout = "logout",
9+
ClearData = "clear_data",
1010
ToggleMenu = "toggle_menu"
1111
}
1212

1313
export interface Login {
1414
type: ActionType.Login;
1515
}
1616

17-
export interface Logout {
18-
type: ActionType.Logout;
19-
}
20-
2117
export interface ToggleMenu {
2218
type: ActionType.ToggleMenu;
2319
}
2420

21+
export interface ClearData {
22+
type: ActionType.ClearData;
23+
}
24+
2525
const login = (): Login => ({
2626
type: ActionType.Login
2727
});
@@ -32,9 +32,11 @@ const logout = () => {
3232
getState: () => ReducerConfigure
3333
) => {
3434
await clearWallet();
35-
dispatch({
36-
type: ActionType.Logout
37-
});
35+
setTimeout(() => {
36+
dispatch({
37+
type: ActionType.ClearData
38+
});
39+
}, 100);
3840
};
3941
};
4042

src/redux/global/globalReducer.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,18 @@ export interface GlobalState {
55
isSideMenuOpen: boolean;
66
}
77

8-
const initialState: GlobalState = {
8+
export const globalInitState: GlobalState = {
99
isAuthenticated: false,
1010
isSideMenuOpen: false
1111
};
1212

13-
export const globalReducer = (state = initialState, action: Action) => {
13+
export const globalReducer = (state = globalInitState, action: Action) => {
1414
switch (action.type) {
1515
case ActionType.Login:
1616
return {
1717
...state,
1818
isAuthenticated: true
1919
};
20-
case ActionType.Logout:
21-
return {
22-
...state,
23-
isAuthenticated: false,
24-
isSideMenuOpen: false
25-
};
2620
case ActionType.ToggleMenu:
2721
return {
2822
...state,

src/redux/index.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { combineReducers } from "redux";
2-
import { assetReducer, AssetState } from "./asset/assetReducer";
3-
import { globalReducer, GlobalState } from "./global/globalReducer";
2+
import { assetInitState, assetReducer, AssetState } from "./asset/assetReducer";
3+
import { ActionType } from "./global/globalActions";
4+
import {
5+
globalInitState,
6+
globalReducer,
7+
GlobalState
8+
} from "./global/globalReducer";
49
import {
510
transactionReducer,
6-
TransactionState
11+
TransactionState,
12+
txInitState
713
} from "./transaction/transactionReducer";
8-
import { walletReducer, WalletState } from "./wallet/walletReducer";
14+
import {
15+
walletInitState,
16+
walletReducer,
17+
WalletState
18+
} from "./wallet/walletReducer";
919

1020
export interface ReducerConfigure {
1121
globalReducer: GlobalState;
@@ -14,10 +24,23 @@ export interface ReducerConfigure {
1424
transactionReducer: TransactionState;
1525
}
1626

17-
const rootReducer = combineReducers({
27+
const appReducer = combineReducers({
1828
globalReducer,
1929
walletReducer,
2030
assetReducer,
2131
transactionReducer
2232
});
33+
34+
const rootReducer = (state: any, action: any) => {
35+
if (action.type === ActionType.ClearData) {
36+
state = {
37+
globalReducer: globalInitState,
38+
walletReducer: walletInitState,
39+
transactionReducer: txInitState,
40+
assetReducer: assetInitState
41+
};
42+
}
43+
return appReducer(state, action);
44+
};
45+
2346
export default rootReducer;

src/redux/transaction/transactionReducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export interface TransactionState {
2525
};
2626
}
2727

28-
const initialState: TransactionState = {
28+
export const txInitState: TransactionState = {
2929
pendingTxList: {},
3030
unconfirmedTxList: {},
3131
sendingTx: {}
3232
};
3333

34-
export const transactionReducer = (state = initialState, action: Action) => {
34+
export const transactionReducer = (state = txInitState, action: Action) => {
3535
switch (action.type) {
3636
case ActionType.CachePendingTxList: {
3737
const address = action.data.address;

src/redux/wallet/walletActions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export enum ActionType {
2121
UpdateWalletAssetAddresses = "updateWalletAssetAddresses",
2222
UpdateWalletName = "updateWalletName",
2323
UpdateAccount = "updateAccount",
24-
SetFetchingAccount = "setFetchingAccount"
24+
SetFetchingAccount = "setFetchingAccount",
25+
ClearWallet = "clearWallet"
2526
}
2627

2728
export interface UpdateWalletName {

src/redux/wallet/walletReducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export interface WalletState {
1414
};
1515
}
1616

17-
const initialState: WalletState = {
17+
export const walletInitState: WalletState = {
1818
platformAddresses: undefined,
1919
assetAddresses: undefined,
2020
walletName: undefined,
2121
accounts: {}
2222
};
2323

24-
export const walletReducer = (state = initialState, action: Action) => {
24+
export const walletReducer = (state = walletInitState, action: Action) => {
2525
switch (action.type) {
2626
case ActionType.UpdateWalletAssetAddresses:
2727
return {

0 commit comments

Comments
 (0)