Browse Source

Orbit refactoring

develop
Ezerous 6 years ago
parent
commit
2fdcb4f89a
  1. 1
      app/src/assets/css/App.css
  2. 2
      app/src/containers/UsernameFormContainer.js
  3. 2
      app/src/index.js
  4. 12
      app/src/redux/actions/orbitActions.js
  5. 2
      app/src/redux/reducers/orbitReducer.js
  6. 7
      app/src/redux/sagas/orbitSaga.js
  7. 4
      app/src/redux/sagas/transactionsSaga.js
  8. 39
      app/src/utils/orbitUtils.js

1
app/src/assets/css/App.css

@ -72,6 +72,7 @@ strong {
position: absolute;
left: calc(50% - 596px);
text-align: center;
z-index: -1; /* Temporary (?) */
}
.navBarText span {

2
app/src/containers/UsernameFormContainer.js

@ -4,7 +4,7 @@ import { connect } from "react-redux";
import { Button, Message, Form, Dimmer, Loader, Header } from 'semantic-ui-react';
import { drizzle } from '../index';
import { createDatabases } from '../orbit';
import { createDatabases } from '../utils/orbitUtils';
import { updateUsername } from '../redux/actions/transactionsActions';
const contract = "Forum";

2
app/src/index.js

@ -6,7 +6,7 @@ import { Drizzle } from 'drizzle';
import store, {history} from './redux/store';
import routes from './router/routes'
import { initIPFS } from './orbit'
import { initIPFS } from './utils/orbitUtils'
import * as serviceWorker from './utils/serviceWorker';
import './assets/css/index.css';

12
app/src/redux/actions/orbitActions.js

@ -3,4 +3,14 @@ const DATABASES_CREATED = 'DATABASES_CREATED';
const DATABASES_LOADED = 'DATABASES_LOADED';
const DATABASES_NOT_READY = 'DATABASES_NOT_READY';
export { DATABASES_CREATED, DATABASES_LOADED, DATABASES_NOT_READY, IPFS_INITIALIZED }
function updateDatabases(type, orbitdb, topicsDB, postsDB){
return {
type,
orbitdb,
topicsDB,
postsDB,
id: orbitdb.id
};
}
export { DATABASES_CREATED, DATABASES_LOADED, DATABASES_NOT_READY, IPFS_INITIALIZED, updateDatabases }

2
app/src/redux/reducers/orbitReducer.js

@ -1,6 +1,7 @@
import { IPFS_INITIALIZED, DATABASES_CREATED, DATABASES_LOADED, DATABASES_NOT_READY } from "../actions/orbitActions";
const initialState = {
ipfs: null,
ipfsInitialized: false,
ready: false,
orbitdb: null,
@ -14,6 +15,7 @@ const orbitReducer = (state = initialState, action) => {
case IPFS_INITIALIZED:
return {
...state,
ipfs: action.ipfs,
ipfsInitialized: true
};
case DATABASES_CREATED:

7
app/src/redux/sagas/orbitSaga.js

@ -1,8 +1,8 @@
import {all, call, put, take, takeLatest} from 'redux-saga/effects'
import { contract, getCurrentAccount} from './drizzleUtilsSaga';
import { loadDatabases } from '../../orbit'
import { DRIZZLE_UTILS_SAGA_INITIALIZED } from "../actions/drizzleUtilsActions";
import { IPFS_INITIALIZED, DATABASES_NOT_READY } from "../actions/orbitActions";
import { loadDatabases } from '../../utils/orbitUtils'
import { DRIZZLE_UTILS_SAGA_INITIALIZED } from '../actions/drizzleUtilsActions';
import { IPFS_INITIALIZED, DATABASES_NOT_READY } from '../actions/orbitActions';
let latestAccount;
@ -32,7 +32,6 @@ function* getOrbitDBInfo() {
console.error(error);
yield put({type: 'ORBIT_SAGA_ERROR', ...[]});
}
}
}

4
app/src/redux/sagas/transactionsSaga.js

@ -1,8 +1,8 @@
import {call, select, take, takeEvery} from 'redux-saga/effects'
import { drizzle } from '../../index'
import { orbitSagaPut } from '../../orbit'
import { DRIZZLE_UTILS_SAGA_INITIALIZED } from "../actions/drizzleUtilsActions";
import { orbitSagaPut } from '../../utils/orbitUtils'
import { DRIZZLE_UTILS_SAGA_INITIALIZED } from '../actions/drizzleUtilsActions';
let transactionsHistory = Object.create(null);

39
app/src/orbit.js → app/src/utils/orbitUtils.js

@ -1,32 +1,26 @@
import IPFS from 'ipfs';
import OrbitDB from 'orbit-db';
import Keystore from 'orbit-db-keystore';
import path from 'path';
import store from './redux/store';
import ipfsOptions from './config/ipfsOptions'
import { IPFS_INITIALIZED, DATABASES_CREATED, DATABASES_LOADED } from './redux/actions/orbitActions';
let ipfs;
import store from '../redux/store';
import { DATABASES_CREATED, DATABASES_LOADED, IPFS_INITIALIZED, updateDatabases } from '../redux/actions/orbitActions';
import IPFS from "ipfs";
import ipfsOptions from "../config/ipfsOptions";
function initIPFS(){
ipfs = new IPFS(ipfsOptions);
const ipfs = new IPFS(ipfsOptions);
ipfs.on('ready', async () => {
store.dispatch({type: IPFS_INITIALIZED});
store.dispatch({type: IPFS_INITIALIZED, ipfs});
console.log("IPFS initialized.");
});
}
async function createDatabases() {
console.log("Creating databases...");
const ipfs = getIPFS();
const orbitdb = await new OrbitDB(ipfs);
const topicsDB = await orbitdb.keyvalue('topics');
const postsDB = await orbitdb.keyvalue('posts');
store.dispatch({
type: DATABASES_CREATED,
orbitdb: orbitdb,
topicsDB: topicsDB,
postsDB: postsDB,
id: orbitdb.id
});
store.dispatch(updateDatabases(DATABASES_CREATED, orbitdb, topicsDB, postsDB));
const orbitKey = orbitdb.keystore.getKey(orbitdb.id);
@ -53,6 +47,7 @@ async function loadDatabases(identityId, identityPublicKey, identityPrivateKey,
privateKey: orbitPrivateKey
}));
const ipfs = getIPFS();
const orbitdb = await new OrbitDB(ipfs, directory, { peerId:orbitId, keystore:keystore});
const topicsDB = await orbitdb.keyvalue('/orbitdb/' + topicsDBId +'/topics');
const postsDB = await orbitdb.keyvalue('/orbitdb/' + postsDBId +'/posts');
@ -60,13 +55,13 @@ async function loadDatabases(identityId, identityPublicKey, identityPrivateKey,
await topicsDB.load();
await postsDB.load();
store.dispatch({
type: DATABASES_LOADED,
orbitdb: orbitdb,
topicsDB: topicsDB,
postsDB: postsDB,
id: orbitdb.id
});
updateDatabases(DATABASES_LOADED, orbitdb, topicsDB, postsDB);
return { orbitdb, topicsDB, postsDB };
}
function getIPFS(){
return store.getState().orbit.ipfs;
}
async function orbitSagaPut(db, key, value) {
Loading…
Cancel
Save