Browse Source

Get user DBs and display user avatar in topics list

develop
Apostolos Fanakis 4 years ago
parent
commit
055b0037aa
  1. 54
      packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx
  2. 13
      packages/concordia-app/src/components/TopicList/TopicListRow/styles.css
  3. 7
      packages/concordia-app/src/constants/UserDatabaseKeys.js
  4. 7
      packages/concordia-app/src/redux/reducers/peerDbReplicationReducer.js
  5. 35
      packages/concordia-app/src/redux/sagas/peerDbReplicationSaga.js

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

@ -2,7 +2,7 @@ import React, {
memo, useEffect, useMemo, useState, memo, useEffect, useMemo, useState,
} from 'react'; } from 'react';
import { import {
Dimmer, Grid, List, Placeholder, Dimmer, Grid, Image, List, Placeholder,
} from 'semantic-ui-react'; } from 'semantic-ui-react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
@ -12,7 +12,9 @@ import { useDispatch, useSelector } from 'react-redux';
import { FETCH_USER_DATABASE } from '../../../redux/actions/peerDbReplicationActions'; import { FETCH_USER_DATABASE } from '../../../redux/actions/peerDbReplicationActions';
import { breeze } from '../../../redux/store'; import { breeze } from '../../../redux/store';
import './styles.css'; import './styles.css';
import { TOPICS_DATABASE } from '../../../constants/OrbitDatabases'; import { TOPICS_DATABASE, USER_DATABASE } from '../../../constants/OrbitDatabases';
import determineKVAddress from '../../../utils/orbitUtils';
import { PROFILE_PICTURE } from '../../../constants/UserDatabaseKeys';
const { orbit } = breeze; const { orbit } = breeze;
@ -24,8 +26,10 @@ const TopicListRow = (props) => {
const [topicAuthor, setTopicAuthor] = useState(null); const [topicAuthor, setTopicAuthor] = useState(null);
const [timeAgo, setTimeAgo] = useState(null); const [timeAgo, setTimeAgo] = useState(null);
const [topicSubject, setTopicSubject] = useState(null); const [topicSubject, setTopicSubject] = useState(null);
const [topicAuthorMeta, setTopicAuthorMeta] = useState(null);
const userAddress = useSelector((state) => state.user.address); const userAddress = useSelector((state) => state.user.address);
const topics = useSelector((state) => state.orbitData.topics); const topics = useSelector((state) => state.orbitData.topics);
const users = useSelector((state) => state.orbitData.users);
const dispatch = useDispatch(); const dispatch = useDispatch();
const history = useHistory(); const history = useHistory();
const { t } = useTranslation(); const { t } = useTranslation();
@ -47,6 +51,13 @@ const TopicListRow = (props) => {
dbName: TOPICS_DATABASE, dbName: TOPICS_DATABASE,
userAddress: topicAuthorAddress, userAddress: topicAuthorAddress,
}); });
dispatch({
type: FETCH_USER_DATABASE,
orbit,
dbName: USER_DATABASE,
userAddress: topicAuthorAddress,
});
} }
}, [dispatch, topicAuthorAddress, userAddress]); }, [dispatch, topicAuthorAddress, userAddress]);
@ -59,6 +70,23 @@ const TopicListRow = (props) => {
} }
}, [topicId, topics]); }, [topicId, topics]);
useEffect(() => {
if (topicAuthorAddress !== null) {
determineKVAddress({ orbit, dbName: USER_DATABASE, userAddress: topicAuthorAddress })
.then((userOrbitAddress) => {
const userFound = users
.find((user) => user.id === userOrbitAddress);
if (userFound) {
setTopicAuthorMeta(userFound);
}
})
.catch((error) => {
console.error('Error during service worker registration:', error);
});
}
}, [topicAuthorAddress, users]);
return useMemo(() => { return useMemo(() => {
const handleTopicClick = () => { const handleTopicClick = () => {
history.push(`/topics/${topicId}`); history.push(`/topics/${topicId}`);
@ -66,8 +94,24 @@ const TopicListRow = (props) => {
return ( return (
<Dimmer.Dimmable as={List.Item} onClick={handleTopicClick} blurring dimmed={loading} className="list-item"> <Dimmer.Dimmable as={List.Item} onClick={handleTopicClick} blurring dimmed={loading} className="list-item">
<List.Icon name="user circle" size="big" inverted color="black" verticalAlign="middle" /> {topicAuthorMeta !== null && topicAuthorMeta[PROFILE_PICTURE]
<List.Content> ? (
<Image
className="profile-picture"
avatar
src={topicAuthorMeta[PROFILE_PICTURE]}
/>
)
: (
<List.Icon
name="user circle"
size="big"
inverted
color="black"
verticalAlign="middle"
/>
)}
<List.Content className="list-content">
<List.Header> <List.Header>
<Grid> <Grid>
<Grid.Column floated="left" width={14}> <Grid.Column floated="left" width={14}>
@ -103,7 +147,7 @@ const TopicListRow = (props) => {
</List.Content> </List.Content>
</Dimmer.Dimmable> </Dimmer.Dimmable>
); );
}, [history, loading, numberOfReplies, t, timeAgo, topicAuthor, topicId, topicSubject]); }, [history, loading, numberOfReplies, t, timeAgo, topicAuthor, topicAuthorMeta, topicId, topicSubject]);
}; };
TopicListRow.defaultProps = { TopicListRow.defaultProps = {

13
packages/concordia-app/src/components/TopicList/TopicListRow/styles.css

@ -4,5 +4,18 @@
} }
.list-item { .list-item {
display: flex !important;
text-align: start; text-align: start;
} }
.profile-picture {
cursor: pointer;
max-width: 36px;
max-height: 36px;
margin: 0;
vertical-align: middle;
}
.list-content {
flex-grow: 1;
}

7
packages/concordia-app/src/constants/UserDatabaseKeys.js

@ -1,2 +1,9 @@
export const PROFILE_PICTURE = 'profile_picture'; export const PROFILE_PICTURE = 'profile_picture';
export const LOCATION = 'location'; export const LOCATION = 'location';
const userDatabaseKeys = [
PROFILE_PICTURE,
LOCATION,
];
export default userDatabaseKeys;

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

@ -1,7 +1,7 @@
import { UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions'; import { UPDATE_ORBIT_DATA } from '../actions/peerDbReplicationActions';
const initialState = { const initialState = {
fetchedPeerDatabases: [], users: [],
topics: [], topics: [],
posts: [], posts: [],
}; };
@ -10,10 +10,13 @@ const peerDbReplicationReducer = (state = initialState, action) => {
const { type } = action; const { type } = action;
if (type === UPDATE_ORBIT_DATA) { if (type === UPDATE_ORBIT_DATA) {
const { topics, posts } = action; const { users, topics, posts } = action;
return { return {
...state, ...state,
users: [
...users,
],
topics: [ topics: [
...topics, ...topics,
], ],

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

@ -9,7 +9,8 @@ import {
} from '@ezerous/breeze/src/orbit/orbitActions'; } from '@ezerous/breeze/src/orbit/orbitActions';
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 { POSTS_DATABASE, TOPICS_DATABASE } from '../../constants/OrbitDatabases'; import { POSTS_DATABASE, TOPICS_DATABASE, USER_DATABASE } from '../../constants/OrbitDatabases';
import userDatabaseKeys from '../../constants/UserDatabaseKeys';
function* fetchUserDb({ orbit, userAddress, dbName }) { function* fetchUserDb({ orbit, userAddress, dbName }) {
const peerDbAddress = yield call(determineKVAddress, { const peerDbAddress = yield call(determineKVAddress, {
@ -20,11 +21,39 @@ function* fetchUserDb({ orbit, userAddress, dbName }) {
} }
function* updateReduxState({ database }) { function* updateReduxState({ database }) {
const { topics, posts } = yield select((state) => ({ const { users, topics, posts } = yield select((state) => ({
users: state.orbitData.users,
topics: state.orbitData.topics, topics: state.orbitData.topics,
posts: state.orbitData.posts, posts: state.orbitData.posts,
})); }));
if (database.dbname === USER_DATABASE) {
const oldUsersUnchanged = users
.filter((user) => !database.id !== user.id);
yield put({
type: UPDATE_ORBIT_DATA,
users: [
...oldUsersUnchanged,
{
id: database.id,
// Don't ask how.. it just works
...Object
.entries(database.all)
.filter(([key]) => userDatabaseKeys.includes(key))
.reduce(((acc, keyValue) => {
const [key, value] = keyValue;
acc[key] = value;
return acc;
}), {}),
},
],
topics: [...topics],
posts: [...posts],
});
}
if (database.dbname === TOPICS_DATABASE) { if (database.dbname === TOPICS_DATABASE) {
const oldTopicsUnchanged = topics const oldTopicsUnchanged = topics
.filter((topic) => !Object .filter((topic) => !Object
@ -34,6 +63,7 @@ function* updateReduxState({ database }) {
yield put({ yield put({
type: UPDATE_ORBIT_DATA, type: UPDATE_ORBIT_DATA,
users: [...users],
topics: [ topics: [
...oldTopicsUnchanged, ...oldTopicsUnchanged,
...Object ...Object
@ -56,6 +86,7 @@ function* updateReduxState({ database }) {
yield put({ yield put({
type: UPDATE_ORBIT_DATA, type: UPDATE_ORBIT_DATA,
users: [...users],
topics: [...topics], topics: [...topics],
posts: [ posts: [
...oldPostsUnchanged, ...oldPostsUnchanged,

Loading…
Cancel
Save