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.error ? ( ++ Message failed to delete +
+ ) : ( +{comment.author}
+ )} {showVoteButtons && ({likes} likes
+ {toBeDeleted ? ( + + ) : ( +{likes} likes
+ )}{comment.body}
{formatDate(comment.created_at)}