Skip to content

Commit 20b2b1a

Browse files
authoredJan 14, 2025
infinite scroll for topic talks (#565)
1 parent e594855 commit 20b2b1a

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed
 

‎app/controllers/topics_controller.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ def index
1212

1313
def show
1414
@topic = Topic.find_by!(slug: params[:slug])
15-
@pagy, @talks = pagy(
15+
@pagy, @talks = pagy_countless(
1616
@topic.talks.includes(:speakers, event: :organisation, child_talks: :speakers).order(date: :desc),
17-
limit: 12,
17+
gearbox_extra: true,
18+
gearbox_limit: [12, 24, 48, 96],
19+
overflow: :empty_page,
1820
page: params[:page]&.to_i || 1
1921
)
2022
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Controller } from '@hotwired/stimulus'
2+
import { useIntersection } from 'stimulus-use'
3+
4+
// Connects to data-controller="auto-click"
5+
export default class extends Controller {
6+
static values = {
7+
rootMargin: { type: String, default: '400px 0px 0px 0px' }
8+
}
9+
10+
initialize () {
11+
useIntersection(this, { rootMargin: this.rootMarginValue })
12+
}
13+
14+
appear () {
15+
this.element.click()
16+
}
17+
}

‎app/javascript/controllers/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import { application } from "./application"
66

7+
import AutoClickController from "./auto-click_controller"
8+
application.register("auto-click", AutoClickController)
9+
710
import AutoSubmitController from "./auto_submit_controller"
811
application.register("auto-submit", AutoSubmitController)
912

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<% manual = (pagy.page % 3 == 0) %>
2+
<% controller = manual ? "" : "auto-click" %>
3+
4+
<div class="<%= class_names("flex justify-center", invisible: !manual) %>" id="talks-cursor">
5+
<% if pagy.next %>
6+
<%= link_to "Load more", topic_path(topic, page: pagy.next),
7+
class: "btn btn-primary",
8+
data: {turbo_stream: true, controller: controller} %>
9+
<% end %>
10+
</div>

‎app/views/topics/show.html.erb

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,5 @@
2222
back_to_title: @topic.name
2323
} %>
2424
</div>
25-
<div class="flex mt-4 w-full">
26-
<%== pagy_nav(@pagy) if @pagy.pages > 1 %>
27-
</div>
25+
<%= render "topics/talks_cursor", pagy: @pagy, topic: @topic %>
2826
</div>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%= turbo_stream.append "topic-talks" do %>
2+
<%= render partial: "talks/card",
3+
collection: @talks,
4+
as: :talk,
5+
locals: {
6+
favoritable: true,
7+
user_favorite_talks_ids: @user_favorite_talks_ids,
8+
watched_talks_ids: user_watched_talks_ids,
9+
back_to: back_to_from_request,
10+
back_to_title: @topic.name
11+
} %>
12+
<% end %>
13+
14+
<%= turbo_stream.replace "talks-cursor" do %>
15+
<%= render "topics/talks_cursor", pagy: @pagy, topic: @topic %>
16+
<% end %>

‎config/initializers/pagy.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "pagy/extras/meilisearch"
22
require "pagy/extras/overflow"
33
require "pagy/extras/gearbox"
4+
require "pagy/extras/countless"
45

56
Pagy::DEFAULT[:overflow] = :last_page
67
Pagy::DEFAULT[:gearbox_extra] = false

0 commit comments

Comments
 (0)