Refactor timelines to use a Turbo Stream-backed pagination

This commit is contained in:
Andreas Nedbal 2022-09-08 22:41:32 +02:00 committed by Andreas Nedbal
parent 5d88dfcb9c
commit b595910564
5 changed files with 14 additions and 16 deletions

View File

@ -27,7 +27,7 @@ class TimelineController < ApplicationController
respond_to do |format| respond_to do |format|
format.html { render "timeline/timeline" } format.html { render "timeline/timeline" }
format.js { render "timeline/timeline", layout: false } format.turbo_stream { render "timeline/timeline", layout: false, status: :see_other }
end end
end end
end end

View File

@ -2,11 +2,9 @@
- @timeline.each do |answer| - @timeline.each do |answer|
= render "answerbox", a: answer = render "answerbox", a: answer
= render "shared/cursored_pagination_dummy", more_data_available: @more_data_available, last_id: @timeline_last_id
- if @more_data_available - if @more_data_available
.d-flex.justify-content-center.justify-content-sm-start .d-flex.justify-content-center.justify-content-sm-start#paginator
%button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @timeline_last_id } } = button_to "#{request.path}?last_id=#{@timeline_last_id}", class: "btn btn-light" do
= t("voc.load") = t("voc.load")
- provide(:title, @title || APP_CONFIG["site_name"]) - provide(:title, @title || APP_CONFIG["site_name"])

View File

@ -1,8 +0,0 @@
$('#timeline').append('<% @timeline.each do |answer|
%><%= j render 'answerbox', a: answer
%><% end %>');
<% if @more_data_available %>
$('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id %>');
<% else %>
$('#pagination, #load-more-btn').remove();
<% end %>

View File

@ -0,0 +1,8 @@
= turbo_stream.append "timeline" do
- @timeline.each do |answer|
= render "answerbox", a: answer
= turbo_stream.update "paginator" do
- if @more_data_available
= button_to "#{request.path}?last_id=#{@timeline_last_id}", class: "btn btn-light" do
= t("voc.load")

View File

@ -39,7 +39,7 @@ Rails.application.routes.draw do
end end
authenticate :user do authenticate :user do
root to: "timeline#index", as: :timeline root to: "timeline#index", via: [:get, :post], as: :timeline
end end
get "/about", to: "about#about" get "/about", to: "about#about"
@ -138,8 +138,8 @@ Rails.application.routes.draw do
end end
get "/discover", to: "discover#index", as: :discover get "/discover", to: "discover#index", as: :discover
get "/public", to: "timeline#public", as: :public_timeline if APP_CONFIG.dig(:features, :public, :enabled) match "/public", to: "timeline#public", via: [:get, :post], as: :public_timeline if APP_CONFIG.dig(:features, :public, :enabled)
get "/list/:list_name", to: "timeline#list", as: :list_timeline match "/list/:list_name", to: "timeline#list", via: [:get, :post], as: :list_timeline
match "/notifications(/:type)", to: "notifications#index", via: [:get, :post], as: :notifications, defaults: { type: "new" } match "/notifications(/:type)", to: "notifications#index", via: [:get, :post], as: :notifications, defaults: { type: "new" }