Skip to content

Commit

Permalink
Allow users to hide the user research recruitment banner
Browse files Browse the repository at this point in the history
Trello: https://trello.com/c/yGMU3sR2

We set a session cookie to hide the banner until the user restarts their
browser.
  • Loading branch information
floehopper committed Aug 10, 2023
1 parent 27cac47 commit 1ec7263
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/controllers/root_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ def index
def signin_required
@application = ::Doorkeeper::Application.find_by(id: session.delete(:signin_missing_for_application))
end

private

def show_user_research_recruitment_banner?
!cookies[:dismiss_user_research_recruitment_banner]
end
helper_method :show_user_research_recruitment_banner?
end
11 changes: 11 additions & 0 deletions app/controllers/user_research_recruitment_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UserResearchRecruitmentController < ApplicationController
before_action :authenticate_user!
skip_after_action :verify_authorized

def update
if params[:choice] == "dismiss-banner"
cookies[:dismiss_user_research_recruitment_banner] = true
redirect_to root_path
end
end
end
6 changes: 4 additions & 2 deletions app/views/root/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
<% content_for :title, "Your applications" %>

<% if show_user_research_recruitment_banner? %>
<% content_for :user_research_recruitment_banner do %>
<section class="user-research-recruitment-banner">
<div class="govuk-width-container">
<hr class="user-research-recruitment-banner__divider govuk-section-break govuk-section-break--l govuk-!-margin-top-0">
<h1 class="user-research-recruitment-banner__title govuk-heading-xl">Help us improve GOV.UK Publishing</h1>
<p class="user-research-recruitment-banner__intro govuk-body">We're holding research sessions to make Publishing work better.</p>
<%= form_tag "#" do %>
<%= form_tag user_research_recruitment_update_path, method: :put do %>
<div class="user-research-recruitment-banner__buttons govuk-button-group">
<button class="govuk-!-font-size-24 govuk-!-font-weight-bold govuk-button govuk-button--start govuk-button--inverse" type="submit">
<span>Find out more</span>
<svg class="govuk-button__start-icon govuk-!-display-none-print" xmlns="http://www.w3.org/2000/svg" width="17.5" height="19" viewBox="0 0 33 40" focusable="false" aria-hidden="true">
<path fill="currentColor" d="M0 0h13l20 20-20 20H0l20-20z"></path>
</svg>
</button>
<button class="govuk-button govuk-button--inverse" type="submit">
<button class="govuk-button govuk-button--inverse" type="submit" name="choice" value="dismiss-banner">
Hide this
</button>
</div>
<% end %>
</div>
</section>
<% end %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@
get "/signin-required" => "root#signin_required"

root to: "root#index"

put "/user-research-recruitment/update" => "user_research_recruitment#update"
end
35 changes: 35 additions & 0 deletions test/controllers/user_research_recruitment_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "test_helper"

class UserResearchRecruitmentControllerTest < ActionController::TestCase
attr_reader :choice

context "#update" do
context "when user clicks the button to dismiss the banner" do
setup do
@choice = "dismiss-banner"
end

should "require signed in users" do
put :update, params: { choice: }

assert_redirected_to new_user_session_path
end

should "set session cookie" do
sign_in create(:user)

put :update, params: { choice: }

assert cookies[:dismiss_user_research_recruitment_banner]
end

should "redirect to root path" do
sign_in create(:user)

put :update, params: { choice: }

assert_redirected_to root_path
end
end
end
end
26 changes: 26 additions & 0 deletions test/integration/user_research_recruitment_banner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ class UserResearchRecruitmentBannerTest < ActionDispatch::IntegrationTest
assert_not has_content?(user_research_recruitment_banner_title)
end

should "hide the banner until the next session" do
user = create(:user, name: "user-name", email: "user@example.com")

using_session("Session 1") do
visit new_user_session_path
signin_with(user)

assert has_content?(user_research_recruitment_banner_title)

within ".user-research-recruitment-banner" do
click_on "Hide this"
end

visit root_path

assert_not has_content?(user_research_recruitment_banner_title)
end

using_session("Session 2") do
visit new_user_session_path
signin_with(user)

assert has_content?(user_research_recruitment_banner_title)
end
end

private

def user_research_recruitment_banner_title
Expand Down

0 comments on commit 1ec7263

Please sign in to comment.