diff --git a/src/App.css b/src/App.css index 1ba3f75..2b4d318 100644 --- a/src/App.css +++ b/src/App.css @@ -563,6 +563,14 @@ select { } .removeButton { + margin: auto; + border-width: 1px; + border-radius: 5px; + padding: 2px 4px 2px 4px; +} + +.cancelRemoveButton { + margin: auto; border-width: 1px; border-radius: 5px; padding: 2px 4px 2px 4px; @@ -573,6 +581,14 @@ select { box-shadow: 0px 0px 3px red; } +.commentDeleteFailLabel { + font-family: arial; + font-weight: bold; + text-align: center; + line-height: 40px; + color: red; +} + #errorBox { display: grid; grid-template-columns: auto; @@ -634,6 +650,23 @@ select { max-width: 550px; } +.commentCardDelete { + display: grid; + grid-template-columns: 40px auto 100px 100px; + grid-template-rows: 40px auto 25px; + grid-template-areas: + "commentAvatar commentAuthor commentVoteControls commentVotes" + "commentBody commentBody commentBody commentBody" + "commentDate commentDate commentDate commentDate"; + background-color: pink; + border: 1px solid #a2baab; + border-radius: 10px; + margin: 10px auto 10px auto; + box-shadow: 0px 0px 3px red; + width: calc(100vw - 20px); + max-width: 550px; +} + .commentAvatar { grid-area: commentAvatar; width: 30px; diff --git a/src/CommentCard.jsx b/src/CommentCard.jsx index 7904495..5a7cefc 100644 --- a/src/CommentCard.jsx +++ b/src/CommentCard.jsx @@ -6,7 +6,13 @@ import { deleteCommentWithId, } from "./apiFunctions"; -const CommentCard = ({ comment, authorAvatars, setComments }) => { +const CommentCard = ({ + comment, + authorAvatars, + setComments, + toBeDeleted, + setMarkedForDeletion, +}) => { const { user } = useContext(UserContext); const [likes, setLikes] = useState(comment.votes); @@ -21,36 +27,56 @@ const CommentCard = ({ comment, authorAvatars, setComments }) => { }); }; - const removeComment = (event) => { - if (window.confirm("Permanently remove comment?")) { - let commentIndex = 0; - setComments((currentComments) => { - return currentComments.filter((item, index) => { - if (item.comment_id === comment.comment_id) { - commentIndex = index; - return false; - } - return true; - }); + const confirmRemoval = () => { + if (toBeDeleted) { + setMarkedForDeletion(null); + } else { + setMarkedForDeletion(comment.comment_id); + } + }; + + const removeComment = () => { + setMarkedForDeletion(null); + let commentIndex = 0; + setComments((currentComments) => { + return currentComments.filter((item, index) => { + if (item.comment_id === comment.comment_id) { + commentIndex = index; + return false; + } + return true; }); - deleteCommentWithId(comment.comment_id).catch(() => { - setComments((currentComments) => { - const newComments = [...currentComments]; - newComments.splice(commentIndex, 0, comment); - return newComments; - }); + }); + deleteCommentWithId(comment.comment_id).catch(() => { + setComments((currentComments) => { + const newComments = [...currentComments]; + comment.error = "Message deletion failed"; + newComments.splice(commentIndex, 0, comment); + return newComments; }); - } + }); }; return ( -
+
{comment.author} -

{comment.author}

+ {comment.error ? ( +

+ Message failed to delete +

+ ) : ( +

{comment.author}

+ )} {showVoteButtons && (
@@ -75,14 +101,25 @@ const CommentCard = ({ comment, authorAvatars, setComments }) => { {showDeleteButton && (
)} -

{likes} likes

+ {toBeDeleted ? ( + + ) : ( +

{likes} likes

+ )}

{comment.body}

{formatDate(comment.created_at)}

diff --git a/src/CommentHistory.jsx b/src/CommentHistory.jsx index 55588ac..b941255 100644 --- a/src/CommentHistory.jsx +++ b/src/CommentHistory.jsx @@ -1,4 +1,5 @@ import CommentCard from "./CommentCard"; +import { useState } from "react"; const CommentHistory = ({ comments, @@ -6,6 +7,8 @@ const CommentHistory = ({ commentCount, setComments, }) => { + const [markedForDeletion, setMarkedForDeletion] = useState(null); + return comments.length === 0 ? (

No comments

) : ( @@ -17,6 +20,8 @@ const CommentHistory = ({ authorAvatars={authorAvatars} key={comment.created_at} setComments={setComments} + toBeDeleted={markedForDeletion === comment.comment_id} + setMarkedForDeletion={setMarkedForDeletion} /> ))}
diff --git a/src/FilterBar.jsx b/src/FilterBar.jsx index f8736ab..d7578a1 100644 --- a/src/FilterBar.jsx +++ b/src/FilterBar.jsx @@ -135,7 +135,7 @@ const FilterBar = () => { type="text" id="authorSearchField" placeholder="All authors" - autocomplete="off" + autoComplete="off" value={authorValue} onChange={authorChanged} />