Skip to content

Commit

Permalink
Merge pull request #7950 from alphagov/email-signup-override-for-doc-…
Browse files Browse the repository at this point in the history
…collections

UI to add a taxonomy topic email override to document collections
  • Loading branch information
hannako authored Nov 28, 2023
2 parents 142ede2 + 88a6f9c commit 547df11
Show file tree
Hide file tree
Showing 20 changed files with 726 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class Admin::DocumentCollectionEmailSubscriptionsController < Admin::BaseController
include Admin::DocumentCollectionEmailOverrideHelper
before_action :load_document_collection
before_action :authorise_user
layout "design_system"

def edit
@topic_list_select_presenter = TopicListSelectPresenter.new(@collection.taxonomy_topic_email_override)
end

def update
if user_has_selected_taxonomy_topic_emails?
if all_required_params_present?
@collection.update!(taxonomy_topic_email_override: params["selected_taxon_content_id"])
else
build_missing_params_flash
return redirect_to form_with_stored_params
end
else
@collection.update!(taxonomy_topic_email_override: nil)
end
build_flash("notice")
redirect_to edit_admin_document_collection_path(@collection)
rescue ActiveRecord::RecordInvalid
redirect_to edit_admin_document_collection_path(@collection)
end

private

def all_required_params_present?
required_params.select { |key| params[key].present? } == required_params
end

def required_params
%w[selected_taxon_content_id email_override_confirmation]
end

def form_with_stored_params
admin_document_collection_edit_email_subscription_path(@collection, params_to_store_state_of_failed_form_submission)
end

def build_missing_params_flash
missing_params = required_params.select { |required_param| params[required_param].blank? }
missing_params.each { |key| build_flash(key) }
end

def params_to_store_state_of_failed_form_submission
{
"selected_taxon_content_id" => params["selected_taxon_content_id"],
"override_email_subscriptions" => params["override_email_subscriptions"],
}
end

def load_document_collection
@collection = DocumentCollection.find(params[:document_collection_id])
end

def authorise_user
redirect_to edit_admin_document_collection_path(@collection) unless current_user.can_edit_email_overrides?
end

def build_flash(key)
flash[key] = {
"selected_taxon_content_id" => "You must choose a topic",
"email_override_confirmation" => "You must confirm you’re happy with the email notification settings",
"notice" => "You’ve selected the email notification settings. #{confirmation_message}. You will not be able to change these settings after you publish the collection.",
}[key]
end

def confirmation_message
if @collection.taxonomy_topic_email_override.present?
"You’ve chosen ‘Emails about the topic’ and the topic #{taxonomy_topic_email_override_title(@collection)}"
else
"You’ve chosen ‘Emails about the page’"
end
end

def user_has_selected_taxonomy_topic_emails?
params[:override_email_subscriptions] == "true"
end
end
29 changes: 29 additions & 0 deletions app/helpers/admin/document_collection_email_override_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Admin::DocumentCollectionEmailOverrideHelper
def taxonomy_topic_cannot_be_set?(collection)
collection.document.live_edition_id.present?
end

def has_page_level_notifications?(collection)
collection.taxonomy_topic_email_override.nil?
end

def taxonomy_topic_email_override_title(collection)
taxonomy_topic_content_item(collection).fetch("title", "")
end

def taxonomy_topic_email_override_base_path(collection)
taxonomy_topic_content_item(collection).fetch("base_path", "")
end

def taxonomy_topic_content_item(collection)
@taxonomy_topic_content_item ||= Services.publishing_api
.get_content(collection.taxonomy_topic_email_override)
.to_h
rescue GdsApi::HTTPNotFound
{}
end

def emails_about_this_topic_checked?(collection, params)
collection.taxonomy_topic_email_override.present? || (params["override_email_subscriptions"] == "true")
end
end
4 changes: 4 additions & 0 deletions app/helpers/admin/editions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def default_edition_tabs(edition)
if edition.is_a?(DocumentCollection) && !edition.new_record?
tabs["Collection documents"] = admin_document_collection_groups_path(edition)
end

