From 952895ae67f27106c9253f2061b780350a47ffa0 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Thu, 9 Jan 2025 18:25:35 +0000 Subject: [PATCH] WIP: show notifications in a tray --- app/assets/stylesheets/notifications.css | 15 +++++++++++++++ app/controllers/notifications_controller.rb | 5 +++++ app/helpers/notifications_helper.rb | 7 +++++++ app/models/notification.rb | 4 ++-- app/views/bubbles/index.html.erb | 2 ++ app/views/notifications/_notification.html.erb | 3 +++ app/views/notifications/index.html.erb | 3 +++ config/routes.rb | 1 + test/controllers/notifications_controller_test.rb | 7 +++++++ 9 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 app/assets/stylesheets/notifications.css create mode 100644 app/controllers/notifications_controller.rb create mode 100644 app/helpers/notifications_helper.rb create mode 100644 app/views/notifications/_notification.html.erb create mode 100644 app/views/notifications/index.html.erb create mode 100644 test/controllers/notifications_controller_test.rb diff --git a/app/assets/stylesheets/notifications.css b/app/assets/stylesheets/notifications.css new file mode 100644 index 000000000..164330528 --- /dev/null +++ b/app/assets/stylesheets/notifications.css @@ -0,0 +1,15 @@ +.notification-tray { + position: fixed; + width: 40ch; + bottom: 1rem; + left: calc(50% - 20ch); + display: flex; + flex-direction: column; + gap: 1rem; +} + +.notification { + background-color: #eeed; + border-radius: 6px; + padding: 0.5rem; +} diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb new file mode 100644 index 000000000..22e40167e --- /dev/null +++ b/app/controllers/notifications_controller.rb @@ -0,0 +1,5 @@ +class NotificationsController < ApplicationController + def index + @notifications = Current.user.notifications.unread.ordered.limit(20) + end +end diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb new file mode 100644 index 000000000..b7fe8de4d --- /dev/null +++ b/app/helpers/notifications_helper.rb @@ -0,0 +1,7 @@ +module NotificationsHelper + def notification_tray_tag + tag.div class: "notification-tray" do + turbo_frame_tag "notifications", src: notifications_path, data: { turbo_permanent: true } + end + end +end diff --git a/app/models/notification.rb b/app/models/notification.rb index 56d2b8ffd..5553dfad9 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -2,6 +2,6 @@ class Notification < ApplicationRecord belongs_to :user belongs_to :bubble - scope :unread, -> { where.not(:read) } - scope :ordered, -> { order(created_at: :desc) } + scope :unread, -> { where(read: false) } + scope :ordered, -> { order(read: :desc, created_at: :desc) } end diff --git a/app/views/bubbles/index.html.erb b/app/views/bubbles/index.html.erb index bc43bf2ab..7928afacf 100644 --- a/app/views/bubbles/index.html.erb +++ b/app/views/bubbles/index.html.erb @@ -50,3 +50,5 @@ <%= render partial: "bubbles/list/bubble", collection: @bubbles, cached: true %> + +<%= notification_tray_tag %> diff --git a/app/views/notifications/_notification.html.erb b/app/views/notifications/_notification.html.erb new file mode 100644 index 000000000..c87ac7166 --- /dev/null +++ b/app/views/notifications/_notification.html.erb @@ -0,0 +1,3 @@ +
+

<%= notification.body %>

+
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb new file mode 100644 index 000000000..ee91b07e4 --- /dev/null +++ b/app/views/notifications/index.html.erb @@ -0,0 +1,3 @@ +<%= turbo_frame_tag "notifications" do %> + <%= render @notifications %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 5f16ae681..d0f860f96 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,7 @@ Rails.application.routes.draw do end resources :bubbles + resources :notifications resources :buckets do resources :bubbles do diff --git a/test/controllers/notifications_controller_test.rb b/test/controllers/notifications_controller_test.rb new file mode 100644 index 000000000..20b28201d --- /dev/null +++ b/test/controllers/notifications_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class NotificationsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end