Refactor questions to use a Turbo Stream-backed pagination

This commit is contained in:
Andreas Nedbal 2022-09-08 23:09:59 +02:00 committed by Andreas Nedbal
parent b595910564
commit 6ae03dbd7f
5 changed files with 12 additions and 14 deletions

View File

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

View File

@ -6,11 +6,9 @@
- @answers.each do |a| - @answers.each do |a|
= render "answerbox", a: a, show_question: false = render "answerbox", a: a, show_question: false
= render "shared/cursored_pagination_dummy", more_data_available: @more_data_available, last_id: @answers_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: @answers_last_id } } = button_to question_path(@question.user.screen_name, @question.id, last_id: @answers_last_id), class: "btn btn-light" do
= t("voc.load") = t("voc.load")
- if user_signed_in? && !current_user.answered?(@question) && current_user != @question.user && @question.user&.privacy_allow_stranger_answers - if user_signed_in? && !current_user.answered?(@question) && current_user != @question.user && @question.user&.privacy_allow_stranger_answers

View File

@ -1,8 +0,0 @@
$('#answers').append('<% @answers.each do |answer|
%><%= j render 'answerbox', a: answer, show_question: false
%><% 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, show_question: false
= turbo_stream.update "paginator" do
- if @more_data_available
= button_to question_path(@question.user.screen_name, @question.id, last_id: @answers_last_id), class: "btn btn-light" do
= t("voc.load")

View File

@ -148,7 +148,7 @@ Rails.application.routes.draw do
match "/user/:username(/p/:page)", to: "user#show", via: [:get, :post], defaults: { page: 1 } match "/user/:username(/p/:page)", to: "user#show", via: [:get, :post], defaults: { page: 1 }
match "/@:username(/p/:page)", to: "user#show", via: [:get, :post], 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 match "/@:username/q/:id", to: "question#show", via: [:get, :post], as: :question
match "/@:username/followers(/p/:page)", to: "user#followers", via: [:get, :post], as: :show_user_followers, defaults: { page: 1 } match "/@:username/followers(/p/:page)", to: "user#followers", via: [:get, :post], as: :show_user_followers, defaults: { page: 1 }
match "/@:username/followings(/p/:page)", to: "user#followings", via: [:get, :post], as: :show_user_followings, defaults: { page: 1 } match "/@:username/followings(/p/:page)", to: "user#followings", via: [:get, :post], as: :show_user_followings, defaults: { page: 1 }
match "/@:username/friends(/p/:page)", to: redirect("/@%{username}/followings/p/%{page}"), via: [:get, :post], defaults: { page: 1 } match "/@:username/friends(/p/:page)", to: redirect("/@%{username}/followings/p/%{page}"), via: [:get, :post], defaults: { page: 1 }