Browse Source

Mirror all orbit data to redux in peer db replication saga

develop
Apostolos Fanakis 4 years ago
parent
commit
b89e56f86c
  1. 7
      packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx
  2. 9
      packages/concordia-app/src/redux/actions/peerDbReplicationActions.js
  3. 27
      packages/concordia-app/src/redux/reducers/peerDbReplicationReducer.js
  4. 93
      packages/concordia-app/src/redux/sagas/peerDbReplicationSaga.js

7
packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx

@ -3,7 +3,7 @@ import { List } from 'semantic-ui-react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import AppContext from '../../AppContext'; import AppContext from '../../AppContext';
import { FETCH_USER_TOPIC } from '../../../redux/actions/peerDbReplicationActions'; import { FETCH_USER_DATABASE } from '../../../redux/actions/peerDbReplicationActions';
const TopicListRow = (props) => { const TopicListRow = (props) => {
const { topicData, topicId } = props; const { topicData, topicId } = props;
@ -22,15 +22,14 @@ const TopicListRow = (props) => {
} }
dispatch({ dispatch({
type: FETCH_USER_TOPIC, type: FETCH_USER_DATABASE,
orbit, orbit,
userAddress: topicData.userAddress, userAddress: topicData.userAddress,
topicId,
}); });
}, [dispatch, orbit, topicData.userAddress, topicId, userAddress]); }, [dispatch, orbit, topicData.userAddress, topicId, userAddress]);
useEffect(() => { useEffect(() => {
const topicFound = topics.find((topic) => topic.topicId === topicId); const topicFound = topics.find((topic) => topic.id === topicId);
if (topicFound) { if (topicFound) {
setTopicSubject(topicFound); setTopicSubject(topicFound);

9
packages/concordia-app/src/redux/actions/peerDbReplicationActions.js

@ -1,9 +1,2 @@
export const FETCH_USER_TOPIC = 'FETCH_USER_TOPIC'; export const FETCH_USER_DATABASE = 'FETCH_USER_DATABASE';
export const ADD_USER_TOPIC = 'ADD_USER_TOPIC';
export const UPDATE_USER_TOPICS = 'UPDATE_USER_TOPICS';
export const FETCH_USER_POST = 'FETCH_USER_POST';
export const ADD_USER_POST = 'ADD_USER_POST';
export const UPDATE_USER_POSTS = 'UPDATE_USER_POSTS';
export const UPDATE_ORBIT_DATA = 'UPDATE_ORBIT_DATA'; export const UPDATE_ORBIT_DATA = 'UPDATE_ORBIT_DATA';

27
packages/concordia-app/src/redux/reducers/peerDbReplicationReducer.js

@ -1,6 +1,7 @@
import { ADD_USER_POST, ADD_USER_TOPIC, UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions'; import { UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions';
const initialState = { const initialState = {
fetchedPeerDatabases: [],
topics: [], topics: [],
posts: [], posts: [],
}; };
@ -8,30 +9,6 @@ const initialState = {
const peerDbReplicationReducer = (state = initialState, action) => { const peerDbReplicationReducer = (state = initialState, action) => {
const { type } = action; const { type } = action;
if (type === ADD_USER_TOPIC) {
const { topic } = action;
return {
...state,
topics: [
...state.topics,
topic,
],
};
}
if (type === ADD_USER_POST) {
const { post } = action;
return {
...state,
posts: [
...state.posts,
post,
],
};
}
if (type === UPDATE_ORBIT_DATA) { if (type === UPDATE_ORBIT_DATA) {
const { topics, posts } = action; const { topics, posts } = action;

93
packages/concordia-app/src/redux/sagas/peerDbReplicationSaga.js

@ -5,96 +5,55 @@ import {
createOrbitDatabase, createOrbitDatabase,
ORBIT_DATABASE_READY, ORBIT_DATABASE_READY,
ORBIT_DATABASE_REPLICATED, ORBIT_DATABASE_REPLICATED,
ORBIT_DATABASE_WRITE,
} from '@ezerous/breeze/src/orbit/orbitActions'; } from '@ezerous/breeze/src/orbit/orbitActions';
import determineDBAddress from '../../orbit/orbitUtils'; import determineDBAddress from '../../orbit/orbitUtils';
import { import { FETCH_USER_DATABASE, UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions';
ADD_USER_POST,
ADD_USER_TOPIC,
FETCH_USER_POST,
FETCH_USER_TOPIC,
UPDATE_ORBIT_DATA,
} from '../actions/peerDbReplicationActions';
function* fetchUserDb({ orbit, peerDbAddress }) {
yield put(createOrbitDatabase(orbit, { name: peerDbAddress, type: 'keyvalue' }));
}
function* fetchTopic({ orbit, userAddress, topicId }) { function* fetchUserDb({ orbit, userAddress }) {
const previousTopics = yield select((state) => state.orbitData.topics);
const peerDbAddress = yield call(determineDBAddress, { const peerDbAddress = yield call(determineDBAddress, {
orbit, dbName: 'topics', type: 'keyvalue', identityId: userAddress, orbit, dbName: 'topics', type: 'keyvalue', identityId: userAddress,
}); });
if (previousTopics === undefined || !previousTopics.some((topic) => topic.topicId === topicId)) { yield put(createOrbitDatabase(orbit, { name: peerDbAddress, type: 'keyvalue' }));
yield put({
type: ADD_USER_TOPIC,
topic: {
userAddress,
dbAddress: peerDbAddress,
topicId,
subject: null,
},
});
}
yield call(fetchUserDb, { orbit, peerDbAddress });
}
function* fetchUserPost({ orbit, userAddress, postId }) {
const previousPosts = yield select((state) => state.orbitData.posts);
const peerDbAddress = yield call(determineDBAddress, {
orbit, dbName: 'posts', type: 'keyvalue', identityId: userAddress,
});
if (previousPosts === undefined || !previousPosts.some((post) => post.postId === postId)) {
yield put({
type: ADD_USER_POST,
posts: {
userAddress,
dbAddress: peerDbAddress,
postId,
subject: null,
message: null,
},
});
}
yield call(fetchUserDb, { orbit, peerDbAddress });
} }
function* updateReduxState({ database }) { function* updateReduxState({ database }) {
const { topics, posts } = yield select((state) => ({ topics: state.orbitData.topics, posts: state.orbitData.posts })); const { topics, posts } = yield select((state) => ({
topics: state.orbitData.topics,
posts: state.orbitData.posts,
}));
if (database.dbname === 'topics') {
yield put({ yield put({
type: UPDATE_ORBIT_DATA, type: UPDATE_ORBIT_DATA,
topics: topics.map((topic) => { topics: [...Object.entries(database.all).map(([key, value]) => ({
if (database.id === topic.dbAddress) { id: parseInt(key, 10),
return ({ subject: value.subject,
...topic, }))],
...database.get(topic.topicId), posts: [...posts],
}); });
} }
return { ...topic }; if (database.dbname === 'posts') {
}), yield put({
posts: posts.map((post) => { type: UPDATE_ORBIT_DATA,
if (database.id === post.dbAddress) { topics: [...topics],
return ({ posts: [...Object.entries(database.all).map(([key, value]) => ({
...post, id: parseInt(key, 10),
...database.get(post.postId), subject: value.subject,
message: value.message,
}))],
}); });
} }
return { ...post };
}),
});
} }
function* peerDbReplicationSaga() { function* peerDbReplicationSaga() {
yield takeEvery(FETCH_USER_TOPIC, fetchTopic); yield takeEvery(FETCH_USER_DATABASE, fetchUserDb);
yield takeEvery(FETCH_USER_POST, fetchUserPost);
yield takeEvery(ORBIT_DATABASE_REPLICATED, updateReduxState); yield takeEvery(ORBIT_DATABASE_REPLICATED, updateReduxState);
yield takeEvery(ORBIT_DATABASE_READY, updateReduxState); yield takeEvery(ORBIT_DATABASE_READY, updateReduxState);
yield takeEvery(ORBIT_DATABASE_WRITE, updateReduxState);
} }
export default peerDbReplicationSaga; export default peerDbReplicationSaga;

Loading…
Cancel
Save