Skip to content

Commit

Permalink
Add email subscribe/unsubscribe flash messages
Browse files Browse the repository at this point in the history
When a user completes the single page email subscribe or unsubscribe
journey they will be redirected to the page they were on with a message
in the account session flash.

Check for these messages in the flash and render the appropriate success
banner. The pages chosen for  phase 1 and phase 2 of the rollout are
either `publication` or `detailed_guide` content types, so only add the
partial to these templates. In the future when we roll out to more pages
we can add the partial to the rest of the content types.
  • Loading branch information
asmith-nhsx committed Nov 11, 2021
1 parent 8e27396 commit 0c88d6f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ContentItemsController < ApplicationController
include GovukPersonalisation::ControllerConcern

rescue_from GdsApi::HTTPForbidden, with: :error_403
rescue_from GdsApi::HTTPNotFound, with: :error_notfound
rescue_from GdsApi::HTTPGone, with: :error_410
Expand Down
2 changes: 2 additions & 0 deletions app/views/content_items/detailed_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
) %>
<% end %>

<%= render 'shared/email_subscribe_unsubscribe_flash' %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<% brexit_child_taxon = @content_item.brexit_child_taxon %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/content_items/publication.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
) %>
<% end %>

<%= render 'shared/email_subscribe_unsubscribe_flash' %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render 'context_and_title' %>
Expand Down
20 changes: 20 additions & 0 deletions app/views/shared/_email_subscribe_unsubscribe_flash.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% if @account_flash.include?("email-subscription-success") %>
<div class="govuk-grid-row govuk-!-margin-top-7">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/success_alert", {
message: sanitize(t("email.subscribe_title")),
description: sanitize(t("email.description_html"))
} %>
</div>
</div>

<% elsif @account_flash.include?("email-unsubscribe-success") %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds govuk-!-margin-top-7">
<%= render "govuk_publishing_components/components/success_alert", {
message: sanitize(t("email.unsubscribe_title")),
description: sanitize(t("email.description_html"))
} %>
</div>
</div>
<% end %>
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ en:
publication_scheme_html: Read about the types of information we routinely publish in our %{link}.
social_media_use_html: Read our policy on %{link}.
welsh_language_scheme_html: Find out about our commitment to %{link}.
email:
subscribe_title: You’ve subscribed to emails about this page
unsubscribe_title: You’ve unsubscribed from emails about this page
description_html: <p class="govuk-body">Go to your GOV.UK account to <a class="govuk-link govuk-notification-banner__link" href="/email/manage">see and manage all your GOV.UK email subscriptions</a>.</p>
fatality_notice:
alt_text: Ministry of Defence crest
field_of_operation: Field of operation
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,22 @@ class ContentItemsControllerTest < ActionController::TestCase
assert response.headers["Vary"].include?("GOVUK-Account-Session-Flash")
end

test "displays the subscription success banner when the 'email-subscription-success' flash is present" do
content_item = content_store_has_schema_example("publication", "publication")

request.headers["GOVUK-Account-Session"] = GovukPersonalisation::Flash.encode_session("session-id", %w[email-subscription-success])
get :show, params: { path: path_for(content_item) }
assert response.body.include?("subscribed to emails about this page")
end

test "displays the unsubscribe success banner when the 'email-unsubscribe-success' flash is present" do
content_item = content_store_has_schema_example("publication", "publication")

request.headers["GOVUK-Account-Session"] = GovukPersonalisation::Flash.encode_session("session-id", %w[email-unsubscribe-success])
get :show, params: { path: path_for(content_item) }
assert response.body.include?("unsubscribed from emails about this page")
end

def path_for(content_item, locale = nil)
base_path = content_item["base_path"].sub(/^\//, "")
base_path.gsub!(/\.#{locale}$/, "") if locale
Expand Down

0 comments on commit 0c88d6f

Please sign in to comment.