Skip to content

Commit

Permalink
✨ Add link to user's profile
Browse files Browse the repository at this point in the history
  • Loading branch information
karinevieira committed May 4, 2024
1 parent eaf573a commit ff66dcf
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/components/application/navbar_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<% end %>
<% end %>

<% nav.item(href: "#", title: t("application.navbar_component.profile")) do |i| %>
<% nav.item(href: user_path(username: current_user.profile.username), title: t("application.navbar_component.profile")) 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
7 changes: 7 additions & 0 deletions app/components/application/navbar_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@

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

private

attr_reader :current_user
end
end
2 changes: 1 addition & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index

def new
respond_to do |format|
format.html { render Posts::NewPage.new(post: Post.new) }
format.html { render Posts::NewPage.new(current_user: current_user, post: Post.new) }
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class UsersController < ApplicationController
def show
result = Users::FindByUsername.result(username: params[:username])

render Users::ShowPage.new(user: result.user)
render Users::ShowPage.new(current_user: current_user, user: result.user)
end
end
2 changes: 0 additions & 2 deletions app/pages/posts/edit_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@
<% end %>
<% end %>
<% end %>

<%= render Application::NavbarComponent.new %>
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 %>
<%= render Application::NavbarComponent.new(current_user: user) %>
6 changes: 4 additions & 2 deletions app/pages/posts/index_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

module Posts
class IndexPage < ApplicationPage
attr_reader :posts, :user

def initialize(posts:, user:)
@posts = posts
@user = user
end

private

attr_reader :posts, :user
end
end
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 @@ -16,5 +16,5 @@
<% end %>
<% end %>

<%= render Application::NavbarComponent.new %>
<%= render Application::NavbarComponent.new(current_user: current_user) %>
</div>
9 changes: 6 additions & 3 deletions app/pages/posts/new_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

module Posts
class NewPage < ApplicationPage
attr_reader :post

def initialize(post:)
def initialize(current_user:, post:)
@current_user = current_user
@post = post
end

private

attr_reader :current_user, :post
end
end
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 @@ -22,5 +22,5 @@
<% end %>
<% end %>

<%= render Application::NavbarComponent.new %>
<%= render Application::NavbarComponent.new(current_user: user) %>
</div>
6 changes: 4 additions & 2 deletions app/pages/profiles/edit_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

module Profiles
class EditPage < ApplicationPage
attr_reader :user

def initialize(user:)
@user = user
end

private

attr_reader :user
end
end
5 changes: 4 additions & 1 deletion app/pages/users/show_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
</div>
</div>

<%= render Application::NavbarComponent.new %>

<% if current_user.present? %>
<%= render Application::NavbarComponent.new(current_user: current_user) %>
<% end %>
9 changes: 6 additions & 3 deletions app/pages/users/show_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

module Users
class ShowPage < ApplicationPage
attr_reader :user

def initialize(user:)
def initialize(current_user:, user:)
@current_user = current_user
@user = user
end

private

attr_reader :current_user, :user
end
end
22 changes: 16 additions & 6 deletions spec/components/application/navbar_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,45 @@

RSpec.describe Application::NavbarComponent, type: :component do
it "renders the Home item" do
rendered = render_inline(described_class.new)
current_user = create(:user)

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

expect(rendered.to_html).to have_link(item_text, href: root_path)
end

it "renders the Search item" do
rendered = render_inline(described_class.new)
current_user = create(:user)

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

expect(rendered.to_html).to have_link(item_text, href: "#")
end

it "renders the New item" do
rendered = render_inline(described_class.new)
current_user = create(:user)

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

expect(rendered.to_html).to have_link(item_text, href: new_post_path)
end

it "renders the Profile item" do
rendered = render_inline(described_class.new)
current_user = create(:user)

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

expect(rendered.to_html).to have_link(item_text, href: "#")
expect(rendered.to_html).to have_link(item_text, href: user_path(username: current_user.profile.username))
end

it "renders the Sign out item" do
rendered = render_inline(described_class.new)
current_user = create(:user)

rendered = render_inline(described_class.new(current_user: current_user))
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
4 changes: 2 additions & 2 deletions spec/pages/posts/new_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require "rails_helper"

RSpec.describe Posts::NewPage, type: :page do
subject(:rendered) { render_inline(described_class.new(post: Post.new)) }

it "renders without problems" do
current_user = create(:user)
rendered = render_inline(described_class.new(current_user: current_user, post: Post.new))
expect(rendered.to_html).to be_present
end
end

0 comments on commit ff66dcf

Please sign in to comment.