From 0674f1e4bb11e169b1ac3535c757030ec1697775 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Tue, 9 Jan 2024 11:58:37 +0000 Subject: [PATCH] feat: add notifications page and remove unread_notification.rb --- app/components/unread_notification.html.erb | 15 ------------- app/components/unread_notification.rb | 12 ----------- app/controllers/notifications_controller.rb | 13 ++++++++++-- app/frontend/styles/_header.scss | 4 ++++ app/helpers/application_helper.rb | 2 ++ app/helpers/navigation_items_helper.rb | 2 +- app/models/user.rb | 8 +++++++ app/views/layouts/application.html.erb | 4 ++-- .../_notification_banner.html.erb | 21 +++++++++++++++++++ app/views/notifications/show.html.erb | 15 +++++++++++++ config/routes.rb | 2 +- 11 files changed, 65 insertions(+), 33 deletions(-) delete mode 100644 app/components/unread_notification.html.erb delete mode 100644 app/components/unread_notification.rb create mode 100644 app/views/notifications/_notification_banner.html.erb create mode 100644 app/views/notifications/show.html.erb diff --git a/app/components/unread_notification.html.erb b/app/components/unread_notification.html.erb deleted file mode 100644 index acdf2d859..000000000 --- a/app/components/unread_notification.html.erb +++ /dev/null @@ -1,15 +0,0 @@ -
-
-
-
-
-

<%= oldest_unread_notification.title %>

- <%= govuk_link_to oldest_unread_notification.link_text, notification_path(oldest_unread_notification), class: "govuk-link--inverse govuk-!-font-weight-bold"%> -
-

- <%= govuk_link_to "Dismiss", notification_dismiss_path(oldest_unread_notification), class: "govuk-link--inverse"%> -

-
-
-
-
diff --git a/app/components/unread_notification.rb b/app/components/unread_notification.rb deleted file mode 100644 index d86280e31..000000000 --- a/app/components/unread_notification.rb +++ /dev/null @@ -1,12 +0,0 @@ -class UnreadNotification < ViewComponent::Base - attr_reader :current_user - - def initialize(current_user:) - @current_user = current_user - super - end - - def oldest_unread_notification - Notification.unread_by(@current_user).first - end -end diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 73fe702d7..d48d57b41 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -1,8 +1,17 @@ class NotificationsController < ApplicationController def dismiss - Notification.find(params[:notification_id]).mark_as_read! for: current_user + current_user.oldest_active_unread_notification.mark_as_read! for: current_user - redirect_to root_path + redirect_back(fallback_location: root_path) + end + + def show + @notification = current_user.oldest_active_unread_notification + if @notification&.page_content + render "show" + else + redirect_back(fallback_location: root_path) + end end end diff --git a/app/frontend/styles/_header.scss b/app/frontend/styles/_header.scss index 12cfd4e54..924276d5f 100644 --- a/app/frontend/styles/_header.scss +++ b/app/frontend/styles/_header.scss @@ -26,3 +26,7 @@ .app-header--orange .govuk-header__container { border-bottom-color: govuk-colour("orange"); } + +.app-header__no-border-bottom { + border-bottom: 0; +} diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 971dd68d9..e91261466 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -12,6 +12,8 @@ module ApplicationHelper def govuk_header_classes(current_user) if current_user && current_user.support? "app-header app-header--orange" + elsif current_user && Notification.unread_by(current_user).present? && !current_page?(notifications_path) + "app-header app-header__no-border-bottom" else "app-header" end diff --git a/app/helpers/navigation_items_helper.rb b/app/helpers/navigation_items_helper.rb index 18132a8a1..95e8cc9cf 100644 --- a/app/helpers/navigation_items_helper.rb +++ b/app/helpers/navigation_items_helper.rb @@ -47,7 +47,7 @@ module NavigationItemsHelper private def home_current?(path) - path == root_path + path == root_path || path == notifications_path end def lettings_logs_current?(path) diff --git a/app/models/user.rb b/app/models/user.rb index 74ac34547..1aa7898e1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -229,6 +229,14 @@ class User < ApplicationRecord sales_logs.after_date(FormHandler.instance.sales_earliest_open_for_editing_collection_start_date).duplicate_sets(id).map { |array_str| array_str ? array_str.map(&:to_i) : [] } end + def active_unread_notifications + Notification.unread_by(self) + end + + def oldest_active_unread_notification + active_unread_notifications.first + end + protected # Checks whether a password is needed or not. For validations only. diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c540ad5cc..afe153016 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -101,8 +101,8 @@ end end %> - <% if current_user.present? && current_page?("/") && Notification.unread_by(current_user).present? %> - <%= render UnreadNotification.new(current_user:) %> + <% if current_user.present? && Notification.unread_by(current_user).present? && !current_page?(notifications_path)%> + <%= render "notifications/notification_banner" %> <% end %> diff --git a/app/views/notifications/_notification_banner.html.erb b/app/views/notifications/_notification_banner.html.erb new file mode 100644 index 000000000..18defb23f --- /dev/null +++ b/app/views/notifications/_notification_banner.html.erb @@ -0,0 +1,21 @@ +
+
+
+
+
+ <% if current_user.active_unread_notifications.count > 1 %> +

Notification 1 of <%= current_user.active_unread_notifications.count %>

+ <% end %> +

<%= current_user.oldest_active_unread_notification.title %>

+ <% if current_user.oldest_active_unread_notification.page_content.present? %> +
+ <%= govuk_link_to current_user.oldest_active_unread_notification.link_text, notifications_path, class: "govuk-link--inverse govuk-!-font-weight-bold"%> +
+ <% end %> +
+

+ <%= govuk_link_to "Dismiss", dismiss_notifications_path, class: "govuk-link--inverse"%> +

+
+
+
diff --git a/app/views/notifications/show.html.erb b/app/views/notifications/show.html.erb new file mode 100644 index 000000000..a9034531f --- /dev/null +++ b/app/views/notifications/show.html.erb @@ -0,0 +1,15 @@ +<% content_for :title, "Notification" %> +<% content_for :before_content do %> + <%= govuk_back_link(href: :back) %> +<% end %> + +
+
+

<%= @notification.title %>

+ <%= @notification.page_content %> +
+
+
+
+ <%= govuk_button_link_to "Back to Home", root_path %> +
diff --git a/config/routes.rb b/config/routes.rb index 4339892dd..06cb4f8d3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -128,7 +128,7 @@ Rails.application.routes.draw do end end - resources :notifications do + resource :notifications do get "dismiss", to: "notifications#dismiss" end