-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pagination, replies, and styling improvements
- Loading branch information
Showing
6 changed files
with
613 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,159 @@ | ||
import React, { forwardRef, useState, useEffect } from "react"; | ||
import { formatDate, truncateText } from "../../utils/helpers"; | ||
import { apiReplies } from "../../utils/api"; | ||
import { faThumbsUp, faThumbsDown } from "@fortawesome/free-solid-svg-icons" | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' | ||
import { faThumbsUp, faThumbsDown, faComment } from "@fortawesome/free-solid-svg-icons"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
|
||
export const Comment = forwardRef(({ comment, isReply, isUserComment }, ref) => { | ||
const [showReplies, setShowReplies] = useState(false); | ||
const [replies, setReplies] = useState([]); | ||
const [seeMore, setSeeMore] = useState(true); | ||
export const Comment = forwardRef( | ||
({ comment, isUserComment, setReply }, ref) => { | ||
const [showReplies, setShowReplies] = useState(false); | ||
const [replies, setReplies] = useState([]); | ||
const [seeMore, setSeeMore] = useState(true); | ||
|
||
const [liked, setLiked] = useState(false); | ||
const [disliked, setDisliked] = useState(false); | ||
const [liked, setLiked] = useState(false); | ||
const [disliked, setDisliked] = useState(false); | ||
|
||
useEffect(() => { | ||
if (showReplies && replies.length === 0 && comment.replies > 0) { | ||
apiReplies(comment.id).then(res => { | ||
setReplies(res.replies); | ||
}); | ||
} | ||
}, [comment.id, comment.replies, replies.length, showReplies]); | ||
const indentation = comment.path.split(".").length; // Number of replies deep | ||
|
||
return ( | ||
<div className={`comment ${isReply ? "reply" : ""} ${isUserComment ? "user" : ""}`} ref={ref}> | ||
<div className="top"> | ||
<b>{isUserComment ? "You" : comment.author_name}</b> | ||
<sub>{formatDate(comment.modified_at)}</sub> | ||
</div> | ||
{seeMore ? ( | ||
<p>{comment.text}</p> | ||
) : ( | ||
<> | ||
<p>{comment.text ? truncateText(comment.text, 150) : ""}</p> | ||
useEffect(() => { | ||
if (showReplies && replies.length === 0 && comment.replies > 0) { | ||
apiReplies(comment.id).then((res) => { | ||
setReplies(res.replies); | ||
}); | ||
} | ||
}, [comment.id, comment.replies, replies.length, showReplies]); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
paddingLeft: indentation === 1 ? "0" : `${2 * (indentation - 1)}vw`, | ||
position: "relative", | ||
}} | ||
> | ||
{indentation > 1 && ( | ||
<div | ||
style={{ | ||
position: "absolute", | ||
left: `${2 * (indentation - 1)}vw`, | ||
top: 0, | ||
bottom: 0, | ||
width: "2px", | ||
backgroundColor: "#ccc", | ||
borderRadius: "1px", | ||
}} | ||
></div> | ||
)} | ||
<div | ||
className={`comment ${indentation !== 1 ? "reply" : ""} ${ | ||
isUserComment ? "user" : "" | ||
}`} | ||
ref={ref} | ||
> | ||
<div className="top"> | ||
<b>{isUserComment ? "You" : comment.author_name}</b> | ||
<sub>{formatDate(comment.modified_at)}</sub> | ||
</div> | ||
{seeMore ? ( | ||
<p style={{ marginLeft: "10px" }}>{comment.text}</p> | ||
) : ( | ||
<> | ||
<p style={{ marginLeft: "10px" }}> | ||
{comment.text ? truncateText(comment.text, 150) : ""} | ||
</p> | ||
<button | ||
className="btn-borderless btn" | ||
onClick={() => setSeeMore(true)} | ||
> | ||
See More | ||
</button> | ||
</> | ||
)} | ||
<div | ||
className="icon-wrapper" | ||
style={{ | ||
borderRadius: "50px", | ||
padding: "2px", | ||
display: "inline-block", | ||
transition: "background-color 0.3s", | ||
marginTop: "10px", | ||
}} | ||
> | ||
<button | ||
className=" btn-borderless btn" | ||
onClick={() => setSeeMore(true)} | ||
className={`btn icon ${liked ? "active" : ""}`} | ||
onClick={() => { | ||
setLiked(!liked); | ||
disliked && setDisliked(false); | ||
}} | ||
style={{ fontSize: "0.9em" }} | ||
> | ||
See More | ||
<FontAwesomeIcon icon={faThumbsUp} /> | ||
</button> | ||
</> | ||
)} | ||
<div className="icon-wrapper"> | ||
<button className={`btn icon ${liked ? "active" : ""}`} onClick={() => {setLiked(!liked); disliked && setDisliked(false)}}><FontAwesomeIcon icon={faThumbsUp} /></button> | ||
<span>{comment.votes + liked - disliked}</span> | ||
<button className={`btn icon ${disliked ? "active" : ""}`} onClick={() => {setDisliked(!disliked); liked && setLiked(false)}}><FontAwesomeIcon icon={faThumbsDown} /></button> | ||
</div> | ||
{comment.replies > 0 && ( | ||
<> | ||
<span style={{ fontSize: "0.9em" }}>{comment.votes + liked - disliked}</span> | ||
<button | ||
className="btn-borderless btn" | ||
onClick={() => setShowReplies(!showReplies)} | ||
className={`btn icon ${disliked ? "active" : ""}`} | ||
onClick={() => { | ||
setDisliked(!disliked); | ||
liked && setLiked(false); | ||
}} | ||
style={{ fontSize: "0.9em" }} | ||
> | ||
{showReplies ? "Hide" : "Show"} Replies | ||
<FontAwesomeIcon icon={faThumbsDown} /> | ||
</button> | ||
<div style={{ paddingLeft: isReply ? "0" : "2vw" }}> | ||
{showReplies && | ||
replies.map(reply => ( | ||
<Comment key={reply.id} comment={reply} isReply /> | ||
))} | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
); | ||
}); | ||
</div> | ||
<div | ||
className="icon-wrapper" | ||
style={{ | ||
borderRadius: "50px", | ||
padding: "2px", | ||
display: "inline-block", | ||
transition: "background-color 0.3s", | ||
marginTop: "10px", | ||
}} | ||
> | ||
{!isUserComment && ( | ||
<button | ||
className="btn icon" | ||
onClick={() => setReply(comment)} | ||
style={{ fontSize: "0.9em" }} | ||
> | ||
<FontAwesomeIcon icon={faComment} /> Reply | ||
</button> | ||
)} | ||
</div> | ||
{comment.replies > 0 && ( | ||
<> | ||
<button | ||
className="btn-borderless btn" | ||
onClick={() => setShowReplies(!showReplies)} | ||
> | ||
{showReplies ? "Hide" : "Show"} Replies | ||
</button> | ||
<div style={{ paddingLeft: indentation !== 1 ? "0" : "2vw" }}> | ||
{showReplies && | ||
replies.map((reply) => ( | ||
<Comment key={reply.id} comment={reply} /> | ||
))} | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} | ||
); | ||
|
||
// Add CSS for hover effect | ||
const styles = ` | ||
.icon-wrapper { | ||
background-color: transparent; | ||
} | ||
.icon-wrapper:hover { | ||
background-color: #f0f0f0; | ||
} | ||
`; | ||
|
||
// Inject styles into the document | ||
const styleSheet = document.createElement("style"); | ||
styleSheet.type = "text/css"; | ||
styleSheet.innerText = styles; | ||
document.head.appendChild(styleSheet); |
Oops, something went wrong.