Skip to content

Commit 8aec642

Browse files
committed
Refactor topics nav to component
1 parent 5b28e2e commit 8aec642

File tree

6 files changed

+103
-47
lines changed

6 files changed

+103
-47
lines changed

app/content/layouts/article.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<%= yield %>
3838
</div>
3939
<% if @page.topics.any? %>
40-
<section class="section-content container">
40+
<section id="article-topics" class="section-content container">
4141
<%= render Pages::Topics.new(topics: @page.topics.pluck(:slug)) %>
4242
</section>
4343
<% end %>

app/views/topics/index.html.erb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44

55
<div class="column-content container py-gap mb-3xl">
66
<nav class="sidebar" data-turbo-permanent>
7-
<%= turbo_frame_tag :topics_sidebar do %>
8-
<%= render Topics::Select.new(topics, class: "lg:hidden") %>
9-
<% topics.each do |topic| %>
10-
<%= tag.div id: dom_id(topic), class: "text-small mb-2 hidden lg:block" do %>
11-
<%= link_to topic.name, topic_path(topic), data: {turbo_frame: :topics_mainbar} %>
12-
<span class="text-faint">(<%= topic.pages_count %>)</span>
13-
<% end %>
14-
<% end %>
7+
<%= turbo_frame_tag "topics-sidebar" do %>
8+
<%= render Topics::Nav.new(topics, class: "lg:hidden") %>
159
<% end %>
1610
</nav>
1711
<div class="mainbar w-full">
18-
<%= turbo_frame_tag :topics_mainbar, data: {turbo_action: "advance"} do %>
12+
<%= turbo_frame_tag "topics-mainbar", data: {turbo_action: "advance"} do %>
1913
<div>
2014
<h3 class="mb-8 lg:hidden">Howdy!</h3>
2115
<div>

app/views/topics/nav.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module Topics
2+
class Nav < ApplicationComponent
3+
include Phlex::Rails::Helpers::CurrentPage
4+
include Phlex::Rails::Helpers::DOMID
5+
include PhlexConcerns::FlexBlock
6+
7+
attr_reader :topics, :attributes
8+
9+
def initialize(topics = [], **attributes)
10+
@topics = topics
11+
@attributes = attributes
12+
end
13+
14+
def view_template
15+
topics_select
16+
topics_list
17+
end
18+
19+
def topics_select
20+
flex_block(class: "lg:hidden") do
21+
label(for: "topic", class: "block text-sm font-medium text-gray-700") do
22+
"Select a topic"
23+
end
24+
select(
25+
name: "topic",
26+
data: {
27+
:controller => "select-nav",
28+
:action => "select-nav#visit",
29+
"select-nav-turbo-frame-value" => "topics-mainbar"
30+
}
31+
) do
32+
option(value: "") { "Start here" }
33+
topics.each do |topic|
34+
option(
35+
value: topic_path(topic),
36+
selected: current_page?(topic_path(topic))
37+
) do
38+
topic.name
39+
end
40+
end
41+
end
42+
end
43+
end
44+
45+
def topics_list
46+
topics.each do |topic|
47+
div id: dom_id(topic), class: "text-small mb-2 hidden lg:block" do
48+
a(
49+
href: topic_path(topic),
50+
data: {
51+
turbo_frame: "topics-mainbar"
52+
}
53+
) do
54+
topic.name
55+
end
56+
57+
whitespace
58+
span(class: "text-faint") do
59+
plain "(#{topic.pages_count})"
60+
end
61+
end
62+
end
63+
end
64+
end
65+
end

app/views/topics/select.rb

Lines changed: 0 additions & 34 deletions
This file was deleted.

app/views/topics/show.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
<div class="column-content container py-gap mb-3xl">
77
<nav class="sidebar" data-turbo-permanent>
8-
<%= turbo_frame_tag :topics_sidebar, src: topics_path %>
8+
<%= turbo_frame_tag "topics-sidebar", src: topics_path %>
99
</nav>
1010
<div class="mainbar w-full">
11-
<%= turbo_frame_tag :topics_mainbar, data: {turbo_action: "advance"} do %>
11+
<%= turbo_frame_tag "topics-mainbar", data: {turbo_action: "advance"} do %>
1212
<% if topic.pages.blank? %>
1313
<div>
1414
<h2>No articles have been added for this topic yet!</h2>
@@ -17,7 +17,7 @@
1717
</div>
1818
</div>
1919
<% else %>
20-
<h3 class="mb-8 hidden lg:block">Topic: <%= topic.name %></h3>
20+
<h3 class="mb-8 hidden lg:block"><%= topic.name %></h3>
2121
<ul>
2222
<% topic.pages.as_published_articles.each do |page| %>
2323
<%= tag.li id: dom_id(page) do %>

spec/system/articles_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,35 @@
66

77
expect(page).to have_content("Articles")
88
end
9+
10+
it "displays a single article" do
11+
Page.upsert_from_sitepress!(limit: 1)
12+
13+
article = Page.first
14+
article.topics << FactoryBot.create_list(:topic, 2, :approved)
15+
article.save!
16+
17+
visit "/articles"
18+
19+
click_on article.title
20+
21+
within("header.page-header") do
22+
expect(page).to have_content(article.title)
23+
end
24+
25+
expect(article.topics.count).to eq(2)
26+
27+
article.topics.each do |topic|
28+
expect(page).to have_link("##{topic.slug}", href: topic_path(topic))
29+
end
30+
31+
first_topic = article.topics.first
32+
click_link "##{first_topic.slug}"
33+
34+
within("header.page-header") do
35+
expect(page).to have_content("Topics")
36+
end
37+
expect(page).to have_content(first_topic.name)
38+
expect(page).to have_content(article.title)
39+
end
940
end

0 commit comments

Comments
 (0)