diff --git a/src/pages/comments/Comment.js b/src/pages/comments/Comment.js index 3aca455..6e9cb3c 100644 --- a/src/pages/comments/Comment.js +++ b/src/pages/comments/Comment.js @@ -1,6 +1,7 @@ import React, { useState } from "react"; import styles from "../../styles/Comment.module.css"; +import btnStyles from "../../styles/Button.module.css"; import CommentEditForm from "./CommentEditForm"; import { Link } from "react-router-dom/"; @@ -8,7 +9,10 @@ import { Media } from "react-bootstrap"; import Avatar from "../../components/Avatar"; import { useCurrentUser } from "../../contexts/CurrentUserContext"; import { MoreDropdown } from "../../components/MoreDropdown"; -import { axiosRes } from "../../api/axiosDefaults"; +import { axiosRes, axiosReq } from "../../api/axiosDefaults"; + +import OverlayTrigger from "react-bootstrap/OverlayTrigger"; +import Tooltip from "react-bootstrap/Tooltip"; const Comment = (props) => { const { @@ -20,6 +24,8 @@ const Comment = (props) => { id, setPost, setComments, + likes_count, + like_id, } = props; const [showEditForm, setShowEditForm] = useState(false); @@ -46,6 +52,46 @@ const Comment = (props) => { } catch (err) {} }; + const handleLike = async () => { + try { + const { data } = await axiosReq.post("/likes/", { comment: id }); + setComments((prevComments) => ({ + ...prevComments, + results: prevComments.results.map((comment) => { + return comment.id === id + ? { + ...comment, + likes_count: comment.likes_count + 1, + like_id: data.id, + } + : comment; + }), + })); + } catch (err) { + console.log(err); + } + }; + + const handleUnlike = async () => { + try { + await axiosReq.delete(`/likes/${like_id}/`); + setComments((prevComments) => ({ + ...prevComments, + results: prevComments.results.map((comment) => { + return comment.id === id + ? { + ...comment, + likes_count: comment.likes_count - 1, + like_id: null, + } + : comment; + }), + })); + } catch (err) { + console.log(err); + } + }; + return (

@@ -68,7 +114,39 @@ const Comment = (props) => { setShowEditForm={setShowEditForm} /> ) : ( -

{content}

+
+

{content}

+
+ {likes_count} + {is_owner ? ( + You can't like your own comment! + } + > + + + ) : like_id ? ( + + + + ) : currentUser ? ( + + + + ) : ( + Log in to like comments!} + > + + + )} +
+
)} {is_owner && !showEditForm && (