From 6e7df263fbd9cb14808472977708bd2c25c5e8cf Mon Sep 17 00:00:00 2001 From: SandraBergstrom Date: Mon, 26 Jun 2023 10:57:37 +0000 Subject: [PATCH] #20 Add like/unlike post --- src/pages/posts/Post.js | 46 +++++++++++++++++++++++++++++---- src/styles/Post.module.css | 52 +++++++++++++++++++++----------------- 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/src/pages/posts/Post.js b/src/pages/posts/Post.js index 5b54165..e7b6c7d 100644 --- a/src/pages/posts/Post.js +++ b/src/pages/posts/Post.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, {useEffect} from "react"; import { useCurrentUser } from "../../contexts/CurrentUserContext"; import styles from "../../styles/Post.module.css"; @@ -9,6 +9,7 @@ import { Link } from "react-router-dom"; import Avatar from "../../components/Avatar"; import OverlayTrigger from "react-bootstrap/OverlayTrigger"; import Tooltip from "react-bootstrap/Tooltip"; +import { axiosReq, axiosRes } from "../../api/axiosDefaults"; const Post = (props) => { const { @@ -24,15 +25,50 @@ const Post = (props) => { image, updated_at, postPage, + setPosts, } = props; + console.log(likes_count) + const currentUser = useCurrentUser(); const is_owner = currentUser?.username === owner; + const handleLike = async () => { + try { + const { data } = await axiosReq.post("/likes/", { post: id }); + setPosts((prevPosts) => ({ + ...prevPosts, + results: prevPosts.results.map((post) => { + return post.id === id + ? { ...post, likes_count: post.likes_count + 1, like_id: data.id } + : post; + }), + })); + } catch (err) { + console.log(err); + } + }; + + const handleUnlike = async () => { + try { + await axiosReq.delete(`/likes/${like_id}/`); + setPosts((prevPosts) => ({ + ...prevPosts, + results: prevPosts.results.map((post) => { + return post.id === id + ? { ...post, likes_count: post.likes_count - 1, like_id: null } + : post; + }), + })); + } catch (err) { + console.log(err); + } + }; + return ( - + {owner} @@ -58,11 +94,11 @@ const Post = (props) => { ) : like_id ? ( - {}}> + ) : currentUser ? ( - {}}> + ) : ( @@ -84,4 +120,4 @@ const Post = (props) => { ); }; -export default Post; \ No newline at end of file +export default Post; diff --git a/src/styles/Post.module.css b/src/styles/Post.module.css index 775d128..835a242 100644 --- a/src/styles/Post.module.css +++ b/src/styles/Post.module.css @@ -1,24 +1,30 @@ .Post { - background-color: #ffffff; - border: 1px solid #dadadf; - border-radius: 2px; - margin-bottom: 16px; - text-align: center; - } - - .Post figure, - h5 { - margin: 0 0; - } - - .Heart { - color: #f85032; - } - - .Heart:hover { - color: #2142b2; - } - - .HeartOutline:hover { - color: #f85032; - } \ No newline at end of file + background-color: white; + border: 1px solid #dadadf; + border-radius: 2px; + margin-bottom: 16px; + text-align: center; +} + +/* .PostImage { + max-height: 600px; + width: auto; + object-fit: cover; +} */ + +.Post figure, +h5 { + margin: 0 0; +} + +.Heart { + color: var(--clr-accent-pink); +} + +.Heart:hover { + color: var(--clr-accent-dark); +} + +.HeartOutline:hover { + color: var(--clr-accent-pink); +}