-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommentCard.jsx
129 lines (121 loc) · 2.97 KB
/
CommentCard.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { formatDate } from "./utils";
import { useContext, useState } from "react";
import { UserContext } from "./contexts/UserContext";
import {
updateVoteForArticleComment,
deleteCommentWithId,
} from "./apiFunctions";
const CommentCard = ({
comment,
authorAvatars,
setComments,
toBeDeleted,
setMarkedForDeletion,
}) => {
const { user } = useContext(UserContext);
const [likes, setLikes] = useState(comment.votes);
const userIsAuthor = () => user.username === comment.author;
const showVoteButtons = user && !userIsAuthor();
const showDeleteButton = user && userIsAuthor();
const adjustVote = (inc) => {
setLikes((currentLikes) => currentLikes + inc);
updateVoteForArticleComment(comment.comment_id, inc).catch(() => {
setLikes((currentLikes) => currentLikes - inc);
});
};
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];
comment.error = "Message deletion failed";
newComments.splice(commentIndex, 0, comment);
return newComments;
});
});
};
return (
<div
className={
toBeDeleted || comment.error
? "commentCardDelete"
: "commentCard"
}
>
<img
src={authorAvatars[comment.author]}
alt={comment.author}
className="commentAvatar"
/>
{comment.error ? (
<p className="commentDeleteFailLabel">
Message failed to delete
</p>
) : (
<p className="commentAuthor">{comment.author}</p>
)}
{showVoteButtons && (
<div className="commentVoteControls">
<button
className="voteButton"
id="voteUp"
disabled={userIsAuthor() ? true : false}
onClick={() => adjustVote(1)}
>
ᐱ
</button>
<button
className="voteButton"
id="voteDown"
disabled={userIsAuthor() ? true : false}
onClick={() => adjustVote(-1)}
>
ᐯ
</button>
</div>
)}
{showDeleteButton && (
<div className="commentVoteControls">
<button
className={`brandedButton ${
toBeDeleted ? "cancelRemoveButton" : "removeButton"
}`}
onClick={confirmRemoval}
>
{toBeDeleted ? "Cancel" : "Delete"}
</button>
</div>
)}
{toBeDeleted ? (
<button
className="brandedButton removeButton"
onClick={removeComment}
>
Delete
</button>
) : (
<p className="commentVotes">{likes} likes</p>
)}
<p className="commentBody">{comment.body}</p>
<p className="commentDate">{formatDate(comment.created_at)}</p>
</div>
);
};
export default CommentCard;