From 76c78d754c18216b6d29586f979960eca135a88e Mon Sep 17 00:00:00 2001 From: apostolof Date: Sun, 28 Mar 2021 16:52:12 +0300 Subject: [PATCH] feat: add poll DBs replication in saga --- .../reducers/peerDbReplicationReducer.js | 8 +++- .../src/redux/sagas/peerDbReplicationSaga.js | 41 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/concordia-app/src/redux/reducers/peerDbReplicationReducer.js b/packages/concordia-app/src/redux/reducers/peerDbReplicationReducer.js index 117d442..9795819 100644 --- a/packages/concordia-app/src/redux/reducers/peerDbReplicationReducer.js +++ b/packages/concordia-app/src/redux/reducers/peerDbReplicationReducer.js @@ -4,13 +4,16 @@ const initialState = { users: [], topics: [], posts: [], + polls: [], }; const peerDbReplicationReducer = (state = initialState, action) => { const { type } = action; if (type === UPDATE_ORBIT_DATA) { - const { users, topics, posts } = action; + const { + users, topics, posts, polls, + } = action; return { ...state, @@ -23,6 +26,9 @@ const peerDbReplicationReducer = (state = initialState, action) => { posts: [ ...posts, ], + polls: [ + ...polls, + ], }; } diff --git a/packages/concordia-app/src/redux/sagas/peerDbReplicationSaga.js b/packages/concordia-app/src/redux/sagas/peerDbReplicationSaga.js index 251a283..b64d91d 100644 --- a/packages/concordia-app/src/redux/sagas/peerDbReplicationSaga.js +++ b/packages/concordia-app/src/redux/sagas/peerDbReplicationSaga.js @@ -7,12 +7,18 @@ import { ORBIT_DB_REPLICATED, ORBIT_DB_WRITE, } 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 { FETCH_USER_DATABASE, UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions'; import userDatabaseKeys from '../../constants/orbit/UserDatabaseKeys'; import { TOPIC_SUBJECT } from '../../constants/orbit/TopicsDatabaseKeys'; import { POST_CONTENT } from '../../constants/orbit/PostsDatabaseKeys'; +import { POLL_OPTIONS, POLL_QUESTION } from '../../constants/orbit/PollsDatabaseKeys'; function* fetchUserDb({ orbit, userAddress, dbName }) { const peerDbAddress = yield call(determineKVAddress, { @@ -23,10 +29,13 @@ function* fetchUserDb({ orbit, userAddress, dbName }) { } function* updateReduxState({ database }) { - const { users, topics, posts } = yield select((state) => ({ + const { + users, topics, posts, polls, + } = yield select((state) => ({ users: state.orbitData.users, topics: state.orbitData.topics, posts: state.orbitData.posts, + polls: state.orbitData.polls, })); if (database.dbname === USER_DATABASE) { @@ -53,6 +62,7 @@ function* updateReduxState({ database }) { ], topics: [...topics], posts: [...posts], + polls: [...polls], }); } @@ -76,6 +86,7 @@ function* updateReduxState({ database }) { })), ], posts: [...posts], + polls: [...polls], }); } @@ -97,6 +108,32 @@ function* updateReduxState({ database }) { [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], + ], + })), + ], }); } }