wired up vote ux

This commit is contained in:
Mark Moser 2016-11-20 11:24:17 -06:00
parent 5ef7f82dbf
commit 74b2415b91
6 changed files with 75 additions and 22 deletions

View File

@ -1,7 +1,8 @@
function handleAjaxResponse($el) {
function handleAjaxResponse($el, callback) {
var $header = $('header');
$el.on("ajax:success", function(e, data){
$header.after('<div class="success">' + data.message + '</div>');
callback(data);
}).on("ajax:error", function(e, xhr) {
if (xhr.status === 400){
$header.after('<div class="error">' + xhr.responseJSON.join('<br>') + '</div>');
@ -11,6 +12,25 @@ function handleAjaxResponse($el) {
});
}
function updateVotes(data){
$("[data-id=up-votes]").html(data.upCount);
$("[data-id=down-votes]").html(data.downCount);
$("[data-id=my-vote]").html(data.myVote);
}
function updateVeto(data){
$("[data-id=interview-request]").html(data.requestCopy);
$("[data-id=interview-decline]").html(data.declineCopy);
}
$(document).ready(function() {
$('[data-id=ajax-action]').each(function(){ handleAjaxResponse($(this)); });
});
$(document).ready(function() {
$('[data-id=vote-count]').each(function(){ handleAjaxResponse($(this), updateVotes); });
});
$(document).ready(function() {
$('[data-id=veto-status]').each(function(){ handleAjaxResponse($(this), updateVeto); });
});

View File

@ -7,9 +7,10 @@ module Admin
current_user.cast_yea_on(@candidate)
results = {
message: "Vote tallied!",
message: "Vote Counted",
upCount: @candidate.votes.yea.count,
downCount: @candidate.votes.nay.count
downCount: @candidate.votes.nay.count,
myVote: "yea"
}
render json: results.to_json
end
@ -20,9 +21,10 @@ module Admin
current_user.cast_nay_on(@candidate)
results = {
message: "Vote tallied!",
message: "Vote Counted",
upCount: @candidate.votes.yea.count,
downCount: @candidate.votes.nay.count
downCount: @candidate.votes.nay.count,
myVote: "nay"
}
render json: results.to_json
end
@ -32,7 +34,11 @@ module Admin
@candidate = Candidate.find_by(test_hash: params[:test_hash])
current_user.approve_candidate(@candidate)
results = { message: "Interview requested!" }
results = {
message: "Interview requested!",
requestCopy: "Requested",
declineCopy: "Decline Interview"
}
render json: results.to_json
end
@ -41,7 +47,11 @@ module Admin
@candidate = Candidate.find_by(test_hash: params[:test_hash])
current_user.decline_candidate(@candidate)
results = { message: "Interview declined." }
results = {
message: "Interview declined.",
requestCopy: "Request Interview",
declineCopy: "Declined"
}
render json: results.to_json
end
end

View File

@ -17,7 +17,7 @@ class Candidate < ApplicationRecord
validates :test_hash, uniqueness: true, presence: true
enum review_status: {
# pending: 0,
pending: 0,
approved: 1,
declined: 2
}

View File

@ -47,6 +47,13 @@ class User < ApplicationRecord
candidate.update_attribute(:review_status, :declined) if vote.save
end
def my_vote candidate
candidate = Candidate.find(candidate.to_i)
my_votes = votes.find_by(candidate_id: candidate.id)
my_votes.nil? ? "undecided" : my_votes.votes
end
# Roles
def admin?
'admin' == role

View File

@ -8,24 +8,33 @@
<strong>Recruiter Email:</strong> <%= mail_to @candidate.recruiter.name, @candidate.recruiter.email %><br />
</div>
<div>
<div class="review_meta__votes">
<div class="review_meta__votes" data-id="vote-count">
<strong>Votes: </strong>
<%= link_to '#cast-vote-up', remote: true do %>
Yea (<%= @candidate.votes.yea.count %>)
<%= link_to admin_up_vote_path(test_hash: @candidate.test_hash), remote: true do %>
Yea (<span data-id="up-votes"><%= @candidate.votes.yea.count %></span>)
<% end %>
<%= link_to '#cast-vote-down', remote: true do %>
Nay (<%= @candidate.votes.nay.count %>)
<% end %>
</div>
<div class="review_meta__vetos">
<strong>Manager Vetos: </strong>
<%= link_to '#request', remote: true do %>
Request Interview
<% end %>
<%= link_to '#decline', remote: true do %>
Decline Interview
<%= link_to admin_down_vote_path(test_hash: @candidate.test_hash), remote: true do %>
Nay (<span data-id="down-votes"><%= @candidate.votes.nay.count %></span>)
<% end %>
<small>(Your vote: <span data-id="my-vote"><%= current_user.my_vote(@candidate) %></span>)</small>
</div>
<% if current_user.acts_as_manager? %>
<div class="review_meta__vetos" data-id="veto-status">
<strong>Manager Vetos: </strong>
<%= link_to admin_approve_vote_path(test_hash: @candidate.test_hash), remote: true do %>
<span data-id="interview-request">
<%= @candidate.approved? ? "Requested" : "Request Interview" %>
</span>
<% end %>
<%= link_to admin_decline_vote_path(test_hash: @candidate.test_hash), remote: true do %>
<span data-id="interview-decline">
<%= @candidate.declined? ? "Declined" : "Decline Interview" %>
</span>
<% end %>
</div>
<% else %>
<strong>Candidate Interview Status: </strong><%= @candidate.review_status %>
<% end %>
</div>
</div>

View File

@ -19,5 +19,12 @@ module Admin
assert assigns(:quiz), "@quiz not present"
assert assigns(:status), "@status not present"
end
test "reviewer can view result for henry" do
auth_reviewer
get admin_result_url(candidates(:henry).test_hash)
assert_response :success
end
end
end