diff --git a/app/controllers/notifications/readings_controller.rb b/app/controllers/notifications/readings_controller.rb new file mode 100644 index 000000000..a5d0f7f11 --- /dev/null +++ b/app/controllers/notifications/readings_controller.rb @@ -0,0 +1,5 @@ +class Notifications::ReadingsController < ApplicationController + def create + Current.user.notifications.find(params[:notification_id]).update!(read: true) + end +end diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 8ac611eb4..1507a090f 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -3,13 +3,22 @@ module NotificationsHelper notification_stream_tag + notification_tray_tag end + def notification_tag(notification, &) + link_to notification.resource, class: "notification", + data: { + turbo_frame: "_top", + action: "notifications--readings#record", + notifications__readings_url_param: notification_readings_url(notification) + }, & + end + private def notification_stream_tag turbo_stream_from Current.user, :notifications end def notification_tray_tag - tag.div id: "notification-tray", class: "notification-tray", data: { turbo_permanent: true } do + tag.div id: "notification-tray", class: "notification-tray", data: { turbo_permanent: true, controller: "notifications--readings" } do turbo_frame_tag("notifications", src: notifications_path) end end diff --git a/app/javascript/controllers/notifications/readings_controller.js b/app/javascript/controllers/notifications/readings_controller.js new file mode 100644 index 000000000..5ec9ffca1 --- /dev/null +++ b/app/javascript/controllers/notifications/readings_controller.js @@ -0,0 +1,18 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + record({ target, params: { url } }) { + navigator.sendBeacon(url, this.#csrfPayload()) + target.remove() + } + + #csrfPayload() { + const data = new FormData() + data.append("authenticity_token", this.#csrfToken()) + return data + } + + #csrfToken() { + return document.querySelector('meta[name="csrf-token"]').content + } +} diff --git a/app/views/notifications/_notification.html.erb b/app/views/notifications/_notification.html.erb index 1d1bfd11f..9a33e5c67 100644 --- a/app/views/notifications/_notification.html.erb +++ b/app/views/notifications/_notification.html.erb @@ -1,4 +1,4 @@ -<%= link_to notification.resource, class: "notification", data: { turbo_frame: "_top" } do %> +<%= notification_tag notification do %>