From c60acebf2128926c5f9ec7836e01725a6d629ee3 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 7 Sep 2022 23:21:18 +0200 Subject: [PATCH] Refactor user relationships to use a Turbo Stream-backed pagination --- app/controllers/user_controller.rb | 6 ++--- app/views/user/show_follow.haml | 23 +++++++++++--------- app/views/user/show_follow.js.erb | 8 ------- app/views/user/show_follow.turbo_stream.haml | 13 +++++++++++ config/routes.rb | 6 ++--- 5 files changed, 32 insertions(+), 24 deletions(-) delete mode 100644 app/views/user/show_follow.js.erb create mode 100644 app/views/user/show_follow.turbo_stream.haml diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 73a9460e..119bd17a 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -26,11 +26,11 @@ class UserController < ApplicationController @relationships_last_id = @relationships.map(&:id).min @more_data_available = !@user.cursored_follower_relationships(last_id: @relationships_last_id, size: 1).count.zero? @users = @relationships.map(&:source) - @type = :friend + @type = :follower respond_to do |format| format.html { render "show_follow" } - format.js { render "show_follow", layout: false } + format.turbo_stream { render "show_follow" } end end @@ -46,7 +46,7 @@ class UserController < ApplicationController respond_to do |format| format.html { render "show_follow" } - format.js { render "show_follow", layout: false } + format.turbo_stream { render "show_follow" } end end # rubocop:enable Metrics/AbcSize diff --git a/app/views/user/show_follow.haml b/app/views/user/show_follow.haml index 183a2457..1d939922 100644 --- a/app/views/user/show_follow.haml +++ b/app/views/user/show_follow.haml @@ -1,14 +1,17 @@ -.row.row-cols-1.row-cols-sm-2.row-cols-md-3#users - - @users.each do |user| - .col.pb-3 - = render 'shared/userbox', user: user, type: @type += turbo_frame_tag "user_#{@type}" do + .row.row-cols-1.row-cols-sm-2.row-cols-md-3#users + - @users.each do |user| + .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 - %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @relationships_last_id } } - = t("voc.load") + - if @more_data_available + .d-flex.justify-content-center.justify-content-sm-start#paginator + - 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") - provide(:title, user_title(@user, 'friends and followers')) - parent_layout 'user/profile' diff --git a/app/views/user/show_follow.js.erb b/app/views/user/show_follow.js.erb deleted file mode 100644 index c164badb..00000000 --- a/app/views/user/show_follow.js.erb +++ /dev/null @@ -1,8 +0,0 @@ -$('#users').append('<% @users.each do |user| - %>
<%= j render 'shared/userbox', user: user, type: @type -%>
<% 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 %> diff --git a/app/views/user/show_follow.turbo_stream.haml b/app/views/user/show_follow.turbo_stream.haml new file mode 100644 index 00000000..9cee2066 --- /dev/null +++ b/app/views/user/show_follow.turbo_stream.haml @@ -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") diff --git a/config/routes.rb b/config/routes.rb index 18725121..b2816cbd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -149,9 +149,9 @@ Rails.application.routes.draw do 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/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/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 } + 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/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 } get "/:username(/p/:page)", to: "user#show", as: :user_alt, defaults: { page: 1 } get "/:username/a/:id", to: "answer#show", as: :answer_alt