From eccee1110650eb13fce8daaa8600718102ecbd3e Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Wed, 21 Nov 2018 20:03:56 -0500 Subject: [PATCH] Initial basic paginionation-ish for notifications (#1175) --- .../initializers/initNotifications.js | 17 +++++++ app/controllers/notifications_controller.rb | 29 +++++++----- .../_aggregated_reactions.html.erb | 3 +- .../_notifications_list.html.erb | 10 ++++ app/views/notifications/index.html.erb | 13 ++--- .../shared/_comment_box.html.erb | 47 ++++++++++--------- 6 files changed, 73 insertions(+), 46 deletions(-) create mode 100644 app/views/notifications/_notifications_list.html.erb diff --git a/app/assets/javascripts/initializers/initNotifications.js b/app/assets/javascripts/initializers/initNotifications.js index a2a44d12a..2b2cd6233 100644 --- a/app/assets/javascripts/initializers/initNotifications.js +++ b/app/assets/javascripts/initializers/initNotifications.js @@ -3,6 +3,7 @@ function initNotifications() { markNotificationsAsRead(); initReactions(); listenForNotificationsBellClick(); + initPagination(); } function markNotificationsAsRead() { @@ -116,3 +117,19 @@ function listenForNotificationsBellClick() { }; }, 180); } + +function initPagination() { + var el = document.getElementById("notifications-pagination") + if (el) { + window.fetch(el.dataset.paginationPath, { + method: 'GET', + credentials: 'same-origin' + }).then(function (response) { + if (response.status === 200) { + response.text().then(function(html){ + el.innerHTML = html + }); + } + }); + } +} \ No newline at end of file diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 377a053e4..179ae13d4 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -8,21 +8,28 @@ class NotificationsController < ApplicationController else current_user end - if params[:filter].to_s.downcase == "posts" - @notifications = NotificationDecorator. - decorate_collection(Notification.where(user_id: current_user.id, notifiable_type: "Article", action: "Published"). - order("notified_at DESC").limit(55).to_a) - elsif params[:filter].to_s.downcase == "comments" - @notifications = NotificationDecorator. - decorate_collection(Notification.where(user_id: current_user.id, notifiable_type: "Comment", action: nil). # Nil action means not reaction in this context - order("notified_at DESC").limit(55).to_a) + if params[:page] + num = 48 + notified_at_offset = Notification.find(params[:page])&.notified_at else - @notifications = NotificationDecorator. - decorate_collection(Notification.where(user_id: current_user.id). - order("notified_at DESC").limit(55).to_a) + num = 12 + end + if params[:filter].to_s.downcase == "posts" + @notifications = Notification.where(user_id: current_user.id, notifiable_type: "Article", action: "Published"). + order("notified_at DESC") + elsif params[:filter].to_s.downcase == "comments" + @notifications = Notification.where(user_id: current_user.id, notifiable_type: "Comment", action: nil). # Nil action means not reaction in this context + or(Notification.where(user_id: current_user.id, notifiable_type: "Mention")). + order("notified_at DESC") + else + @notifications = Notification.where(user_id: current_user.id). + order("notified_at DESC") end @last_user_reaction = @user.reactions.last&.id @last_user_comment = @user.comments.last&.id + @notifications = @notifications.where("notified_at < ?", notified_at_offset) if notified_at_offset + @notifications = NotificationDecorator.decorate_collection(@notifications.limit(num)) + render partial: "notifications_list" if notified_at_offset end end diff --git a/app/views/notifications/_aggregated_reactions.html.erb b/app/views/notifications/_aggregated_reactions.html.erb index 515198c57..f77b4a45a 100644 --- a/app/views/notifications/_aggregated_reactions.html.erb +++ b/app/views/notifications/_aggregated_reactions.html.erb @@ -1,9 +1,8 @@ <%# TODO: change to map of IDs %> <% siblings = notification.json_data["reaction"]["aggregated_siblings"].select{|n| n["created_at"] > 24.hours.ago } %> -<% cache "activity-aggregated-reactions-#{siblings}_#{siblings}" do %> +<% cache "activity-aggregated-reactions-#{siblings}" do %> <% actors = siblings.map { |n| n["user"] }.uniq %> <% reactable_data = notification.json_data["reaction"]["reactable"] %> - <% cache "activity-profile-pic-#{actors.first["id"]}-#{actors.first["profile_image_90"]}" do %> " class="small-pic-link-wrapper">
diff --git a/app/views/notifications/_notifications_list.html.erb b/app/views/notifications/_notifications_list.html.erb new file mode 100644 index 000000000..3244320bf --- /dev/null +++ b/app/views/notifications/_notifications_list.html.erb @@ -0,0 +1,10 @@ +<% notification_count = 0 %> +<% @notifications.each do |notification| %> + <% next if (notification.notified_at < 24.hours.ago && notification.aggregated?) %> + <% notification_count = notification_count + 1 %> +
" data-notification-id="<%= notification.id %>" > + <%= render "#{notification.notifiable_type.downcase}", notification: notification %> +
+<% rescue => e %> + <% logger.error("Notifification error - #{e.message} - #{notification.id}") %> +<% end %> diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index b829cb7da..5c9111fca 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -42,16 +42,9 @@
" href="/notifications/comments">COMMENTS " href="/notifications/posts">POSTS
- <% notification_count = 0 %> - <% @notifications.each do |notification| %> - <% next if (notification.notified_at < 24.hours.ago && notification.aggregated?) %> - <% notification_count = notification_count + 1 %> - <% break if notification_count > 45 %> -
"> - <%= render "#{notification.notifiable_type.downcase}", notification: notification %> -
- <% rescue => e %> - <% logger.error("Notifification error - #{e.message} - #{notification.id}") %> + <%= render "notifications_list" %> + <% if @notifications.any? %> +
<% end %> <% else %>
diff --git a/app/views/notifications/shared/_comment_box.html.erb b/app/views/notifications/shared/_comment_box.html.erb index 0a6248be8..1e30af907 100644 --- a/app/views/notifications/shared/_comment_box.html.erb +++ b/app/views/notifications/shared/_comment_box.html.erb @@ -19,30 +19,31 @@ "/> <% end %> - <% reply = Comment.select(:id, :user_id, :ancestry).find_by(id: json_data["comment"]["id"]).children.find_by(user_id: current_user.id) %> - <% if reply %> - - Replied - - <% else %> - " href="#<%= json_data["comment"]["path"] %>"> - Reply - - <% end %> -
- <% @comment = Comment.new %> - <%= form_for(@comment, authenticity_token: false, html: {id: "comment-form-for-#{json_data["comment"]["id"]}", onsubmit: "handleCommentSubmit.bind(this)(event)"}) do |f| %> - - <%= f.hidden_field :commentable_id, value: json_data["comment"]["commentable"]["id"] %> - <%= f.hidden_field :commentable_type, value: json_data["comment"]["commentable"]["class"]["name"] %> + <% cache "comment-box-comment-reply-#{@last_user_comment}-#{json_data["comment"]["updated_at"]}-#{json_data["comment"]["id"]}" do %> + <% reply = Comment.select(:id, :user_id, :ancestry).find_by(id: json_data["comment"]["id"]).children.find_by(user_id: current_user.id) %> + <% if reply %> + + Replied + + <% else %> + " href="#<%= json_data["comment"]["path"] %>"> + Reply + + <% end %> + + <% @comment = Comment.new %> + <%= form_for(@comment, authenticity_token: false, html: {id: "comment-form-for-#{json_data["comment"]["id"]}", onsubmit: "handleCommentSubmit.bind(this)(event)"}) do |f| %> + <%= f.hidden_field :commentable_id, value: json_data["comment"]["commentable"]["id"] %> + <%= f.hidden_field :commentable_type, value: json_data["comment"]["commentable"]["class"]["name"] %> - <%= f.hidden_field :parent_id, value: json_data["comment"]["id"] %> -
- <%= f.text_area :body_markdown, id: "comment-textarea-for-#{json_data["comment"]["id"]}" %> -
-
- <%= f.submit "SUBMIT", id: "submit-button", onclick:"validateField(event)" %> -
+ <%= f.hidden_field :parent_id, value: json_data["comment"]["id"] %> +
+ <%= f.text_area :body_markdown, id: "comment-textarea-for-#{json_data["comment"]["id"]}" %> +
+
+ <%= f.submit "SUBMIT", id: "submit-button", onclick:"validateField(event)" %> +
+ <% end %> <% end %> <% end %> <% end %>