diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 49d9e32c..d1b752a9 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -1,19 +1,71 @@ class NotificationsController < ApplicationController + before_action :set_receipts, only: [:index, :show, :mark_read, :mark_unread] + before_action :set_notification, only: [:show, :mark_read, :mark_unread] + before_action :set_receipt, only: [:show, :mark_read, :mark_unread] + def index @notifications = current_user.mailbox.notifications respond_to do |format| format.html - format.json { render json: @notifications.to_json } + format.json do + render json: @notifications.map do |notification| + receipt = @receipts.find_by(notification_id: notification.id) + notification.as_json.merge(is_read: receipt.is_read) + end + end end end def show - @notification = current_user.mailbox.notifications.find_by(id: params[:id]) - + @receipt.update(is_read: true) respond_to do |format| format.html - format.json { render json: @notification.to_json } + format.json do + render json: @notification.as_json.merge( + is_read: @receipt.is_read + ) + end end end + + def mark_read + @receipt.update(is_read: true) + respond_to do |format| + format.js + format.json do + render json: @notification.as_json.merge( + is_read: @receipt.is_read + ) + end + end + end + + def mark_unread + @receipt.update(is_read: false) + respond_to do |format| + format.js + format.json do + render json: @notification.as_json.merge( + is_read: @receipt.is_read + ) + end + end + end + + private + + def set_receipts + @receipts = current_user.mailbox.receipts + .includes(:notification).where(mailbox_type: nil) + end + + def set_notification + @notification = current_user.mailbox.notifications.find_by(id: params[:id]) + end + + def set_receipt + @receipt = @receipts.find_by(notification_id: params[:id]) + end + end diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 9efae3d6..a1f5f832 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -6,11 +6,19 @@

Notifications

diff --git a/app/views/notifications/mark_read.js.erb b/app/views/notifications/mark_read.js.erb new file mode 100644 index 00000000..63b2965e --- /dev/null +++ b/app/views/notifications/mark_read.js.erb @@ -0,0 +1,3 @@ +$('#notification-<%= @notification.id %> > a') + .text('(read)') + .attr('href', '<%= mark_unread_notification_path(@notification.id) %>') diff --git a/app/views/notifications/mark_unread.js.erb b/app/views/notifications/mark_unread.js.erb new file mode 100644 index 00000000..12fa138b --- /dev/null +++ b/app/views/notifications/mark_unread.js.erb @@ -0,0 +1,3 @@ +$('#notification-<%= @notification.id %> > a') + .text('(unread)') + .attr('href', '<%= mark_read_notification_path(@notification.id) %>') diff --git a/config/routes.rb b/config/routes.rb index 8bd009b5..b34908fd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,7 +36,12 @@ Metamaps::Application.routes.draw do resources :mappings, except: [:index, :new, :edit] resources :messages, only: [:show, :create, :update, :destroy] - resources :notifications, only: [:index, :show] + resources :notifications, only: [:index, :show] do + member do + put :mark_read + put :mark_unread + end + end resources :metacode_sets, except: [:show]