From 753424f2d2c93c75a9481d1c7798160077313fe7 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 7 Sep 2022 23:20:03 +0200 Subject: [PATCH] Refactor user answers to use a Turbo Stream-backed pagination --- app/controllers/user_controller.rb | 2 +- app/views/user/show.haml | 19 +++++++++---------- app/views/user/show.js.erb | 8 -------- app/views/user/show.turbo_stream.haml | 8 ++++++++ config/routes.rb | 4 ++-- 5 files changed, 20 insertions(+), 21 deletions(-) delete mode 100644 app/views/user/show.js.erb create mode 100644 app/views/user/show.turbo_stream.haml diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 888321e6..d3f4e129 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -15,7 +15,7 @@ class UserController < ApplicationController respond_to do |format| format.html - format.js { render layout: false } + format.turbo_stream end end diff --git a/app/views/user/show.haml b/app/views/user/show.haml index 6b42e1cc..442b414f 100644 --- a/app/views/user/show.haml +++ b/app/views/user/show.haml @@ -1,14 +1,13 @@ -- unless @user.banned? - #answers - - @answers.each do |a| - = render 'answerbox', a: a += turbo_frame_tag "user_answers" do + - unless @user.banned? + #answers + - @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 - %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @answers_last_id } } - = t("voc.load") + - if @more_data_available + .d-flex.justify-content-center.justify-content-sm-start#paginator + = button_to user_path(@user, last_id: @answers_last_id), class: "btn btn-light" do + = t("voc.load") :ruby provide(:title, user_title(@user)) diff --git a/app/views/user/show.js.erb b/app/views/user/show.js.erb deleted file mode 100644 index be445de6..00000000 --- a/app/views/user/show.js.erb +++ /dev/null @@ -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 %> diff --git a/app/views/user/show.turbo_stream.haml b/app/views/user/show.turbo_stream.haml new file mode 100644 index 00000000..f4aaf60b --- /dev/null +++ b/app/views/user/show.turbo_stream.haml @@ -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") diff --git a/config/routes.rb b/config/routes.rb index c7e2426f..37958092 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -145,8 +145,8 @@ Rails.application.routes.draw do match "/inbox(/:author)", via: [:get, :post], to: "inbox#show", as: :inbox - get "/user/:username(/p/:page)", to: "user#show", defaults: { page: 1 } - get "/@:username(/p/:page)", to: "user#show", as: :user, 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 } 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 }