2018-07-01 19:17:41 +10:00
|
|
|
import Header from './Header'
|
|
|
|
import Main from './Main'
|
2018-07-03 16:31:10 +10:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import imgAvatar from '../../img/avatar-default.png';
|
|
|
|
|
|
|
|
class App extends Component {
|
|
|
|
|
2018-07-04 16:34:51 +10:00
|
|
|
constructor(props) {
|
2018-07-03 16:31:10 +10:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
user: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 16:34:51 +10:00
|
|
|
componentDidMount() {
|
2018-07-03 16:31:10 +10:00
|
|
|
EmbarkJS.onReady(() => {
|
|
|
|
setTimeout(() => { this._loadCurrentUser(); }, 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-04 16:34:51 +10:00
|
|
|
_loadCurrentUser = async () => {
|
|
|
|
const accounts = await web3.eth.getAccounts();
|
|
|
|
try {
|
|
|
|
const usernameHash = await DTwitter.methods.owners(accounts[0]).call();
|
|
|
|
if (usernameHash) {
|
|
|
|
// get user details from contract
|
|
|
|
const user = await DTwitter.methods.users(usernameHash).call();
|
|
|
|
|
|
|
|
// update user picture with ipfs url
|
|
|
|
user.picture = user.picture.length > 0 ? EmbarkJS.Storage.getUrl(user.picture) : imgAvatar;
|
|
|
|
|
|
|
|
// update state with user details
|
2018-07-05 21:30:16 +10:00
|
|
|
return this.setState({ user: user, account: accounts[0] });
|
2018-07-03 16:31:10 +10:00
|
|
|
}
|
2018-07-04 16:34:51 +10:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error('Error loading currenet user: ', err);
|
|
|
|
}
|
2018-07-03 16:31:10 +10:00
|
|
|
}
|
|
|
|
|
2018-07-04 16:34:51 +10:00
|
|
|
render() {
|
2018-07-03 16:31:10 +10:00
|
|
|
return (
|
|
|
|
<div>
|
2018-07-05 21:30:16 +10:00
|
|
|
<Header user={ this.state.user } account={ this.state.account } />
|
|
|
|
<Main user={ this.state.user } account={ this.state.account } onAfterUserUpdate={ (e) => this._loadCurrentUser() } />
|
2018-07-03 16:31:10 +10:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-07-01 19:17:41 +10:00
|
|
|
|
|
|
|
export default App
|