Skip to content

Commit c24f00c

Browse files
committed
support liking a comment
1 parent f3aa1ca commit c24f00c

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

src/gitment.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class Gitment {
7979
meta: {},
8080
comments: undefined,
8181
reactions: [],
82+
commentReactions: {},
8283
currentPage: 1,
8384
})
8485

@@ -133,7 +134,10 @@ class Gitment {
133134

134135
update() {
135136
return Promise.all([this.loadMeta(), this.loadUserInfo()])
136-
.then(() => Promise.all([this.loadComments(), this.loadReactions()]))
137+
.then(() => Promise.all([
138+
this.loadComments().then(() => this.loadCommentReactions()),
139+
this.loadReactions(),
140+
]))
137141
.catch(e => this.state.error = e)
138142
}
139143

@@ -212,7 +216,10 @@ class Gitment {
212216
}
213217

214218
loadReactions() {
215-
if (!this.accessToken) return Promise.resolve([])
219+
if (!this.accessToken) {
220+
this.state.reactions = []
221+
return Promise.resolve([])
222+
}
216223

217224
return this.getIssue()
218225
.then((issue) => {
@@ -225,6 +232,31 @@ class Gitment {
225232
})
226233
}
227234

235+
loadCommentReactions() {
236+
if (!this.accessToken) {
237+
this.state.commentReactions = {}
238+
return Promise.resolve([])
239+
}
240+
241+
const comments = this.state.comments
242+
const comentReactions = {}
243+
244+
return Promise.all(comments.map((comment) => {
245+
if (!comment.reactions.heart) return []
246+
247+
const { owner, repo } = this
248+
return http.get(`/repos/${owner}/${repo}/issues/comments/${comment.id}/reactions`, { content: 'heart' })
249+
}))
250+
.then(reactionsArray => {
251+
comments.forEach((comment, index) => {
252+
comentReactions[comment.id] = reactionsArray[index]
253+
})
254+
this.state.commentReactions = comentReactions
255+
256+
return comentReactions
257+
})
258+
}
259+
228260
login() {
229261
window.location.href = this.loginLink
230262
}
@@ -270,6 +302,39 @@ class Gitment {
270302
this.state.meta.reactions.heart--
271303
})
272304
}
305+
306+
likeAComment(commentId) {
307+
if (!this.accessToken) {
308+
alert('Login to Like')
309+
return Promise.reject()
310+
}
311+
312+
const { owner, repo } = this
313+
const comment = this.state.comments.find(comment => comment.id === commentId)
314+
315+
return http.post(`/repos/${owner}/${repo}/issues/comments/${commentId}/reactions`, {
316+
content: 'heart',
317+
})
318+
.then(reaction => {
319+
this.state.commentReactions[commentId].push(reaction)
320+
comment.reactions.heart++
321+
})
322+
}
323+
324+
unlikeAComment(commentId) {
325+
if (!this.accessToken) return Promise.reject()
326+
327+
const reactions = this.state.commentReactions[commentId]
328+
const comment = this.state.comments.find(comment => comment.id === commentId)
329+
const { user } = this.state
330+
const index = reactions.findIndex(reaction => reaction.user.login === user.login)
331+
332+
return http.delete(`/reactions/${reactions[index].id}`)
333+
.then(() => {
334+
reactions.splice(index, 1)
335+
comment.reactions.heart--
336+
})
337+
}
273338
}
274339

275340
module.exports = Gitment

src/theme/default.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function renderHeader({ meta, user, reactions }, instance) {
3838
return container
3939
}
4040

41-
function renderComments({ meta, comments, currentPage, user, error }, instance) {
41+
function renderComments({ meta, comments, commentReactions, currentPage, user, error }, instance) {
4242
const container = document.createElement('div')
4343
container.className = 'gitment-container gitment-comments-container'
4444

@@ -104,10 +104,21 @@ function renderComments({ meta, comments, currentPage, user, error }, instance)
104104
? ` • <span title="comment was edited at ${updateDate}">edited</span>`
105105
: ''
106106
}
107+
<div class="gitment-comment-like-btn">${heartIcon} ${comment.reactions.heart || ''}</div>
107108
</div>
108109
<div class="gitment-comment-body gitment-markdown">${comment.body_html}</div>
109110
</div>
110111
`
112+
const likeButton = commentItem.querySelector('.gitment-comment-like-btn')
113+
const likedReaction = commentReactions[comment.id]
114+
&& commentReactions[comment.id].find(reaction => reaction.user.login === user.login)
115+
if (likedReaction) {
116+
likeButton.classList.add('liked')
117+
likeButton.onclick = () => instance.unlikeAComment(comment.id)
118+
} else {
119+
likeButton.classList.remove('liked')
120+
likeButton.onclick = () => instance.likeAComment(comment.id)
121+
}
111122
commentsList.appendChild(commentItem)
112123
})
113124

style/default.css

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,27 @@
5858
border-bottom: 2px solid #e6ebf1;
5959
}
6060

61-
.gitment-header-like-btn {
61+
.gitment-header-like-btn,
62+
.gitment-comment-like-btn {
6263
cursor: pointer;
6364
}
6465

66+
.gitment-comment-like-btn {
67+
float: right;
68+
}
69+
6570
.gitment-header-like-btn svg {
6671
vertical-align: middle;
6772
height: 30px;
6873
}
6974

70-
.gitment-header-like-btn.liked svg {
75+
.gitment-comment-like-btn svg {
76+
vertical-align: middle;
77+
height: 20px;
78+
}
79+
80+
.gitment-header-like-btn.liked svg,
81+
.gitment-comment-like-btn.liked svg {
7182
fill: #f05f70;
7283
}
7384

@@ -416,6 +427,7 @@ a.gitment-editor-footer-tip:hover {
416427
}
417428

418429
.gitment-footer-container {
430+
margin: 19px 0;
419431
text-align: right;
420432
}
421433

0 commit comments

Comments
 (0)