Deduplicate shared logic in `TimelineController`

This commit is contained in:
Karina Kwiatek 2022-07-19 17:28:38 +02:00 committed by Karina Kwiatek
parent ec702c90b8
commit 6e61e0cabe
7 changed files with 17 additions and 65 deletions

View File

@ -4,36 +4,30 @@ class TimelineController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
def index def index
@timeline = current_user.cursored_timeline(last_id: params[:last_id]) paginate_timeline { |args| current_user.cursored_timeline(**args) }
@timeline_last_id = @timeline.map(&:id).min
@more_data_available = !current_user.cursored_timeline(last_id: @timeline_last_id, size: 1).count.zero?
respond_to do |format|
format.html
format.js { render layout: false }
end
end end
def list def list
@list = current_user.lists.find_by!(name: params[:list_name]) @list = current_user.lists.find_by!(name: params[:list_name])
@timeline = @list.cursored_timeline(last_id: params[:last_id]) @title = list_title(current_user.lists.find_by!(name: params[:list_name]))
@timeline_last_id = @timeline.map(&:id).min paginate_timeline { |args| @list.cursored_timeline(**args) }
@more_data_available = !@list.cursored_timeline(last_id: @timeline_last_id, size: 1).count.zero?
respond_to do |format|
format.html
format.js { render layout: false }
end
end end
def public def public
@timeline = Answer.cursored_public_timeline(last_id: params[:last_id]) @title = generate_title("Public Timeline")
paginate_timeline { |args| Answer.cursored_public_timeline(**args) }
end
private
def paginate_timeline
@timeline = yield(last_id: params[:last_id])
@timeline_last_id = @timeline.map(&:id).min @timeline_last_id = @timeline.map(&:id).min
@more_data_available = !Answer.cursored_public_timeline(last_id: @timeline_last_id, size: 1).count.zero? @more_data_available = !yield(last_id: @timeline_last_id, size: 1).count.zero?
respond_to do |format| respond_to do |format|
format.html format.html { render "timeline/timeline" }
format.js { render layout: false } format.js { render "timeline/timeline", layout: false }
end end
end end
end end

View File

@ -1,13 +0,0 @@
#timeline
- @timeline.each do |answer|
= render 'answerbox', a: answer
= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_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: @timeline_last_id } }
= t 'views.actions.load'
- provide(:title, list_title(@list))
- parent_layout 'feed'

View File

@ -1,8 +0,0 @@
$('#timeline').append('<% @timeline.each do |answer|
%><%= j render 'answerbox', a: answer
%><% end %>');
<% if @more_data_available %>
$('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id %>');
<% else %>
$('#pagination, #load-more-btn').remove();
<% end %>

View File

@ -1,13 +0,0 @@
#timeline
- @timeline.each do |answer|
= render 'answerbox', a: answer
= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_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: @timeline_last_id } }
= t 'views.actions.load'
- provide(:title, generate_title('Public Timeline'))
- parent_layout 'feed'

View File

@ -1,8 +0,0 @@
$('#timeline').append('<% @timeline.each do |answer|
%><%= j render 'answerbox', a: answer
%><% end %>');
<% if @more_data_available %>
$('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id %>');
<% else %>
$('#pagination, #load-more-btn').remove();
<% end %>

View File

@ -1,13 +1,13 @@
#timeline #timeline
- @timeline.each do |answer| - @timeline.each do |answer|
= render 'answerbox', a: answer = render "answerbox", a: answer
= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id = render "shared/cursored_pagination_dummy", more_data_available: @more_data_available, last_id: @timeline_last_id
- if @more_data_available - if @more_data_available
.d-flex.justify-content-center.justify-content-sm-start .d-flex.justify-content-center.justify-content-sm-start
%button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @timeline_last_id } } %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @timeline_last_id } }
= t 'views.actions.load' = t 'views.actions.load'
- provide(:title, APP_CONFIG['site_name']) - provide(:title, @title ||= APP_CONFIG["site_name"])
- parent_layout 'feed' - parent_layout 'feed'