mirror of https://gitlab.com/ecentrics/concordia
Apostolos Fanakis
4 years ago
7 changed files with 187 additions and 21 deletions
@ -0,0 +1,9 @@ |
|||
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 UPDATE_ORBIT_DATA = 'UPDATE_ORBIT_DATA'; |
@ -0,0 +1,52 @@ |
|||
import { ADD_USER_POST, ADD_USER_TOPIC, UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions'; |
|||
|
|||
const initialState = { |
|||
topics: [], |
|||
posts: [], |
|||
}; |
|||
|
|||
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; |
|||
|
|||
return { |
|||
...state, |
|||
topics: [ |
|||
...topics, |
|||
], |
|||
posts: [ |
|||
...posts, |
|||
], |
|||
}; |
|||
} |
|||
|
|||
return state; |
|||
}; |
|||
|
|||
export default peerDbReplicationReducer; |
@ -0,0 +1,100 @@ |
|||
import { |
|||
call, put, select, takeEvery, |
|||
} from 'redux-saga/effects'; |
|||
import { |
|||
createOrbitDatabase, |
|||
ORBIT_DATABASE_READY, |
|||
ORBIT_DATABASE_REPLICATED, |
|||
} 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' })); |
|||
} |
|||
|
|||
function* fetchTopic({ orbit, userAddress, topicId }) { |
|||
const previousTopics = yield select((state) => state.orbitData.topics); |
|||
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 }); |
|||
} |
|||
|
|||
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 }) { |
|||
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 }; |
|||
}), |
|||
}); |
|||
} |
|||
|
|||
function* peerDbReplicationSaga() { |
|||
yield takeEvery(FETCH_USER_TOPIC, fetchTopic); |
|||
yield takeEvery(FETCH_USER_POST, fetchUserPost); |
|||
yield takeEvery(ORBIT_DATABASE_REPLICATED, updateReduxState); |
|||
yield takeEvery(ORBIT_DATABASE_READY, updateReduxState); |
|||
} |
|||
|
|||
export default peerDbReplicationSaga; |
Loading…
Reference in new issue