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 %>
<%= avatar_image_tag(notification.creator) %>
diff --git a/config/routes.rb b/config/routes.rb index 6b5153088..38860013e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,7 +17,11 @@ Rails.application.routes.draw do end resources :bubbles - resources :notifications + resources :notifications do + scope module: :notifications do + resource :readings, only: :create + end + end resources :buckets do resources :bubbles do diff --git a/test/controllers/notifications/readings_controller_test.rb b/test/controllers/notifications/readings_controller_test.rb new file mode 100644 index 000000000..cce305a3d --- /dev/null +++ b/test/controllers/notifications/readings_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class Notifications::ReadingsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end