if edition.is_a?(DocumentCollection) && current_user.can_edit_email_overrides?
tabs["Email notifications"] = admin_document_collection_edit_email_subscription_path(edition)
end
end
end

Expand Down
12 changes: 11 additions & 1 deletion app/helpers/admin/tabbed_nav_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,21 @@ def consultation_nav_items(edition, current_path)
end

def document_collection_nav_items(edition, current_path)
{
collection_documents_element = {
label: "Collections",
href: admin_document_collection_groups_path(edition),
current: current_path == admin_document_collection_groups_path(edition),
}
email_notifications_element = {
label: "Email notifications",
href: admin_document_collection_edit_email_subscription_path(edition),
current: current_path == admin_document_collection_edit_email_subscription_path(edition),
}
if current_user.can_edit_email_overrides?
[collection_documents_element, email_notifications_element]
else
[collection_documents_element]
end
end

def document_collection_group_nav_items(group, current_path)
Expand Down
11 changes: 11 additions & 0 deletions app/helpers/errors_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ def errors_for(errors, attribute)
}
.presence
end

def errors_from_flash(flash)
return nil if flash.blank?

flash.map do |array|
{
href: "##{array.first}",
text: array.last,
}
end
end
end
84 changes: 84 additions & 0 deletions app/presenters/topic_list_select_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
class TopicListSelectPresenter
# This presenter is used to select a taxonomy topic email override for some document collections
#  We are only going to show the branches which the dept has told us they will need to tag to.
TAGGABLE_BRANCHES = [
"/business-and-industry",
"/business-tax",
"/childcare-parenting",
"/defence",
"/defence-and-armed-forces",
"/education",
"/employment",
"/environment",
"/government",
"/housing-and-local-government",
"/money",
"/regional-and-local-government",
"/society-and-culture",
"/transport",
"/welfare",
"/work",
].freeze

def initialize(taxonomy_topic_email_override = nil)
@taxonomy_topic_email_override = taxonomy_topic_email_override
end

attr_reader :taxonomy_topic_email_override

def grouped_options(selected_taxon_content_id = nil)
branches_sorted_by_level_one_taxon_name.map do |level_one_taxon|
[level_one_taxon.name, sorted_transformed_descendants(level_one_taxon, selected_taxon_content_id)]
end
end

private

def branches_sorted_by_level_one_taxon_name
topic_taxonomy.visible_branches
.select { |branch| TAGGABLE_BRANCHES.include?(branch.base_path) }
.sort_by { |branch| branch.name.downcase }
end

def sorted_transformed_descendants(level_one_taxon, selected_taxon_content_id)
transform_descendants(level_one_taxon, selected_taxon_content_id)
.sort_by { |s| s[:text].downcase }
end

def transform_descendants(level_one_taxon, selected_taxon_content_id)
sort_descendants(level_one_taxon).map do |child|
transform_taxon(child, selected_taxon_content_id)
end
end

def sort_descendants(level_one_taxon)
sorted_descendants = level_one_taxon.descendants.sort_by { |descendant| descendant.name.downcase }
[level_one_taxon, sorted_descendants].flatten
end

def taxon_with_ancestors(taxon)
ancestors_names = taxon.ancestors.map(&:name)
ancestor_string = ancestors_names.join(" > ")

"#{ancestor_string} > #{taxon.name} " if ancestors_names.any?
end

def transform_taxon(taxon, selected_taxon_content_id = nil)
formatted_taxon_name =
taxon.ancestors.present? ? taxon_with_ancestors(taxon) : taxon.name.to_s
{
text: formatted_taxon_name,
value: taxon.content_id,
selected: selected?(taxon.content_id, selected_taxon_content_id),
}
end

def selected?(content_id, selected_taxon_content_id)
previously_selected = selected_taxon_content_id || taxonomy_topic_email_override
previously_selected == content_id
end

