Initial basic paginionation-ish for notifications (#1175)

This commit is contained in:
Ben Halpern 2018-11-21 20:03:56 -05:00 committed by GitHub
parent e36cf51a9e
commit eccee11106
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 73 additions and 46 deletions

View file

@ -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
});
}
});
}
}

View file

@ -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

View file

@ -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 %>
<a href="<%= actors.first["path"] %>" class="small-pic-link-wrapper">
<div class="small-pic">

View file

@ -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 %>
<div class="single-article single-article-small-pic <%= "unseen" unless notification.read? %>" data-notification-id="<%= notification.id %>" >
<%= render "#{notification.notifiable_type.downcase}", notification: notification %>
</div>
<% rescue => e %>
<% logger.error("Notifification error - #{e.message} - #{notification.id}") %>
<% end %>

View file

@ -42,16 +42,9 @@
<a class="query-filter-button <%= "selected" if params[:filter].to_s.downcase == "comments" %>" href="/notifications/comments">COMMENTS</a>
<a class="query-filter-button <%= "selected" if params[:filter].to_s.downcase == "posts" %>" href="/notifications/posts">POSTS</a>
</div>
<% 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 %>
<div class="single-article single-article-small-pic <%= "unseen" unless notification.read? %>">
<%= render "#{notification.notifiable_type.downcase}", notification: notification %>
</div>
<% rescue => e %>
<% logger.error("Notifification error - #{e.message} - #{notification.id}") %>
<%= render "notifications_list" %>
<% if @notifications.any? %>
<div id="notifications-pagination" data-pagination-path="/notifications/<%= params[:filter].to_s %>?page=<%= @notifications.last.id %>"></div>
<% end %>
<% else %>
<div class="content notification-content comment-content">

View file

@ -19,30 +19,31 @@
<img class="reacted-emoji" src="<%= asset_path("emoji/emoji-one-nausea-face.png") %>"/>
</button>
<% 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 %>
<a class="toggle-reply-form already-replied-link" href="<%= reply.path %>">
Replied
</a>
<% else %>
<a class="toggle-reply-form" data-reactable-id="<%= json_data["comment"]["id"] %>" href="#<%= json_data["comment"]["path"] %>">
Reply
</a>
<% end %>
</div>
<% @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| %>
<input type="hidden" name="authenticity_token" value="NOTHING" id="new_comment_authenticity_token">
<%= 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 %>
<a class="toggle-reply-form already-replied-link" href="<%= reply.path %>">
Replied
</a>
<% else %>
<a class="toggle-reply-form" data-reactable-id="<%= json_data["comment"]["id"] %>" href="#<%= json_data["comment"]["path"] %>">
Reply
</a>
<% end %>
</div>
<% @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"] %>
<div class="field" id="textarea-wrapper">
<%= f.text_area :body_markdown, id: "comment-textarea-for-#{json_data["comment"]["id"]}" %>
</div>
<div class="actions" id="submit-wrapper">
<%= f.submit "SUBMIT", id: "submit-button", onclick:"validateField(event)" %>
</div>
<%= f.hidden_field :parent_id, value: json_data["comment"]["id"] %>
<div class="field" id="textarea-wrapper">
<%= f.text_area :body_markdown, id: "comment-textarea-for-#{json_data["comment"]["id"]}" %>
</div>
<div class="actions" id="submit-wrapper">
<%= f.submit "SUBMIT", id: "submit-button", onclick:"validateField(event)" %>
</div>
<% end %>
<% end %>
<% end %>
<% end %>