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';
|
|
|
|
|
2018-07-06 19:06:14 +10:00
|
|
|
/**
|
|
|
|
* Class representing the highest order component. Any user
|
|
|
|
* updates in child components should trigger an event in this
|
|
|
|
* class so that the current user details can be re-fetched from
|
|
|
|
* the contract and propagated to all children that rely on it
|
|
|
|
*
|
|
|
|
* @extends React.Component
|
|
|
|
*/
|
2018-07-03 16:31:10 +10:00
|
|
|
class App extends Component {
|
|
|
|
|
2018-07-06 19:06:14 +10:00
|
|
|
//#region Constructor
|
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-06 19:06:14 +10:00
|
|
|
//#endregion
|
2018-07-03 16:31:10 +10:00
|
|
|
|
2018-07-06 19:06:14 +10:00
|
|
|
//#region Helper methods
|
|
|
|
/**
|
|
|
|
* Loads user details from the contract based on the current
|
|
|
|
* account (address).
|
|
|
|
*
|
|
|
|
* First, the owners mapping is queried using the owner address key. It returns
|
|
|
|
* the hash of the username it maps to. This username hash is then used to query
|
|
|
|
* the users mapping in the contract to get the details of the user. Once the user
|
|
|
|
* details are returned, the state is updated with the details, which triggers a
|
|
|
|
* render in this component and all child components.
|
|
|
|
*
|
|
|
|
* @returns {null}
|
|
|
|
*/
|
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-06 19:06:14 +10:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region React lifecycle events
|
|
|
|
componentDidMount() {
|
|
|
|
EmbarkJS.onReady(() => {
|
|
|
|
setTimeout(() => { this._loadCurrentUser(); }, 0);
|
|
|
|
});
|
|
|
|
}
|
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-06 19:06:14 +10:00
|
|
|
//#endregion
|
2018-07-03 16:31:10 +10:00
|
|
|
}
|
2018-07-01 19:17:41 +10:00
|
|
|
|
|
|
|
export default App
|