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 : ( ({ diff --git a/app/src/utils/orbitUtils.js b/app/src/utils/orbitUtils.js index a4422e5..2249bb9 100644 --- a/app/src/utils/orbitUtils.js +++ b/app/src/utils/orbitUtils.js @@ -8,6 +8,7 @@ import ipfsOptions from '../config/ipfsOptions'; function initIPFS() { const ipfs = new IPFS(ipfsOptions); + ipfs.on('error', (error) => console.error(`IPFS error: ${error}`)); ipfs.on('ready', async () => { store.dispatch({ type: IPFS_INITIALIZED, ipfs @@ -43,9 +44,8 @@ async function createDatabases() { } async function loadDatabases(identityId, identityPublicKey, identityPrivateKey, - orbitId, orbitPublicKey, orbitPrivateKey, - topicsDBId, postsDBId) { - console.log('Loading databases...'); + orbitId, orbitPublicKey, orbitPrivateKey, + topicsDBId, postsDBId) { const directory = './orbitdb'; const keystore = Keystore.create(path.join(directory, orbitId, '/keystore')); @@ -59,12 +59,15 @@ async function loadDatabases(identityId, identityPublicKey, identityPrivateKey, { peerId: orbitId, keystore }); - const topicsDB = await orbitdb.keyvalue(`/orbitdb/${topicsDBId}/topics`); - const postsDB = await orbitdb.keyvalue(`/orbitdb/${postsDBId}/posts`); + const topicsDB = await orbitdb.keyvalue(`/orbitdb/${topicsDBId}/topics`) + .catch((error) => console.error(`TopicsDB init error: ${error}`)); + const postsDB = await orbitdb.keyvalue(`/orbitdb/${postsDBId}/posts`) + .catch((error) => console.error(`PostsDB init error: ${error}`)); - await topicsDB.load(); - await postsDB.load(); + await topicsDB.load().catch((error) => console.error(`TopicsDB loading error: ${error}`)); + await postsDB.load().catch((error) => console.error(`PostsDB loading error: ${error}`)); + console.log('Orbit databases loaded successfully.'); store.dispatch(updateDatabases(DATABASES_LOADED, orbitdb, topicsDB, postsDB)); } @@ -73,7 +76,7 @@ function getIPFS() { } async function orbitSagaPut(db, key, value) { - db.put(key, value); + await db.put(key, value).catch((error) => console.error(`Orbit put error: ${error}`)); } export { initIPFS, createDatabases, loadDatabases, orbitSagaPut };