Improve dropdown UX for internal/badges (#5880) [deploy]

* Improve dropdown UX for internal/badges

Add a "blank" option to the dropdown to list of badges on internal/badges route.
Also add some checks to handle if an admin accidentally tries to award a "blank" badge.

* Ensure error is surfaced as flash message
This commit is contained in:
Vaidehi Joshi 2020-02-04 09:02:14 -08:00 committed by GitHub
parent b09a0c60f3
commit 267e08f6f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 4 deletions

View file

@ -6,12 +6,16 @@ class Internal::BadgesController < Internal::ApplicationController
end
def award_badges
raise ArgumentError, "Please choose a badge to award" if permitted_params[:badge].blank?
usernames = permitted_params[:usernames].split(/\s*,\s*/)
badge_slug = permitted_params[:badge]
message = permitted_params[:message_markdown].presence || "Congrats!"
BadgeRewarder.award_badges(usernames, badge_slug, message)
BadgeRewarder.award_badges(usernames, permitted_params[:badge], message)
flash[:success] = "BadgeRewarder task ran!"
redirect_to internal_badges_url
rescue ArgumentError => e
flash[:danger] = e.message
redirect_to "/internal/badges"
end
private

View file

@ -16,7 +16,7 @@ class Internal::ToolsController < Internal::ApplicationController
end
redirect_to "/internal/tools"
rescue StandardError => e
flash[:danger] = e
flash[:danger] = e.message
redirect_to "/internal/tools"
end

View file

@ -3,7 +3,7 @@
<%= form_with(url: internal_badges_award_badges_path, local: true) do |f| %>
<div class="form-group">
<%= f.label :badge, "Badge" %>
<%= f.select("badge", @badges.map { |b| [b.title, b.slug] }, class: "form-control") %>
<%= f.select("badge", @badges.map { |b| [b.title, b.slug] }, include_blank: true, class: "form-control") %>
</div>
<div class="form-group">
<%= f.label :usernames, "Usernames (Comma Delimited)*" %>

View file

@ -30,5 +30,14 @@ RSpec.describe "/internal/badges", type: :request do
}
end.to change { user.badges.count }.by(1).and change { user2.badges.count }.by(1)
end
it "does not award a badge and raises an error if a badge is not specified" do
expect do
post internal_badges_award_badges_path, params: {
usernames: "#{user.username}, #{user2.username}",
message_markdown: ""
}
end.to change { user.badges.count }.by(0)
end
end
end

View file

@ -13,6 +13,12 @@ RSpec.describe "Admin awards badges", type: :system do
click_on "Award Badges"
end
def award_no_badges
fill_in "usernames", with: "#{user.username}, #{user2.username}"
fill_in "message_markdown", with: "He who controls the spice controls the universe."
click_on "Award Badges"
end
before do
create_list :badge, 5
sign_in admin
@ -46,4 +52,9 @@ RSpec.describe "Admin awards badges", type: :system do
end
end
end
it "does not award badges if no badge is selected" do
expect { award_no_badges }.to change { user.badges.count }.by(0)
expect(page).to have_content("Please choose a badge to award")
end
end