Skip to content

Commit

Permalink
✨ activates navbar items (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
karinevieira authored Jun 8, 2024
1 parent eef64e2 commit a97c19f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
6 changes: 3 additions & 3 deletions app/components/application/navbar_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= render Structure::NavbarComponent.new do |nav| %>
<% nav.item(href: root_path, title: t("application.navbar_component.home"), active: true) do |i| %>
<% nav.item(href: root_path, title: t("application.navbar_component.home"), active: activate_home_item?) do |i| %>
<% i.icon do %>
<svg xmlns="http://www.w3.org/2000/svg" class="mr-2 w-5 h-5 group-hover:text-gray-600" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25" /></svg>
<% end %>
Expand All @@ -11,13 +11,13 @@
<% end %>
<% end %>

<% nav.item(href: new_post_path, title: t("application.navbar_component.new")) do |i| %>
<% nav.item(href: new_post_path, title: t("application.navbar_component.new"), active: activate_new_post_item?) do |i| %>
<% i.icon do %>
<svg xmlns="http://www.w3.org/2000/svg" class="mr-2 w-5 h-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6.827 6.175A2.31 2.31 0 015.186 7.23c-.38.054-.757.112-1.134.175C2.999 7.58 2.25 8.507 2.25 9.574V18a2.25 2.25 0 002.25 2.25h15A2.25 2.25 0 0021.75 18V9.574c0-1.067-.75-1.994-1.802-2.169a47.865 47.865 0 00-1.134-.175 2.31 2.31 0 01-1.64-1.055l-.822-1.316a2.192 2.192 0 00-1.736-1.039 48.774 48.774 0 00-5.232 0 2.192 2.192 0 00-1.736 1.039l-.821 1.316z" /><path stroke-linecap="round" stroke-linejoin="round" d="M16.5 12.75a4.5 4.5 0 11-9 0 4.5 4.5 0 019 0zM18.75 10.5h.008v.008h-.008V10.5z" /></svg>
<% end %>
<% end %>

<% nav.item(href: user_path(username: current_user.profile.username), title: t("application.navbar_component.profile")) do |i| %>
<% nav.item(href: user_path(username: current_user.profile.username), title: t("application.navbar_component.profile"), active: activate_profile_item?) do |i| %>
<% i.icon do %>
<svg xmlns="http://www.w3.org/2000/svg" class="mr-2 w-5 h-5 group-hover:text-gray-600" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0A17.933 17.933 0 0112 21.75c-2.676 0-5.216-.584-7.499-1.632z" /></svg>
<% end %>
Expand Down
21 changes: 19 additions & 2 deletions app/components/application/navbar_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@

module Application
class NavbarComponent < ViewComponent::Base
def initialize(current_user:)
def initialize(current_user:, current_controller:)
@current_user = current_user
@current_controller = current_controller
end

private

attr_reader :current_user
attr_reader :current_user, :current_controller

def activate_home_item?
current_controller.is_a?(PostsController) && current_action_is_a?(:index)
end

def activate_new_post_item?
current_controller.is_a?(PostsController) && current_action_is_a?(:new)
end

def activate_profile_item?
current_controller.is_a?(UsersController) || current_controller.is_a?(ProfilesController)
end

def current_action_is_a?(action)
current_controller.action_name == action.to_s
end
end
end
2 changes: 1 addition & 1 deletion app/pages/posts/index_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<%= render Structure::CardComponent.with_collection(posts, user: user) %>
</div>

<%= render Application::NavbarComponent.new(current_user: user) %>
<%= render Application::NavbarComponent.new(current_user: user, current_controller: controller) %>
2 changes: 1 addition & 1 deletion app/pages/posts/new_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
<% end %>
<% end %>

<%= render Application::NavbarComponent.new(current_user: current_user) %>
<%= render Application::NavbarComponent.new(current_user: current_user, current_controller: controller) %>
</div>
2 changes: 1 addition & 1 deletion app/pages/profiles/edit_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
<% end %>
<% end %>

<%= render Application::NavbarComponent.new(current_user: user) %>
<%= render Application::NavbarComponent.new(current_user: user, current_controller: controller) %>
</div>
2 changes: 1 addition & 1 deletion app/pages/users/show_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
</div>

<% if current_user.present? %>
<%= render Application::NavbarComponent.new(current_user: current_user) %>
<%= render Application::NavbarComponent.new(current_user: current_user, current_controller: controller) %>
<% end %>
10 changes: 5 additions & 5 deletions spec/components/application/navbar_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
it "renders the Home item" do
current_user = create(:user)

rendered = render_inline(described_class.new(current_user: current_user))
rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.home")

expect(rendered.to_html).to have_link(item_text, href: root_path)
Expand All @@ -15,7 +15,7 @@
it "renders the Search item" do
current_user = create(:user)

rendered = render_inline(described_class.new(current_user: current_user))
rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.search")

expect(rendered.to_html).to have_link(item_text, href: "#")
Expand All @@ -24,7 +24,7 @@
it "renders the New item" do
current_user = create(:user)

rendered = render_inline(described_class.new(current_user: current_user))
rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.new")

expect(rendered.to_html).to have_link(item_text, href: new_post_path)
Expand All @@ -33,7 +33,7 @@
it "renders the Profile item" do
current_user = create(:user)

rendered = render_inline(described_class.new(current_user: current_user))
rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.profile")

expect(rendered.to_html).to have_link(item_text, href: user_path(username: current_user.profile.username))
Expand All @@ -42,7 +42,7 @@
it "renders the Sign out item" do
current_user = create(:user)

rendered = render_inline(described_class.new(current_user: current_user))
rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.sign_out")

expect(rendered.to_html).to have_link(item_text, href: destroy_user_session_path)
Expand Down

0 comments on commit a97c19f

Please sign in to comment.