From 035f2b7d7fc92c00ac4fbf2ed3d74a6d2b87c49a Mon Sep 17 00:00:00 2001 From: aeroreyna Date: Tue, 8 Mar 2022 20:53:11 -0800 Subject: [PATCH] Likes working --- components/Post.js | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/components/Post.js b/components/Post.js index 50de76c..0ebff27 100644 --- a/components/Post.js +++ b/components/Post.js @@ -12,15 +12,30 @@ let Post = (props) => { const viewer = props.viewer; let [showCommentsB, changeshowCommentsB] = useState(false); let [post, changePost] = useState(props.post); + let [likes, changeLikes] = useState(Object.keys(post.reactions).length) let toProfileText = post.toProfile && post.toProfile !== post.profileid ? {">"} : undefined; let cleanContent = post.content.replace(/@[A-z]+:.+\w/g, ''); const newComentAdded = (commentData) => { - //add to comments let newPostObj = { ...post }; newPostObj.comments.push(commentData); changePost(newPostObj); }; + const newPostReaction = () => { + let newPostObj = { ...post }; + if (!newPostObj.reactions[viewer._id]) { + API.newPostReaction(post._id).then(() => { + newPostObj.reactions[viewer._id] = { type: "like" }; + changeLikes(likes+1); + }); + } else { + API.removePostReaction(viewer._id).then(() => { + delete newPostObj.reactions[viewer._id]; + changeLikes(likes-1); + }); + } + changePost(newPostObj); + } const renderComment = ({ item }) => ( ); @@ -36,20 +51,28 @@ let Post = (props) => { - + {showCommentsB && } { - showCommentsB && item.createdAt} - /> + showCommentsB && + + item.createdAt} + /> } - ); }