Refactor user relationships to use a Turbo Stream-backed pagination

This commit is contained in:
Andreas Nedbal 2022-09-07 23:21:18 +02:00 committed by Andreas Nedbal
parent 688a68e769
commit c60acebf21
5 changed files with 32 additions and 24 deletions

View File

@ -26,11 +26,11 @@ class UserController < ApplicationController
@relationships_last_id = @relationships.map(&:id).min @relationships_last_id = @relationships.map(&:id).min
@more_data_available = !@user.cursored_follower_relationships(last_id: @relationships_last_id, size: 1).count.zero? @more_data_available = !@user.cursored_follower_relationships(last_id: @relationships_last_id, size: 1).count.zero?
@users = @relationships.map(&:source) @users = @relationships.map(&:source)
@type = :friend @type = :follower
respond_to do |format| respond_to do |format|
format.html { render "show_follow" } format.html { render "show_follow" }
format.js { render "show_follow", layout: false } format.turbo_stream { render "show_follow" }
end end
end end
@ -46,7 +46,7 @@ class UserController < ApplicationController
respond_to do |format| respond_to do |format|
format.html { render "show_follow" } format.html { render "show_follow" }
format.js { render "show_follow", layout: false } format.turbo_stream { render "show_follow" }
end end
end end
# rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/AbcSize

View File

@ -1,14 +1,17 @@
.row.row-cols-1.row-cols-sm-2.row-cols-md-3#users = turbo_frame_tag "user_#{@type}" do
- @users.each do |user| .row.row-cols-1.row-cols-sm-2.row-cols-md-3#users
.col.pb-3 - @users.each do |user|
= render 'shared/userbox', user: user, type: @type .col.pb-3
= render 'shared/userbox', user: user, type: @type
= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @relationships_last_id - if @more_data_available
.d-flex.justify-content-center.justify-content-sm-start#paginator
- if @more_data_available - if @type === :follower
.d-flex.justify-content-center.justify-content-sm-start = button_to show_user_followers_path(@user, last_id: @relationships_last_id), class: "btn btn-light" do
%button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @relationships_last_id } } = t("voc.load")
= t("voc.load") - else
= button_to show_user_followings_path(@user, last_id: @relationships_last_id), class: "btn btn-light" do
= t("voc.load")
- provide(:title, user_title(@user, 'friends and followers')) - provide(:title, user_title(@user, 'friends and followers'))
- parent_layout 'user/profile' - parent_layout 'user/profile'

View File

@ -1,8 +0,0 @@
$('#users').append('<% @users.each do |user|
%><div class="col pb-3"><%= j render 'shared/userbox', user: user, type: @type
%></div><% end %>');
<% if @more_data_available %>
$('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @relationships_last_id %>');
<% else %>
$('#pagination, #load-more-btn').remove();
<% end %>

View File

@ -0,0 +1,13 @@
= turbo_stream.append "users" do
- @users.each do |user|
.col.pb-3
= render 'shared/userbox', user: user, type: @type
= turbo_stream.update "paginator" do
- if @more_data_available
- if @type === :follower
= button_to show_user_followers_path(@user, last_id: @relationships_last_id), class: "btn btn-light" do
= t("voc.load")
- else
= button_to show_user_followings_path(@user, last_id: @relationships_last_id), class: "btn btn-light" do
= t("voc.load")

View File

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