def topic_taxonomy
@topic_taxonomy ||= Taxonomy::TopicTaxonomy.new
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<p class="govuk-body-m">
You cannot change the email notifications for this document collection.
If you have any questions about this, contact the managing editor at HMRC.
</p>
<p class="govuk-body-m"> Users who sign up to notifications on this document collection get an email alert when:</p>
<% if has_page_level_notifications?(@collection) %>
<%= render "govuk_publishing_components/components/list", {
visible_counters: true,
items: [
"there’s a major change to any content that’s listed on the document collection (the content items in the ‘Collection documents’ tab)",
"there’s a major change to the document collection itself",
"the page is unpublished",
],
} %>
<% else %>
<%= render "govuk_publishing_components/components/list", {
visible_counters: true,
items: [
"there’s a major change to any content tagged to the topic #{link_to taxonomy_topic_email_override_title(@collection), taxonomy_topic_email_override_base_path(@collection)}",
"the page is unpublished",
],
} %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%= form_for [:admin, @collection], url: admin_document_collection_update_email_subscription_path(@collection), method: "put" do |form| %>
<%= render "govuk_publishing_components/components/radio", {
name: "override_email_subscriptions",
id: "email_override_email_subscription_choice",
items: [
{
value: false,
text: "Emails about this page",
hint_text: "Users will get an email when the document collection or any of the content listed on it is updated.",
checked: has_page_level_notifications?(@collection),
bold: true,
},
{
value: true,
text: "Emails about the topic",
bold: true,
hint_text: "Users will get an email when any content related to the topic is updated. You choose the topic from the topic taxonomy.",
checked: emails_about_this_topic_checked?(@collection, params),
conditional: render("taxonomy_choice"),
},
],
} %>

<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Save",
data_attributes: {
module: "gem-track-click",
"track-category": "form-button",
"track-action": "update-email-subscriptions-button",
"track-label": "Save",
},
} %>
<%= link_to("Cancel", admin_document_collection_path(@collection), class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div>
<h4 class="govuk-heading-s govuk-!-margin-bottom-2">Choose the topic</h4>
<%= render "components/select-with-search", {
label: "Choose a topic from the topic taxonomy. You can only choose one.",
error_message: flash["selected_taxon_content_id"],
error_id: flash["selected_taxon_content_id"],
id: "selected_taxon_content_id",
include_blank: true,
grouped_options: @topic_list_select_presenter.grouped_options(params["selected_taxon_content_id"]),
} %>
<hr class="govuk-section-break--m">
<%= render "govuk_publishing_components/components/checkboxes", {
id: "email_override_confirmation",
name: "email_override_confirmation",
heading_size: "s",
heading: "You cannot change the email notification settings after the collection is published.",
no_hint_text: true,
error: flash["email_override_confirmation"],
items: [
{
label: "Select this box to confirm you're happy with what you've selected.",
value: true,
},
],
} %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<% content_for :page_title, "Email notification settings" %>
<% content_for :title_margin_bottom, 8 %>
<% content_for :page_class, "document-collection-email-groups edit" %>
<% errors = errors_from_flash(flash) %>

<% if errors.present? %>
<%= render "govuk_publishing_components/components/error_summary", {
id: "error-summary",
title: "There are some problems",
items: errors,
} %>
<% end %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "components/secondary_navigation", {
aria_label: "Document navigation tabs",
items: secondary_navigation_tabs_items(@collection, request.path),
} %>

<h2 class="govuk-heading-l">Email notifications</h2>

<% if taxonomy_topic_cannot_be_set?(@collection) %>
<%= render partial: "email_override_summary_page" %>
<% else %>
<p class="govuk-body">Choose the type of email updates users will get if they sign up for notifications.</p>
<%= render partial: "form" %>
<% end %>
</div>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })
put :order, on: :collection
end
end
get "email-subscriptions" => "document_collection_email_subscriptions#edit", as: :edit_email_subscription
put "email-subscriptions" => "document_collection_email_subscriptions#update", as: :update_email_subscription
post "whitehall-member" => "document_collection_group_memberships#create_whitehall_member", as: :new_whitehall_member
end

Expand Down
Loading

0 comments on commit 547df11

Please sign in to comment.