diff --git a/app/assets/javascripts/pagination.coffee b/app/assets/javascripts/pagination.coffee new file mode 100644 index 00000000..a97a249c --- /dev/null +++ b/app/assets/javascripts/pagination.coffee @@ -0,0 +1,15 @@ +jQuery -> + if $('#pagination').size() > 0 + $('#pagination').hide() + loading_posts = false + + $('#load-more-btn').show().click -> + unless loading_posts + loading_posts = true + more_posts_url = $('#pagination .pagination .next a').attr('href') + $this = $(this) + $this.html('').addClass('disabled') + $.getScript more_posts_url, -> + $this.text('Load more').removeClass('disabled') if $this + loading_posts = false + return \ No newline at end of file diff --git a/app/controllers/ajax/answer_controller.rb b/app/controllers/ajax/answer_controller.rb index c45d186f..61775889 100644 --- a/app/controllers/ajax/answer_controller.rb +++ b/app/controllers/ajax/answer_controller.rb @@ -12,6 +12,7 @@ class Ajax::AnswerController < ApplicationController end answer.user.decrement! :answered_count + answer.question.decrement! :answer_count if answer.user == current_user Inbox.create!(user: answer.user, question: answer.question, new: true) end # TODO: decide what happens with the question diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index b3c272b6..2650ef77 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -1,5 +1,12 @@ class StaticController < ApplicationController def index + if user_signed_in? + @timeline = current_user.timeline.paginate(page: params[:page]) + respond_to do |format| + format.html + format.js + end + end end def about diff --git a/app/views/static/index.html.haml b/app/views/static/index.html.haml index 8fe0d072..d7f1fdad 100644 --- a/app/views/static/index.html.haml +++ b/app/views/static/index.html.haml @@ -36,8 +36,15 @@ .col-md-9.col-xs-12.col-sm-9 = render 'layouts/messages' - - current_user.timeline.each do |answer| - = render 'shared/answerbox', a: answer + #timeline + - @timeline.each do |answer| + = render 'shared/answerbox', a: answer + + #pagination= will_paginate @timeline, renderer: BootstrapPagination::Rails, page_links: false + + - if @timeline.next_page + %button#load-more-btn.btn.btn-default{type: :button, 'data-current-page' => @timeline.current_page, 'data-type' => 'timeline'} + Load more = render "shared/links" - else .jumbotron diff --git a/app/views/static/index.js.erb b/app/views/static/index.js.erb new file mode 100644 index 00000000..64f5f33a --- /dev/null +++ b/app/views/static/index.js.erb @@ -0,0 +1,10 @@ +$('#timeline').append('<% @timeline.each do |answer| + %><%= j render 'shared/answerbox', a: answer +%><% end %>'); +<% if @timeline.next_page %> + var _p = $('#pagination'); + _p.replaceWith('<%= j will_paginate @timeline, renderer: BootstrapPagination::Rails, page_links: false %>'); + _p.hide(); +<% else %> + $('#pagination, #load-more-btn').remove(); +<% end %> \ No newline at end of file