posts: make post votes work the same way as comment votes.
Change post votes to work the same way as comment votes: * Make the upvote arrow blue if you've upvoted the post, or grey if you haven't. Likewise for the downvote arrow. * Make it so you can click the upvote or downvote arrows to undo the vote. * Don't show any notices when you vote on a post. Also fix it so that votes work the same way on the posts page, the comments page, and in the modqueue. Before it wasn't possible to undo votes on the comments page or in the modqueue.
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
class ApplicationComponent < ViewComponent::Base
|
||||
delegate :link_to_user, :time_ago_in_words_tagged, :format_text, :edit_icon,
|
||||
:delete_icon, :undelete_icon, :flag_icon, :upvote_icon, :downvote_icon,
|
||||
:link_icon, to: :helpers
|
||||
|
||||
def policy(subject)
|
||||
Pundit.policy!(current_user, subject)
|
||||
end
|
||||
|
||||
27
app/components/post_votes_component.rb
Normal file
27
app/components/post_votes_component.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# This component represents the post score and upvote/downvote buttons ("🠉 5 🠋")
|
||||
class PostVotesComponent < ApplicationComponent
|
||||
attr_reader :post, :current_user
|
||||
|
||||
def initialize(post:, current_user:)
|
||||
@post = post
|
||||
@current_user = current_user
|
||||
end
|
||||
|
||||
def can_vote?
|
||||
policy(PostVote).create?
|
||||
end
|
||||
|
||||
def current_vote
|
||||
post.votes.find_by(user: current_user)
|
||||
end
|
||||
|
||||
def upvoted?
|
||||
can_vote? && current_vote&.is_positive?
|
||||
end
|
||||
|
||||
def downvoted?
|
||||
can_vote? && current_vote&.is_negative?
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
<span class="post-votes" data-id="<%= post.id %>">
|
||||
<% if can_vote? %>
|
||||
<% if upvoted? %>
|
||||
<%= link_to upvote_icon, post_post_votes_path(post_id: post.id), class: "post-upvote-link post-unvote-link active-link", method: :delete, remote: true %>
|
||||
<% else %>
|
||||
<%= link_to upvote_icon, post_post_votes_path(post_id: post.id, score: "up"), class: "post-upvote-link inactive-link", method: :post, remote: true %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<span class="post-score"><%= post.score %></span>
|
||||
|
||||
<% if can_vote? %>
|
||||
<% if downvoted? %>
|
||||
<%= link_to downvote_icon, post_post_votes_path(post_id: post.id), class: "post-downvote-link post-unvote-link active-link", method: :delete, remote: true %>
|
||||
<% else %>
|
||||
<%= link_to downvote_icon, post_post_votes_path(post_id: post.id, score: "down"), class: "post-downvote-link inactive-link", method: :post, remote: true %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</span>
|
||||
@@ -0,0 +1,10 @@
|
||||
.post-votes {
|
||||
// Fix it so that the vote buttons don't move when the score changes width.
|
||||
// XXX duplicated from app/components/comment_component/comment_component.scss
|
||||
.post-score {
|
||||
display: inline-block;
|
||||
text-align: center;
|
||||
min-width: 1.25em;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user