import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { push } from 'connected-react-router';
import { connect } from 'react-redux';
import { Tab } from 'semantic-ui-react';
import { drizzle } from '../index';
import ProfileInformation from '../components/ProfileInformation';
import TopicList from '../components/TopicList';
import PostList from '../components/PostList';
import LoadingSpinner from '../components/LoadingSpinner';
import { setNavBarTitle } from '../redux/actions/userInterfaceActions';
const callsInfo = [
{
contract: 'Forum',
method: 'getUsername'
}, {
contract: 'Forum',
method: 'getUserTopics'
}, {
contract: 'Forum',
method: 'getUserPosts'
}
];
class ProfileContainer extends Component {
constructor(props) {
super(props);
this.getBlockchainData = this.getBlockchainData.bind(this);
this.dataKey = [];
const address = this.props.match.params.address
? this.props.match.params.address
: this.props.user.address;
this.state = {
pageStatus: 'initialized',
userAddress: address,
username: '',
topicIDs: null,
postIDs: null
};
}
getBlockchainData() {
if (this.state.pageStatus === 'initialized'
&& this.props.drizzleStatus.initialized) {
callsInfo.forEach((call, index) => {
this.dataKey[index] = drizzle.contracts[call.contract].methods[call.method].cacheCall(
this.state.userAddress,
);
});
this.setState({
pageStatus: 'loading'
});
}
if (this.state.pageStatus === 'loading') {
let pageStatus = 'loaded';
callsInfo.forEach((call, index) => {
if (!this.props.contracts[call.contract][call.method][this.dataKey[index]]) {
pageStatus = 'loading';
}
});
if (pageStatus === 'loaded') {
this.setState({
pageStatus
});
}
}
if (this.state.pageStatus === 'loaded') {
if (this.state.username === '') {
const transaction = this.props.contracts[callsInfo[0].contract][callsInfo[0].method][this.dataKey[0]];
if (transaction) {
const username = transaction.value;
this.props.setNavBarTitle(username);
this.setState({
username
});
}
}
if (this.state.topicIDs === null) {
const transaction = this.props.contracts[callsInfo[1].contract][callsInfo[1].method][this.dataKey[1]];
if (transaction) {
this.setState({
topicIDs: transaction.value
});
}
}
if (this.state.postIDs === null) {
const transaction = this.props.contracts[callsInfo[2].contract][callsInfo[2].method][this.dataKey[2]];
if (transaction) {
this.setState({
postIDs: transaction.value
});
}
}
/* this.props.store.dispatch(hideProgressBar()); */
}
}
render() {
if (!this.props.user.hasSignedUp) {
this.props.navigateTo('/signup');
return (null);
}
const infoTab = (