This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Wallet.js
219 lines (197 loc) · 5.88 KB
/
Wallet.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
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
// @format
import React, { Component } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import styled from "styled-components";
import { FoldingCube } from "styled-spinkit";
import { toast } from "react-toastify";
import CinemarketToken from "./cinemarket/CinemarketToken";
import CinemarketNavigation from "./cinemarket/CinemarketNavigation";
import CinemarketHeadline from "./cinemarket/CinemarketHeadline";
import CinemarketInfo from "./cinemarket/CinemarketInfo";
import CinemarketAccountsLockedModal from "./cinemarket/CinemarketAccountsLockedModal";
import getWeb3 from "../utils/getWeb3";
import { fetchTransactionsBegin } from "../actions/fetchTransactions";
const StyledWalletWrapper = styled.div`
display: flex;
`;
const StyledWallet = styled.div`
width: 70%;
margin-left: 15%;
display: flex;
align-items: space-between;
flex-flow: row wrap;
justify-content: space-between;
`;
const Separator = styled.div`
width: 70%;
margin-left: 15%;
border-bottom: 1px solid black;
`;
const SeparatorHeadline = styled.h3`
margin-bottom: 0.1em;
`;
const StyledLoader = styled.div`
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
`;
const StyledEmptySet = styled.div`
text-align: center;
margin-top: 20vh;
`;
class Wallet extends Component {
constructor(props) {
super(props);
this.state = {
isOpen: false,
modals: {},
accounts: [],
accountsLocked: false
};
}
toggleModal = tokenId => {
return () => {
let modals = this.state.modals;
modals[tokenId] = !modals[tokenId];
this.setState({ modals });
};
};
componentDidMount() {
this.checkAccountsLocked();
}
async checkAccountsLocked() {
const web3 = await getWeb3();
const intervalId = setInterval(async () => {
const accounts = await web3.eth.getAccounts();
if (accounts.length) {
this.setState({ accountsLocked: false });
clearInterval(intervalId);
this.checkMetamaskSetup();
} else {
this.setState({ accountsLocked: true });
}
}, 100);
}
async checkMetamaskSetup() {
const web3 = await getWeb3();
if (web3.currentProvider.isMetaMask) {
const accounts = await web3.eth.getAccounts();
if (accounts.length) {
let contracts = localStorage.getItem("tokens");
contracts = contracts.split(",");
this.props.getTransactions(web3, accounts[0], contracts);
this.setState({ accounts });
} else {
toast.warning("Unlock Metamask");
}
} else {
toast.warning("Install Metamask");
}
}
componentDidUpdate(prevProps, prevState) {
if (this.props.error) {
toast.error(this.props.error.message);
}
}
separator(i, name) {
return (
<Separator>
<SeparatorHeadline>{name}</SeparatorHeadline>
</Separator>
);
}
totalCollectibles(transactions) {
return Object.keys(transactions).reduce((initVal, currVal) => {
return initVal + transactions[currVal].length;
}, 0);
}
render() {
const { modals, accountsLocked } = this.state;
const { transactions, loading } = this.props;
const totalCollectibles = this.totalCollectibles(transactions);
if (loading) {
return (
<StyledWalletWrapper>
<CinemarketNavigation />
<div style={{ width: "100%", minHeight: "100vh" }}>
<CinemarketHeadline />
<CinemarketInfo />
<StyledLoader>
<FoldingCube color="#000" />
</StyledLoader>
</div>
<CinemarketAccountsLockedModal accountsLocked={accountsLocked} />
</StyledWalletWrapper>
);
} else if (totalCollectibles === 0) {
return (
<StyledWalletWrapper>
<CinemarketNavigation />
<div style={{ width: "100%", minHeight: "100vh" }}>
<CinemarketHeadline />
<CinemarketInfo />
<StyledEmptySet>
<h1>No collectibles found...</h1>
<p>Add a token to view your collectibles</p>
</StyledEmptySet>
</div>
<CinemarketAccountsLockedModal accountsLocked={accountsLocked} />
</StyledWalletWrapper>
);
} else {
return (
<StyledWalletWrapper>
<CinemarketNavigation />
<div style={{ width: "100%", minHeight: "100vh" }}>
<CinemarketHeadline />
<CinemarketInfo />
{Object.keys(transactions).map((contractAddress, i) => (
<div key={i}>
{transactions[contractAddress].length > 0 &&
this.separator(i, transactions[contractAddress][0].name)}
<StyledWallet>
{transactions[contractAddress].map(
(
{ token, _tokenId, name, contract, link, tokenHash },
j
) => (
<CinemarketToken
key={j}
token={token}
tokenId={_tokenId}
tokenHash={tokenHash}
link={link}
name={name}
contract={contract}
modals={modals}
toggleModal={this.toggleModal}
account={this.state.accounts[0]}
/>
)
)}
</StyledWallet>
</div>
))}
</div>
</StyledWalletWrapper>
);
}
}
}
const mapDispatchToProps = {
getTransactions: fetchTransactionsBegin
};
const mapStateToProps = (state, ownProps) => {
return {
transactions: state.transactions.items,
loading: state.transactions.loading,
error: state.transactions.error
};
};
Wallet = connect(
mapStateToProps,
mapDispatchToProps
)(Wallet);
export default Wallet;