Refactor user questions to use a Turbo Stream-backed pagination

This commit is contained in:
Andreas Nedbal 2022-09-07 23:20:47 +02:00 committed by Andreas Nedbal
parent 753424f2d2
commit 688a68e769
5 changed files with 18 additions and 19 deletions

View File

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

View File

@ -1,12 +1,11 @@
#questions
= turbo_frame_tag "user_questions" do
#questions
- @questions.each do |q|
= render 'shared/question', q: q, type: nil
= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @questions_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: @questions_last_id } }
- if @more_data_available
.d-flex.justify-content-center.justify-content-sm-start#paginator
= button_to show_user_questions_path(@user, last_id: @questions_last_id), class: "btn btn-light" do
= t("voc.load")
- provide(:title, questions_title(@user))

View File

@ -1,8 +0,0 @@
$('#questions').append('<% @questions.each do |q|
%><%= j render 'shared/question', q: q, type: nil
%><% end %>');
<% if @more_data_available %>
$('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @questions_last_id %>');
<% else %>
$('#pagination, #load-more-btn').remove();
<% end %>

View File

@ -0,0 +1,8 @@
= turbo_stream.append "questions" do
- @questions.each do |q|
= render 'shared/question', q: q, type: nil
= turbo_stream.update "paginator" do
- if @more_data_available
= button_to show_user_questions_path(@user, last_id: @questions_last_id), class: "btn btn-light" do
= t("voc.load")

View File

@ -152,7 +152,7 @@ Rails.application.routes.draw do
get "/@:username/followers(/p/:page)", to: "user#followers", as: :show_user_followers, defaults: { page: 1 }
get "/@:username/followings(/p/:page)", to: "user#followings", as: :show_user_followings, defaults: { page: 1 }
get "/@:username/friends(/p/:page)", to: redirect("/@%{username}/followings/p/%{page}"), defaults: { page: 1 }
get "/@:username/questions(/p/:page)", to: "user#questions", as: :show_user_questions, defaults: { page: 1 }
match "/@:username/questions(/p/:page)", to: "user#questions", via: [:get, :post], as: :show_user_questions, defaults: { page: 1 }
get "/:username(/p/:page)", to: "user#show", as: :user_alt, defaults: { page: 1 }
get "/:username/a/:id", to: "answer#show", as: :answer_alt
get "/:username/q/:id", to: "question#show", as: :question_alt