diff --git a/app/helpers/browse_helper.rb b/app/helpers/browse_helper.rb index a577ec9f7..1d3fa2ec2 100644 --- a/app/helpers/browse_helper.rb +++ b/app/helpers/browse_helper.rb @@ -1,21 +1,10 @@ module BrowseHelper - def display_popular_links_for_slug?(slug) - popular_links_data(slug).present? - end - - def popular_links_data(slug) - if I18n.exists?("browse.popular_links.#{slug}", :en) - I18n.t("browse.popular_links.#{slug}") - end - end - - def popular_links_for_slug(slug) - links = popular_links_data(slug) - count = links.length - links.map.with_index(1) do |link, index| + def with_tracking_data(popular_content) + count = popular_content.length + popular_content.map.with_index(1) do |link, index| { text: link[:title], - href: link[:url], + href: link[:link], data_attributes: { module: "ga4-link-tracker", ga4_track_links_only: "", diff --git a/app/models/mainstream_browse_page.rb b/app/models/mainstream_browse_page.rb index cb4ebb510..72c76a50d 100644 --- a/app/models/mainstream_browse_page.rb +++ b/app/models/mainstream_browse_page.rb @@ -45,10 +45,20 @@ def second_level_pages_curated? details["second_level_ordering"] == "curated" end + def top_level_browse_page? + top_level_browse_pages.any? { |page| page.base_path == base_path } + end + def lists @lists ||= ListSet.new("section", @content_item.content_id, details["groups"]) end + def popular_list + return unless top_level_browse_page? + + @popular_list ||= PopularListSet.fetch(@content_item) + end + def slug base_path.sub(%r{\A/browse/}, "") end diff --git a/app/models/popular_list_set.rb b/app/models/popular_list_set.rb new file mode 100644 index 000000000..66cd5feb1 --- /dev/null +++ b/app/models/popular_list_set.rb @@ -0,0 +1,40 @@ +class PopularListSet + def initialize(content_item) + @content_item = content_item + end + + def self.fetch(content_item) + new(content_item).formatted_results + end + + def formatted_results + if search_response["results"].present? + search_response["results"].map do |result| + { + title: result["title"], + link: result["link"], + } + end + end + end + +private + + attr_reader :content_item + + def search_response + params = { + filter_any_mainstream_browse_page_content_ids: second_level_browse_content_ids, + count: 3, + order: "-popularity", + fields: SearchApiFields::POPULAR_BROWSE_SEARCH_FIELDS, + } + Services.cached_search(params) + end + + def second_level_browse_content_ids + content_item + .linked_items("second_level_browse_pages") + .map { |content_item| content_item.content_item_data["content_id"] } + end +end diff --git a/app/services/search_api_fields.rb b/app/services/search_api_fields.rb index 4da5d9ab3..2e0f5e02c 100644 --- a/app/services/search_api_fields.rb +++ b/app/services/search_api_fields.rb @@ -35,4 +35,10 @@ module SearchApiFields content_id organisations document_collections].freeze + + POPULAR_BROWSE_SEARCH_FIELDS = %w[ title + link + public_timestamp + display_type + content_store_document_type].freeze end diff --git a/app/views/browse/show.html.erb b/app/views/browse/show.html.erb index 2d00fa7f4..ca1fa5b82 100644 --- a/app/views/browse/show.html.erb +++ b/app/views/browse/show.html.erb @@ -34,7 +34,7 @@ } %> <% end %> -<% if display_popular_links_for_slug?(page.slug) %> +<% if page.popular_list %>