From b59591056495747c3d431ba339f7d2022aab529a Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 8 Sep 2022 22:41:32 +0200 Subject: [PATCH] Refactor timelines to use a Turbo Stream-backed pagination --- app/controllers/timeline_controller.rb | 2 +- app/views/timeline/timeline.haml | 6 ++---- app/views/timeline/timeline.js.erb | 8 -------- app/views/timeline/timeline.turbo_stream.haml | 8 ++++++++ config/routes.rb | 6 +++--- 5 files changed, 14 insertions(+), 16 deletions(-) delete mode 100644 app/views/timeline/timeline.js.erb create mode 100644 app/views/timeline/timeline.turbo_stream.haml diff --git a/app/controllers/timeline_controller.rb b/app/controllers/timeline_controller.rb index 6d3bcecc..a7542134 100644 --- a/app/controllers/timeline_controller.rb +++ b/app/controllers/timeline_controller.rb @@ -27,7 +27,7 @@ class TimelineController < ApplicationController respond_to do |format| 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 diff --git a/app/views/timeline/timeline.haml b/app/views/timeline/timeline.haml index b8330cd1..e7eb7916 100644 --- a/app/views/timeline/timeline.haml +++ b/app/views/timeline/timeline.haml @@ -2,11 +2,9 @@ - @timeline.each do |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 - .d-flex.justify-content-center.justify-content-sm-start - %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @timeline_last_id } } + .d-flex.justify-content-center.justify-content-sm-start#paginator + = button_to "#{request.path}?last_id=#{@timeline_last_id}", class: "btn btn-light" do = t("voc.load") - provide(:title, @title || APP_CONFIG["site_name"]) diff --git a/app/views/timeline/timeline.js.erb b/app/views/timeline/timeline.js.erb deleted file mode 100644 index 59591f8d..00000000 --- a/app/views/timeline/timeline.js.erb +++ /dev/null @@ -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 %> diff --git a/app/views/timeline/timeline.turbo_stream.haml b/app/views/timeline/timeline.turbo_stream.haml new file mode 100644 index 00000000..a840812e --- /dev/null +++ b/app/views/timeline/timeline.turbo_stream.haml @@ -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") diff --git a/config/routes.rb b/config/routes.rb index b2816cbd..075c3b83 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,7 +39,7 @@ Rails.application.routes.draw do end authenticate :user do - root to: "timeline#index", as: :timeline + root to: "timeline#index", via: [:get, :post], as: :timeline end get "/about", to: "about#about" @@ -138,8 +138,8 @@ Rails.application.routes.draw do end get "/discover", to: "discover#index", as: :discover - get "/public", to: "timeline#public", as: :public_timeline if APP_CONFIG.dig(:features, :public, :enabled) - get "/list/:list_name", to: "timeline#list", as: :list_timeline + match "/public", to: "timeline#public", via: [:get, :post], as: :public_timeline if APP_CONFIG.dig(:features, :public, :enabled) + 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" }