diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index fa55325cb..969a69e12 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -28,8 +28,8 @@ module NotificationsHelper tag.div id: dom_id(notification), class: "tray__item" do concat( link_to(notification, - class: [ "card card--notification", { "card--closed": notification.card.closed? } ], - data: { turbo_frame: "_top" }, + class: [ "card card--notification", { "card--closed": notification.card.closed? }, { "unread": !notification.read? } ], + data: { turbo_frame: "_top", badge_target: "unread", action: "badge#update" }, style: { "--card-color:": notification.card.color }, &) ) @@ -42,7 +42,7 @@ module NotificationsHelper method: :delete, class: "card__notification-unread-indicator btn btn--circle borderless", title: "Mark as unread", - data: { action: "form#submit:stop", form_target: "submit" }, + data: { action: "form#submit:stop badge#update:stop", form_target: "submit" }, form: { data: { controller: "form" } } do concat(icon_tag("unseen")) concat(tag.span("Mark as unread", class: "for-screen-reader")) @@ -51,7 +51,7 @@ module NotificationsHelper button_to read_notification_path(notification), class: "card__notification-unread-indicator btn btn--circle borderless", title: "Mark as read", - data: { action: "form#submit:stop", form_target: "submit" }, + data: { action: "form#submit:stop badge#update:stop", form_target: "submit" }, form: { data: { controller: "form" } } do concat(icon_tag("remove-med")) concat(tag.span("Mark as read", class: "for-screen-reader")) diff --git a/app/javascript/controllers/badge_controller.js b/app/javascript/controllers/badge_controller.js new file mode 100644 index 000000000..e84ee3013 --- /dev/null +++ b/app/javascript/controllers/badge_controller.js @@ -0,0 +1,43 @@ +import { Controller } from "@hotwired/stimulus" +import { onNextEventLoopTick } from "helpers/timing_helpers" + +export default class extends Controller { + static targets = [ "unread" ] + static classes = [ "unread" ] + + connect() { + onNextEventLoopTick(() => this.update()) + } + + update() { + onNextEventLoopTick(() => { + if (this.#available) { + const unreadCount = this.#unreadCount + + console.log(unreadCount, this.unreadTargets) + + if (unreadCount > 0) { + navigator.setAppBadge(unreadCount) + } else { + navigator.clearAppBadge() + } + } + }) + } + + clear() { + onNextEventLoopTick(() => { + if (this.#available) { + navigator.clearAppBadge() + } + }) + } + + get #unreadCount() { + return this.unreadTargets.filter(unreadTarget => unreadTarget.classList.contains(this.unreadClass)).length + } + + get #available() { + return "setAppBadge" in navigator + } +} diff --git a/app/views/notifications/_tray.html.erb b/app/views/notifications/_tray.html.erb index f87ac6f23..23caa68ea 100644 --- a/app/views/notifications/_tray.html.erb +++ b/app/views/notifications/_tray.html.erb @@ -1,6 +1,6 @@ <%= turbo_stream_from Current.user, :notifications %> -
+
@@ -10,7 +10,7 @@ <%= link_to settings_notifications_path, class: "btn borderless tray__notification-settings", title: "Notification settings", - data: { action: "click->dialog#close" } do %> + data: { action: "dialog#close" } do %> <%= icon_tag "settings" %> Settings <% end %> @@ -28,7 +28,7 @@ <%= button_to read_all_notifications_path, class: "btn borderless tray__clear-notifications", title: "Mark all notifications as read", - data: { action: "click->dialog#close", turbo_frame: "notifications" } do %> + data: { action: "dialog#close badge#clear", turbo_frame: "notifications" } do %> <%= icon_tag "check" %> Clear all <% end %> diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 4ef4d7fe2..eb84721c1 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -17,29 +17,31 @@ <% end %> -
- <% if @unread.any? %> -
-

New for you

- <%= button_to "Mark all as read", read_all_notifications_path, class: "btn txt-small", form: { data: { turbo: false } } %> +
+
+ <% if @unread.any? %> +
+

New for you

+ <%= button_to "Mark all as read", read_all_notifications_path, class: "btn txt-small", form: { data: { turbo: false } }, data: { action: "badge#clear" } %> +
+ <% else %> +
+ Nothing new for you. +
+ <% end %> + +
+ <%= render partial: "notifications/notification", collection: @unread, cached: true %>
- <% else %> -
- Nothing new for you. +
+ +
+

Previously seen

+ +
+ <%= render partial: "notifications/notification", collection: @page.records, cached: true %>
- <% end %> - -
- <%= render partial: "notifications/notification", collection: @unread, cached: true %> -
-
- -
-

Previously seen

- -
- <%= render partial: "notifications/notification", collection: @page.records, cached: true %> -
-
+
+ <%= notifications_next_page_link(@page) if @page.records.any? %>