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. 101
      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 { useDispatch, useSelector } from 'react-redux';
import AppContext from '../../AppContext';
import { FETCH_USER_TOPIC } from '../../../redux/actions/peerDbReplicationActions';
import { FETCH_USER_DATABASE } from '../../../redux/actions/peerDbReplicationActions';
const TopicListRow = (props) => {
const { topicData, topicId } = props;
@ -22,15 +22,14 @@ const TopicListRow = (props) => {
}
dispatch({
type: FETCH_USER_TOPIC,
type: FETCH_USER_DATABASE,
orbit,
userAddress: topicData.userAddress,
topicId,
});
}, [dispatch, orbit, topicData.userAddress, topicId, userAddress]);
useEffect(() => {
const topicFound = topics.find((topic) => topic.topicId === topicId);
const topicFound = topics.find((topic) => topic.id === topicId);
if (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 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 FETCH_USER_DATABASE = 'FETCH_USER_DATABASE';
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 = {
fetchedPeerDatabases: [],
topics: [],
posts: [],
};
@ -8,30 +9,6 @@ const initialState = {
const peerDbReplicationReducer = (state = initialState, 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) {
const { topics, posts } = action;

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

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

Loading…
Cancel
Save