Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit b9d6521

Browse files
Merge pull request #101 from Sebastian-Webster/add-like-count-to-image-posts
add like count to image posts
2 parents e27edc5 + beef3a7 commit b9d6521

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

backend/controllers/User.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,9 @@ const getImagePostsByUserName = async (req, res) => {
439439
return
440440
}
441441

442-
ImagePost.findPostsByCreatorId(foundUserByName._id, limit, skip, publicId).then(result => {
442+
ImagePost.findPostsByCreatorId(foundUserByName._id, limit, skip).then(result => {
443443
//Get rid of object IDs
444-
const cleanedResult = result.map(post => ({title: post.title, body: post.body, datePosted: post.datePosted, imageKey: post.imageKey, liked: post.liked, postId: post._id, edited: post.editHistory.length > 0, timesEdited: post.editHistory.length, dateEdited: post.dateEdited}))
444+
const cleanedResult = ImagePost.prepareDataToSendToUserSync(result, false, publicId)
445445
http.OK(res, 'Successfully found posts', cleanedResult)
446446
}).catch(error => {
447447
http.ServerError(res, 'An error occured while fetching image posts. Please try again later.')

backend/libraries/ImagePost.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,9 @@ const User = require('./User')
33
const user = new User();
44

55
class ImagePostLibrary {
6-
findPostsByCreatorId = (creatorId, limit, skip, viewerId) => {
6+
findPostsByCreatorId = (creatorId, limit, skip) => {
77
return new Promise((resolve, reject) => {
8-
ImagePost.find({creatorId}).skip(skip).limit(limit).then(result => {
9-
const toResolve = result.map(item => {
10-
const toReturn = {
11-
...item._doc,
12-
liked: item.likes.includes(viewerId)
13-
}
14-
delete toReturn.likes
15-
return toReturn
16-
})
17-
resolve(toResolve)
18-
}).catch(error => {
19-
reject(error)
20-
})
8+
ImagePost.find({creatorId}).skip(skip).limit(limit).then(resolve).catch(reject)
219
})
2210
}
2311

@@ -85,8 +73,9 @@ class ImagePostLibrary {
8573

8674
prepareDataToSendToUserSync = (posts, cached, publicId) => {
8775
const tempArray = []
88-
for (const item of posts) {
89-
const {_id, creatorId, likes, __v, editHistory, ...cleanResult} = item;
76+
for (const itemIndex in posts) {
77+
const item = posts[itemIndex]
78+
const {_id, creatorId, likes, __v, editHistory, ...cleanResult} = item._doc || item; //If there is a ._doc, choose it over item
9079
cleanResult.liked = user.checkIfUserLikedPost(likes, publicId)
9180
if (typeof cleanResult.liked !== 'boolean') {
9281
throw new Error(`cleanResult.liked is not a boolean. It is actually type ${typeof cleanResult.liked} and it's value is ${cleanResult.liked}`)
@@ -95,6 +84,8 @@ class ImagePostLibrary {
9584
cleanResult.postId = _id;
9685
cleanResult.edited = item.editHistory.length > 0;
9786
cleanResult.timesEdited = item.editHistory.length;
87+
cleanResult.likeCount = likes.length;
88+
console.log(cleanResult)
9889
tempArray.push(cleanResult)
9990
}
10091
return tempArray

frontend/src/components/ImagePost.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Box from '@mui/material/Box'
1515
import CircularProgress from '@mui/material/CircularProgress'
1616
import useColorScheme from '../hooks/useColorScheme';
1717

18-
const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicId, postId, dispatch, userId, previewMode, profileName, profileImage, contextMenuPostId, contextMenuAnchorElement, editMode, saving, edited, timesEdited, dateEdited, isPostOwner}) => {
18+
const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicId, postId, dispatch, userId, previewMode, profileName, profileImage, contextMenuPostId, contextMenuAnchorElement, editMode, saving, edited, timesEdited, dateEdited, isPostOwner, likeCount}) => {
1919
const {darkMode, setDarkMode} = useContext(DarkModeContext)
2020
const changingLikeStatus = useRef(false)
2121
const deleting = useRef(false)
@@ -181,11 +181,14 @@ const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicI
181181
</>
182182
:
183183
<>
184-
<FontAwesomeIcon
185-
icon={liked ? fasHeart : farHeart}
186-
style={{color: liked ? 'red' : darkMode ? 'white' : 'black', cursor: 'pointer', fontSize: 30}}
187-
onClick={toggleLike}
188-
/>
184+
<div style={{display: 'flex', justifyContent: 'left', alignItems: 'center'}}>
185+
<FontAwesomeIcon
186+
icon={liked ? fasHeart : farHeart}
187+
style={{color: liked ? 'red' : darkMode ? 'white' : 'black', cursor: 'pointer', fontSize: 30}}
188+
onClick={toggleLike}
189+
/>
190+
<h3 style={{margin: 0, marginLeft: 10}}>{likeCount} {likeCount === 1 ? 'like' : 'likes'}</h3>
191+
</div>
189192
<h4 style={{marginTop: 10, marginBottom: 5}}>Posted {calculateDifferenceBetweenNowAndUTCMillisecondsTime(datePosted)}</h4>
190193
{edited && <h4 style={{margin: 0}}>Edited {calculateDifferenceBetweenNowAndUTCMillisecondsTime(dateEdited)}</h4>}
191194
</>

frontend/src/routes/Profile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ const Profile = () => {
260260

261261
const newPostsAfterLike = state.posts;
262262
newPostsAfterLike[likePostIndex].liked = true;
263+
newPostsAfterLike[likePostIndex].likeCount = newPostsAfterLike[likePostIndex].likeCount + 1;
263264

264265
return {...state, posts: newPostsAfterLike, reRenderTimes: state.reRenderTimes + 1}
265266
case 'unlikePost':
@@ -271,6 +272,7 @@ const Profile = () => {
271272

272273
const newPostsAfterUnlike = state.posts;
273274
newPostsAfterUnlike[unlikePostIndex].liked = false;
275+
newPostsAfterUnlike[unlikePostIndex].likeCount = newPostsAfterUnlike[unlikePostIndex].likeCount - 1;
274276

275277
return {...state, posts: newPostsAfterUnlike, reRenderTimes: state.reRenderTimes + 1}
276278
case 'deletePost':

0 commit comments

Comments
 (0)