Browse Source

Add navigation items helper

pull/526/head
Paul Robert Lloyd 3 years ago committed by baarkerlounger
parent
commit
5362cf0687
  1. 6
      app/components/primary_navigation_component.html.erb
  2. 4
      app/components/primary_navigation_component.rb
  3. 41
      app/helpers/navigation_items_helper.rb
  4. 17
      app/views/layouts/application.html.erb
  5. 6
      spec/components/primary_navigation_component_spec.rb

6
app/components/primary_navigation_component.html.erb

@ -2,13 +2,13 @@
<div class="govuk-width-container">
<ul class="app-primary-navigation__list">
<% items.each do |item| %>
<% if highlighted_tab?(item, request.fullpath) %>
<% if item.current %>
<li class="app-primary-navigation__item app-primary-navigation__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" } %>
</li>
<% else %>
<li class="app-primary-navigation__item">
<%= govuk_link_to item[:name], item[:url], class: "app-primary-navigation__link" %>
<%= govuk_link_to item[:text], item[:href], class: "app-primary-navigation__link" %>
</li>
<% end %>
<% end %>

4
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

41
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

17
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 %>
<div class="govuk-width-container">

6
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

Loading…
Cancel
Save