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} + /> } - ); }