|
|
@ -1,6 +1,5 @@ |
|
|
|
import React, { Component } from 'react'; |
|
|
|
import PropTypes from 'prop-types'; |
|
|
|
import { GetTopicResult } from '../CustomPropTypes' |
|
|
|
import { connect } from 'react-redux'; |
|
|
|
import { withRouter } from 'react-router-dom'; |
|
|
|
|
|
|
@ -11,21 +10,10 @@ import TimeAgo from 'react-timeago'; |
|
|
|
import { addPeerDatabase } from '../redux/actions/orbitActions'; |
|
|
|
|
|
|
|
class Topic extends Component { |
|
|
|
constructor(props) { |
|
|
|
super(props); |
|
|
|
this.state = { |
|
|
|
askedForReplication: false, |
|
|
|
fetchedSubject: false |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
componentDidUpdate() { |
|
|
|
const { dispatch, topicData, topicSubject, orbitDB } = this.props; |
|
|
|
const { askedForReplication } = this.state; |
|
|
|
if(!askedForReplication && orbitDB.ipfsInitialized && orbitDB.orbitdb && dispatch && !topicSubject && topicData) { |
|
|
|
dispatch(addPeerDatabase(`/orbitdb/${topicData.value[0]}/topics`)); |
|
|
|
this.setState({ askedForReplication: true }); |
|
|
|
} |
|
|
|
componentDidMount() { |
|
|
|
const { dispatch, userAddress, topicData } = this.props; |
|
|
|
if(topicData.userAddress !== userAddress ) |
|
|
|
dispatch(addPeerDatabase(topicData.fullOrbitAddress)); |
|
|
|
} |
|
|
|
|
|
|
|
render() { |
|
|
@ -62,30 +50,14 @@ class Topic extends Component { |
|
|
|
</div> |
|
|
|
<hr /> |
|
|
|
<div className="topic-meta"> |
|
|
|
<p className={`no-margin${ |
|
|
|
topicData !== null ? '' : ' grey-text'}`}
|
|
|
|
> |
|
|
|
{topicData !== null |
|
|
|
? topicData.value[2] |
|
|
|
: 'Username' |
|
|
|
} |
|
|
|
<p className="no-margin"> |
|
|
|
{topicData.userName} |
|
|
|
</p> |
|
|
|
<p className={`no-margin${ |
|
|
|
topicData !== null ? '' : ' grey-text'}`}
|
|
|
|
> |
|
|
|
{`Number of replies: ${topicData !== null |
|
|
|
? topicData.value[4].length |
|
|
|
: ''}` |
|
|
|
} |
|
|
|
<p className="no-margin"> |
|
|
|
Number of Replies: {topicData.nReplies} |
|
|
|
</p> |
|
|
|
<p className="topic-date grey-text"> |
|
|
|
{topicData !== null |
|
|
|
&& ( |
|
|
|
<TimeAgo |
|
|
|
date={topicData.value[3]*1000} |
|
|
|
/> |
|
|
|
) |
|
|
|
} |
|
|
|
<TimeAgo date={topicData.timestamp}/> |
|
|
|
</p> |
|
|
|
</div> |
|
|
|
</Card.Content> |
|
|
@ -97,43 +69,37 @@ class Topic extends Component { |
|
|
|
Topic.propTypes = { |
|
|
|
user: PropTypes.object.isRequired, |
|
|
|
history: PropTypes.object.isRequired, |
|
|
|
topicData: GetTopicResult.isRequired, |
|
|
|
//TODO: topicData: GetTopicResult.isRequired,
|
|
|
|
orbitDB: PropTypes.object.isRequired, |
|
|
|
topicID: PropTypes.number.isRequired |
|
|
|
}; |
|
|
|
|
|
|
|
function getTopicSubject(state, props){ |
|
|
|
const { user, orbit } = state; |
|
|
|
if (orbit.ipfsInitialized && orbit.orbitdb) { |
|
|
|
const { topicData, topicID } = props; |
|
|
|
if (topicData){ |
|
|
|
if(user && topicData.value[1] === user.address) { |
|
|
|
const orbitData = orbit.topicsDB.get(topicID); |
|
|
|
if(orbitData && orbitData.subject) |
|
|
|
return orbitData.subject; |
|
|
|
} |
|
|
|
else{ |
|
|
|
const db = orbit.peerDatabases.find(db => db.fullAddress === `/orbitdb/${topicData.value[0]}/topics`); |
|
|
|
if(db && db.store){ |
|
|
|
const localOrbitData = db.store.get(topicID); |
|
|
|
if (localOrbitData) |
|
|
|
return localOrbitData.subject; |
|
|
|
} |
|
|
|
} |
|
|
|
const { user: {address: userAddress}, orbit } = state; |
|
|
|
const { topicData, topicID } = props; |
|
|
|
if(userAddress === topicData.userAddress) { |
|
|
|
const orbitData = orbit.topicsDB.get(topicID); |
|
|
|
if(orbitData && orbitData.subject) |
|
|
|
return orbitData.subject; |
|
|
|
} |
|
|
|
else{ |
|
|
|
const db = orbit.peerDatabases.find(db => db.fullAddress === topicData.fullOrbitAddress); |
|
|
|
if(db && db.store){ |
|
|
|
const localOrbitData = db.store.get(topicID); |
|
|
|
if (localOrbitData) |
|
|
|
return localOrbitData.subject; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function mapStateToProps(state, ownProps) { |
|
|
|
return { |
|
|
|
user: state.user, |
|
|
|
userAddress: state.user.address, |
|
|
|
orbitDB: state.orbit, |
|
|
|
topicSubject: getTopicSubject(state, ownProps) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export default withRouter(connect(mapStateToProps)(Topic)); |
|
|
|