From 688a68e769c0ec8f039000bf557c0c755555327d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 7 Sep 2022 23:20:47 +0200 Subject: [PATCH] Refactor user questions to use a Turbo Stream-backed pagination --- app/controllers/user_controller.rb | 2 +- app/views/user/questions.haml | 17 ++++++++--------- app/views/user/questions.js.erb | 8 -------- app/views/user/questions.turbo_stream.haml | 8 ++++++++ config/routes.rb | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) delete mode 100644 app/views/user/questions.js.erb create mode 100644 app/views/user/questions.turbo_stream.haml diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index d3f4e129..73a9460e 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -60,7 +60,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/questions.haml b/app/views/user/questions.haml index b662abc8..08727c5a 100644 --- a/app/views/user/questions.haml +++ b/app/views/user/questions.haml @@ -1,13 +1,12 @@ -#questions - - @questions.each do |q| - = render 'shared/question', q: q, type: nil += 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 } } - = t("voc.load") + - 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)) - parent_layout 'user/profile' diff --git a/app/views/user/questions.js.erb b/app/views/user/questions.js.erb deleted file mode 100644 index 5b9e0ce7..00000000 --- a/app/views/user/questions.js.erb +++ /dev/null @@ -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 %> diff --git a/app/views/user/questions.turbo_stream.haml b/app/views/user/questions.turbo_stream.haml new file mode 100644 index 00000000..75175a5a --- /dev/null +++ b/app/views/user/questions.turbo_stream.haml @@ -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") diff --git a/config/routes.rb b/config/routes.rb index 37958092..18725121 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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