Refactor user answers to use a Turbo Stream-backed pagination

This commit is contained in:
Andreas Nedbal 2022-09-07 23:20:03 +02:00 committed by Andreas Nedbal
parent 3074ce3bbe
commit 753424f2d2
5 changed files with 20 additions and 21 deletions

View File

@ -15,7 +15,7 @@ class UserController < ApplicationController
respond_to do |format| respond_to do |format|
format.html format.html
format.js { render layout: false } format.turbo_stream
end end
end end

View File

@ -1,14 +1,13 @@
- unless @user.banned? = turbo_frame_tag "user_answers" do
#answers - unless @user.banned?
- @answers.each do |a| #answers
= render 'answerbox', a: a - @answers.each do |a|
= render 'answerbox', a: a
= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @answers_last_id - if @more_data_available
.d-flex.justify-content-center.justify-content-sm-start#paginator
- if @more_data_available = button_to user_path(@user, last_id: @answers_last_id), class: "btn btn-light" do
.d-flex.justify-content-center.justify-content-sm-start = t("voc.load")
%button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @answers_last_id } }
= t("voc.load")
:ruby :ruby
provide(:title, user_title(@user)) provide(:title, user_title(@user))

View File

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

View File

@ -0,0 +1,8 @@
= turbo_stream.append "answers" do
- @answers.each do |a|
= render 'answerbox', a: a
= turbo_stream.update "paginator" do
- if @more_data_available
= button_to user_path(@user, last_id: @answers_last_id), class: "btn btn-light" do
= t("voc.load")

View File

@ -145,8 +145,8 @@ Rails.application.routes.draw do
match "/inbox(/:author)", via: [:get, :post], to: "inbox#show", as: :inbox match "/inbox(/:author)", via: [:get, :post], to: "inbox#show", as: :inbox
get "/user/:username(/p/:page)", to: "user#show", defaults: { page: 1 } match "/user/:username(/p/:page)", to: "user#show", via: [:get, :post], defaults: { page: 1 }
get "/@:username(/p/:page)", to: "user#show", as: :user, defaults: { page: 1 } match "/@:username(/p/:page)", to: "user#show", via: [:get, :post], as: :user, defaults: { page: 1 }
get "/@:username/a/:id", to: "answer#show", via: "get", as: :answer get "/@:username/a/:id", to: "answer#show", via: "get", as: :answer
get "/@:username/q/:id", to: "question#show", via: "get", as: :question get "/@:username/q/:id", to: "question#show", via: "get", as: :question
get "/@:username/followers(/p/:page)", to: "user#followers", as: :show_user_followers, defaults: { page: 1 } get "/@:username/followers(/p/:page)", to: "user#followers", as: :show_user_followers, defaults: { page: 1 }