Skip to content

Commit bbca43b

Browse files
committed
updated logic for following/unfollowing threads in place to match develop
1 parent d3e6221 commit bbca43b

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

app/assets/javascripts/comments.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,19 +355,25 @@ $(() => {
355355
return;
356356
}
357357

358-
const data = await QPixel.followComments(postId);
358+
const shouldFollow = action === 'follow';
359359

360-
QPixel.handleJSONResponse(data, () => {
361-
const isFollowing = action === 'follow';
360+
const data = shouldFollow ?
361+
await QPixel.followComments(postId) :
362+
await QPixel.unfollowComments(postId);
362363

363-
target.dataset.action = isFollowing ? 'unfollow' : 'follow';
364+
QPixel.handleJSONResponse(data, () => {
365+
target.dataset.action = shouldFollow ? 'unfollow' : 'follow';
364366

365367
const icon = document.createElement('i');
366-
icon.classList.add('fas', 'fa-fw', isFollowing ? 'fa-bell-slash' : 'fa-bell');
368+
icon.classList.add('fas', 'fa-fw', shouldFollow ? 'fa-bell-slash' : 'fa-bell');
369+
const text = document.createTextNode(` ${ shouldFollow ? 'Unfollow' : 'Follow' } new`);
370+
target.replaceChildren(icon, text);
367371

368-
const text = document.createTextNode(` ${ isFollowing ? 'Unfollow' : 'Follow' } new`);
372+
const form = target.closest('form');
369373

370-
target.replaceChildren(icon, text);
374+
if (form) {
375+
form.action = `/comments/post/${postId}/${shouldFollow ? 'unfollow' : 'follow'}`;
376+
}
371377
});
372378
});
373379
});

app/views/posts/_follow_comments_link.html.erb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@
66
user : User to check the following status for
77
"%>
88

9-
<%= form_tag follow_post_comments_path(post_id: post.id), method: :post do %>
10-
<% if post.followed_by?(user) %>
11-
<%= button_tag type: :submit,
12-
class: "button is-muted is-outlined is-small js-follow-comments",
13-
data: { post_id: post.id, action: 'unfollow' },
14-
title: 'Don\'t follow new comment threads on this post',
15-
'aria-label': 'Unfollow new comment threads on this post' do %>
16-
<i class="fas fa-fw fa-bell-slash"></i> Unfollow new
17-
<% end %>
18-
<% else %>
19-
<%= button_tag type: :submit,
20-
class: "button is-muted is-outlined is-small js-follow-comments",
21-
data: { post_id: post.id, action: 'follow' },
22-
title: 'Follow all new comment threads on this post',
23-
'aria-label': 'Follow all new comment threads on this post' do %>
24-
<i class="fas fa-fw fa-bell"></i> Follow new
25-
<% end %>
9+
<%
10+
is_followed = post.followed_by?(user)
11+
action_path = is_followed ?
12+
unfollow_post_comments_path(post_id: post.id) :
13+
follow_post_comments_path(post_id: post.id)
14+
text = "#{is_followed ? 'Unfollow' : 'Follow'} new"
15+
title = 'Switch following new comment threads on this post'
16+
%>
17+
18+
<%= form_tag action_path, method: :post do %>
19+
<%= button_tag type: :submit,
20+
class: "button is-muted is-outlined is-small js-follow-comments",
21+
data: { post_id: post.id, action: is_followed ? 'unfollow' : 'follow' },
22+
aria: { label: title },
23+
title: title do %>
24+
<i class="fas fa-fw <%= is_followed ? 'fa-bell-slash' : 'fa-bell' %>"></i> <%= text %>
2625
<% end %>
2726
<% end %>

0 commit comments

Comments
 (0)