From beef3a796ea6de8e03362d11625c59373377535e Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Mon, 19 Sep 2022 08:48:08 +1200 Subject: [PATCH] add like count to image posts --- backend/controllers/User.js | 4 ++-- backend/libraries/ImagePost.js | 23 +++++++---------------- frontend/src/components/ImagePost.js | 15 +++++++++------ frontend/src/routes/Profile.js | 2 ++ 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/backend/controllers/User.js b/backend/controllers/User.js index eca57b8..da895e2 100644 --- a/backend/controllers/User.js +++ b/backend/controllers/User.js @@ -439,9 +439,9 @@ const getImagePostsByUserName = async (req, res) => { return } - ImagePost.findPostsByCreatorId(foundUserByName._id, limit, skip, publicId).then(result => { + ImagePost.findPostsByCreatorId(foundUserByName._id, limit, skip).then(result => { //Get rid of object IDs - 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})) + const cleanedResult = ImagePost.prepareDataToSendToUserSync(result, false, publicId) http.OK(res, 'Successfully found posts', cleanedResult) }).catch(error => { http.ServerError(res, 'An error occured while fetching image posts. Please try again later.') diff --git a/backend/libraries/ImagePost.js b/backend/libraries/ImagePost.js index ed48e73..89d5aaf 100644 --- a/backend/libraries/ImagePost.js +++ b/backend/libraries/ImagePost.js @@ -3,21 +3,9 @@ const User = require('./User') const user = new User(); class ImagePostLibrary { - findPostsByCreatorId = (creatorId, limit, skip, viewerId) => { + findPostsByCreatorId = (creatorId, limit, skip) => { return new Promise((resolve, reject) => { - ImagePost.find({creatorId}).skip(skip).limit(limit).then(result => { - const toResolve = result.map(item => { - const toReturn = { - ...item._doc, - liked: item.likes.includes(viewerId) - } - delete toReturn.likes - return toReturn - }) - resolve(toResolve) - }).catch(error => { - reject(error) - }) + ImagePost.find({creatorId}).skip(skip).limit(limit).then(resolve).catch(reject) }) } @@ -85,8 +73,9 @@ class ImagePostLibrary { prepareDataToSendToUserSync = (posts, cached, publicId) => { const tempArray = [] - for (const item of posts) { - const {_id, creatorId, likes, __v, editHistory, ...cleanResult} = item; + for (const itemIndex in posts) { + const item = posts[itemIndex] + const {_id, creatorId, likes, __v, editHistory, ...cleanResult} = item._doc || item; //If there is a ._doc, choose it over item cleanResult.liked = user.checkIfUserLikedPost(likes, publicId) if (typeof cleanResult.liked !== 'boolean') { 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 { cleanResult.postId = _id; cleanResult.edited = item.editHistory.length > 0; cleanResult.timesEdited = item.editHistory.length; + cleanResult.likeCount = likes.length; + console.log(cleanResult) tempArray.push(cleanResult) } return tempArray diff --git a/frontend/src/components/ImagePost.js b/frontend/src/components/ImagePost.js index db0b8a4..9591d76 100644 --- a/frontend/src/components/ImagePost.js +++ b/frontend/src/components/ImagePost.js @@ -15,7 +15,7 @@ import Box from '@mui/material/Box' import CircularProgress from '@mui/material/CircularProgress' import useColorScheme from '../hooks/useColorScheme'; -const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicId, postId, dispatch, userId, previewMode, profileName, profileImage, contextMenuPostId, contextMenuAnchorElement, editMode, saving, edited, timesEdited, dateEdited, isPostOwner}) => { +const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicId, postId, dispatch, userId, previewMode, profileName, profileImage, contextMenuPostId, contextMenuAnchorElement, editMode, saving, edited, timesEdited, dateEdited, isPostOwner, likeCount}) => { const {darkMode, setDarkMode} = useContext(DarkModeContext) const changingLikeStatus = useRef(false) const deleting = useRef(false) @@ -181,11 +181,14 @@ const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicI : <> - +
+ +

{likeCount} {likeCount === 1 ? 'like' : 'likes'}

+

Posted {calculateDifferenceBetweenNowAndUTCMillisecondsTime(datePosted)}

{edited &&

Edited {calculateDifferenceBetweenNowAndUTCMillisecondsTime(dateEdited)}

} diff --git a/frontend/src/routes/Profile.js b/frontend/src/routes/Profile.js index 2125a3c..3133b7a 100644 --- a/frontend/src/routes/Profile.js +++ b/frontend/src/routes/Profile.js @@ -260,6 +260,7 @@ const Profile = () => { const newPostsAfterLike = state.posts; newPostsAfterLike[likePostIndex].liked = true; + newPostsAfterLike[likePostIndex].likeCount = newPostsAfterLike[likePostIndex].likeCount + 1; return {...state, posts: newPostsAfterLike, reRenderTimes: state.reRenderTimes + 1} case 'unlikePost': @@ -271,6 +272,7 @@ const Profile = () => { const newPostsAfterUnlike = state.posts; newPostsAfterUnlike[unlikePostIndex].liked = false; + newPostsAfterUnlike[unlikePostIndex].likeCount = newPostsAfterUnlike[unlikePostIndex].likeCount - 1; return {...state, posts: newPostsAfterUnlike, reRenderTimes: state.reRenderTimes + 1} case 'deletePost':