Browse Source

feat: add poll DBs replication in saga

develop
Apostolos Fanakis 4 years ago
parent
commit
76c78d754c
Signed by: Apostolof GPG Key ID: 8600B4C4163B3269
  1. 8
      packages/concordia-app/src/redux/reducers/peerDbReplicationReducer.js
  2. 41
      packages/concordia-app/src/redux/sagas/peerDbReplicationSaga.js

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

@ -4,13 +4,16 @@ const initialState = {
users: [], users: [],
topics: [], topics: [],
posts: [], posts: [],
polls: [],
}; };
const peerDbReplicationReducer = (state = initialState, action) => { const peerDbReplicationReducer = (state = initialState, action) => {
const { type } = action; const { type } = action;
if (type === UPDATE_ORBIT_DATA) { if (type === UPDATE_ORBIT_DATA) {
const { users, topics, posts } = action; const {
users, topics, posts, polls,
} = action;
return { return {
...state, ...state,
@ -23,6 +26,9 @@ const peerDbReplicationReducer = (state = initialState, action) => {
posts: [ posts: [
...posts, ...posts,
], ],
polls: [
...polls,
],
}; };
} }

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

@ -7,12 +7,18 @@ import {
ORBIT_DB_REPLICATED, ORBIT_DB_REPLICATED,
ORBIT_DB_WRITE, ORBIT_DB_WRITE,
} from '@ezerous/breeze/src/orbit/orbitActions'; } from '@ezerous/breeze/src/orbit/orbitActions';
import { POSTS_DATABASE, TOPICS_DATABASE, USER_DATABASE } from 'concordia-shared/src/constants/orbit/OrbitDatabases'; import {
POLLS_DATABASE,
POSTS_DATABASE,
TOPICS_DATABASE,
USER_DATABASE,
} from 'concordia-shared/src/constants/orbit/OrbitDatabases';
import determineKVAddress from '../../utils/orbitUtils'; import determineKVAddress from '../../utils/orbitUtils';
import { FETCH_USER_DATABASE, UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions'; import { FETCH_USER_DATABASE, UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions';
import userDatabaseKeys from '../../constants/orbit/UserDatabaseKeys'; import userDatabaseKeys from '../../constants/orbit/UserDatabaseKeys';
import { TOPIC_SUBJECT } from '../../constants/orbit/TopicsDatabaseKeys'; import { TOPIC_SUBJECT } from '../../constants/orbit/TopicsDatabaseKeys';
import { POST_CONTENT } from '../../constants/orbit/PostsDatabaseKeys'; import { POST_CONTENT } from '../../constants/orbit/PostsDatabaseKeys';
import { POLL_OPTIONS, POLL_QUESTION } from '../../constants/orbit/PollsDatabaseKeys';
function* fetchUserDb({ orbit, userAddress, dbName }) { function* fetchUserDb({ orbit, userAddress, dbName }) {
const peerDbAddress = yield call(determineKVAddress, { const peerDbAddress = yield call(determineKVAddress, {
@ -23,10 +29,13 @@ function* fetchUserDb({ orbit, userAddress, dbName }) {
} }
function* updateReduxState({ database }) { function* updateReduxState({ database }) {
const { users, topics, posts } = yield select((state) => ({ const {
users, topics, posts, polls,
} = yield select((state) => ({
users: state.orbitData.users, users: state.orbitData.users,
topics: state.orbitData.topics, topics: state.orbitData.topics,
posts: state.orbitData.posts, posts: state.orbitData.posts,
polls: state.orbitData.polls,
})); }));
if (database.dbname === USER_DATABASE) { if (database.dbname === USER_DATABASE) {
@ -53,6 +62,7 @@ function* updateReduxState({ database }) {
], ],
topics: [...topics], topics: [...topics],
posts: [...posts], posts: [...posts],
polls: [...polls],
}); });
} }
@ -76,6 +86,7 @@ function* updateReduxState({ database }) {
})), })),
], ],
posts: [...posts], posts: [...posts],
polls: [...polls],
}); });
} }
@ -97,6 +108,32 @@ function* updateReduxState({ database }) {
[POST_CONTENT]: value[POST_CONTENT], [POST_CONTENT]: value[POST_CONTENT],
})), })),
], ],
polls: [...polls],
});
}
if (database.dbname === POLLS_DATABASE) {
const oldPollsUnchanged = polls
.filter((poll) => !Object
.keys(database.all)
.map((key) => parseInt(key, 10))
.includes(poll.id));
yield put({
type: UPDATE_ORBIT_DATA,
users: [...users],
topics: [...topics],
posts: [...posts],
polls: [
...oldPollsUnchanged,
...Object.entries(database.all).map(([key, value]) => ({
id: parseInt(key, 10),
[POLL_QUESTION]: value[POLL_QUESTION],
[POLL_OPTIONS]: [
...value[POLL_OPTIONS],
],
})),
],
}); });
} }
} }

Loading…
Cancel
Save