<% items.each do |item| %>
- <% if highlighted_tab?(item, request.fullpath) %>
+ <% if item.current %>
-
- <%= govuk_link_to item[:name], item[:url], class: "app-primary-navigation__link", aria: { current: "page" } %>
+ <%= govuk_link_to item[:text], item[:href], class: "app-primary-navigation__link", aria: { current: "page" } %>
<% else %>
-
- <%= govuk_link_to item[:name], item[:url], class: "app-primary-navigation__link" %>
+ <%= govuk_link_to item[:text], item[:href], class: "app-primary-navigation__link" %>
<% end %>
<% end %>
diff --git a/app/components/primary_navigation_component.rb b/app/components/primary_navigation_component.rb
index 41ed73535..33f5a9529 100644
--- a/app/components/primary_navigation_component.rb
+++ b/app/components/primary_navigation_component.rb
@@ -5,8 +5,4 @@ class PrimaryNavigationComponent < ViewComponent::Base
@items = items
super
end
-
- def highlighted_tab?(item, path)
- item.fetch(:current, false) || item.fetch(:comparable_urls).any? { |url| path.include?(url) }
- end
end
diff --git a/app/helpers/navigation_items_helper.rb b/app/helpers/navigation_items_helper.rb
new file mode 100644
index 000000000..afc2bc0d3
--- /dev/null
+++ b/app/helpers/navigation_items_helper.rb
@@ -0,0 +1,41 @@
+module NavigationItemsHelper
+ NavigationItem = Struct.new(:text, :href, :current, :classes)
+
+ def primary_items(current_user)
+ if current_user.support?
+ [
+ NavigationItem.new("Organisations", organisations_path, organisation_current?),
+ NavigationItem.new("Users", users_path, users_current?),
+ NavigationItem.new("Logs", case_logs_path, logs_current?),
+ ]
+ else
+ [
+ NavigationItem.new("Logs", case_logs_path, logs_current?),
+ NavigationItem.new("Users", users_organisation_path(current_user.organisation), users_current?),
+ NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", organisation_current?),
+ ]
+ end
+ end
+
+private
+
+ def current?(current_controller, controllers)
+ current_controller.controller_name.in?(Array.wrap(controllers))
+ end
+
+ def current_action?(current_controller, action)
+ current_controller.action_name == action
+ end
+
+ def logs_current?
+ current?(controller, %w[case_logs form])
+ end
+
+ def users_current?
+ current?(controller, %w[users]) || current_action?(controller, "users")
+ end
+
+ def organisation_current?
+ current?(controller, %w[organisations]) && !current_action?(controller, "users")
+ end
+end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 6866095da..cefeea24b 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -65,20 +65,9 @@
) %>
<% if !current_user.nil? %>
- <% if current_user.support? %>
- <% items = [
- { name: "Organisations", url: "/organisations", comparable_urls: ["/details", "/organisations"] },
- { name: "Users", url: "/users", comparable_urls: ["/users", "/account"] },
- { name: "Logs", url: case_logs_path, comparable_urls: ["/logs"] },
- ] %>
- <% else %>
- <% items = [
- { name: "Logs", url: case_logs_path, comparable_urls: ["/logs"] },
- { name: "Users", url: users_organisation_path(current_user.organisation), comparable_urls: ["/users", "/account"] },
- { name: "About your organisation", url: "/organisations/#{current_user.organisation.id}", comparable_urls: ["/organisations"] },
- ] %>
- <% end %>
- <%= render PrimaryNavigationComponent.new(items:) %>
+ <%= render PrimaryNavigationComponent.new(
+ items: primary_items(current_user)
+ ) %>
<% end %>
diff --git a/spec/components/primary_navigation_component_spec.rb b/spec/components/primary_navigation_component_spec.rb
index ef2db67de..999dbfaf5 100644
--- a/spec/components/primary_navigation_component_spec.rb
+++ b/spec/components/primary_navigation_component_spec.rb
@@ -2,9 +2,9 @@ require "rails_helper"
RSpec.describe PrimaryNavigationComponent, type: :component do
let(:items) do
- [{ name: "Organisations", url: "/organisations", current: true, comparable_urls: ["/organisations", "/something-else"] },
- { name: "Users", url: "/users", comparable_urls: ["/users"] },
- { name: "Logs ", url: "/logs", comparable_urls: ["/logs"] }]
+ [{ text: "Organisations", href: "/organisations", current: true },
+ { text: "Users", href: "/users", current: false },
+ { text: "Logs ", href: "/logs", current: false }]
end
context "when the item is 'current' in nav tabs" do