diff --git a/app/package.json b/app/package.json index e480194..f04598a 100644 --- a/app/package.json +++ b/app/package.json @@ -28,7 +28,7 @@ "redux-saga": "0.16.2", "semantic-ui-react": "0.86.0", "uuid": "3.3.2", - "web3": "1.0.0-beta.48" + "web3": "1.0.0-beta.50" }, "scripts": { "start": "react-scripts start", diff --git a/app/src/components/Topic.js b/app/src/components/Topic.js index 73a4cf7..48511a5 100644 --- a/app/src/components/Topic.js +++ b/app/src/components/Topic.js @@ -23,56 +23,52 @@ class Topic extends Component { } componentDidMount() { - const { topicSubjectFetchStatus } = this.state; - const { topicData, orbitDB, topicID } = this.props; - - if (topicData !== null - && topicSubjectFetchStatus === 'pending' - && orbitDB.ipfsInitialized - && orbitDB.orbitdb) { - this.fetchSubject(topicID); - } + this.fetchSubject(this.props.topicID); } componentDidUpdate() { - const { topicSubjectFetchStatus } = this.state; - const { topicData, orbitDB, topicID } = this.props; - - if (topicData !== null - && topicSubjectFetchStatus === 'pending' - && orbitDB.ipfsInitialized - && orbitDB.orbitdb) { - this.fetchSubject(topicID); - } + this.fetchSubject(this.props.topicID); } async fetchSubject(topicID) { + const { topicSubjectFetchStatus } = this.state; const { topicData, user, orbitDB } = this.props; - let topicSubject; - - if (topicData.value[1] === user.address) { - const orbitData = orbitDB.topicsDB.get(topicID); - topicSubject = orbitData.subject; - } else { - const fullAddress = `/orbitdb/${topicData.value[0]}/topics`; - const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'}); - await store.load(); - - const localOrbitData = store.get(topicID); - if (localOrbitData) { - topicSubject = localOrbitData.subject; + + if (topicData !== null + && topicSubjectFetchStatus === 'pending' + && orbitDB.ipfsInitialized + && orbitDB.orbitdb) { + + let topicSubject; + + if (topicData.value[1] === user.address) { + const orbitData = orbitDB.topicsDB.get(topicID); + if(orbitData) + topicSubject = orbitData.subject; } else { - // Wait until we have received something from the network + const fullAddress = `/orbitdb/${topicData.value[0]}/topics`; + const store = await orbitDB.orbitdb.open(fullAddress, {type: 'keyvalue'}); + await store.load(); + + const localOrbitData = store.get(topicID); + if (localOrbitData) + topicSubject = localOrbitData.subject; + + store.events.on('replicate', () => { + console.log("Initiated OrbitDB data replication."); + }); + store.events.on('replicated', () => { + console.log("OrbitDB data replicated successfully."); topicSubject = store.get(topicID).subject; }); } - } - this.setState({ - topicSubject, - topicSubjectFetchStatus: 'fetched' - }); + this.setState({ + topicSubject, + topicSubjectFetchStatus: 'fetched' + }); + } } render() { @@ -93,7 +89,7 @@ class Topic extends Component { >
- {topicSubject !== null ? topicSubject
+ {(topicSubject) ? topicSubject
: (