Browse Source

feat: add notifications page and remove unread_notification.rb

pull/2131/head
natdeanlewissoftwire 2 years ago
parent
commit
0674f1e4bb
  1. 15
      app/components/unread_notification.html.erb
  2. 12
      app/components/unread_notification.rb
  3. 13
      app/controllers/notifications_controller.rb
  4. 4
      app/frontend/styles/_header.scss
  5. 2
      app/helpers/application_helper.rb
  6. 2
      app/helpers/navigation_items_helper.rb
  7. 8
      app/models/user.rb
  8. 4
      app/views/layouts/application.html.erb
  9. 21
      app/views/notifications/_notification_banner.html.erb
  10. 15
      app/views/notifications/show.html.erb
  11. 2
      config/routes.rb

15
app/components/unread_notification.html.erb

@ -1,15 +0,0 @@
<div class="app-unread-notification">
<div class="govuk-width-container">
<br>
<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters govuk-body">
<p class="govuk-!-font-weight-bold"><%= oldest_unread_notification.title %></p>
<%= govuk_link_to oldest_unread_notification.link_text, notification_path(oldest_unread_notification), class: "govuk-link--inverse govuk-!-font-weight-bold"%>
</div>
<p class="govuk-grid-column-one-quarter govuk-!-text-align-right ">
<%= govuk_link_to "Dismiss", notification_dismiss_path(oldest_unread_notification), class: "govuk-link--inverse"%>
</p>
</div>
<br>
</div>
</div>

12
app/components/unread_notification.rb

@ -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

13
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

4
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;
}

2
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

2
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)

8
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.

4
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 %>

21
app/views/notifications/_notification_banner.html.erb

@ -0,0 +1,21 @@
<div class="app-unread-notification">
<div class="govuk-width-container">
<br>
<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<% if current_user.active_unread_notifications.count > 1 %>
<p>Notification 1 of <%= current_user.active_unread_notifications.count %></p>
<% end %>
<p class="govuk-!-font-weight-bold"><%= current_user.oldest_active_unread_notification.title %></p>
<% if current_user.oldest_active_unread_notification.page_content.present? %>
<div class ="govuk-body">
<%= govuk_link_to current_user.oldest_active_unread_notification.link_text, notifications_path, class: "govuk-link--inverse govuk-!-font-weight-bold"%>
</div>
<% end %>
</div>
<p class="govuk-grid-column-one-quarter govuk-!-text-align-right ">
<%= govuk_link_to "Dismiss", dismiss_notifications_path, class: "govuk-link--inverse"%>
</p>
</div>
</div>
</div>

15
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 %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl"><%= @notification.title %></h1>
<%= @notification.page_content %>
</div>
</div>
<br>
<div>
<%= govuk_button_link_to "Back to Home", root_path %>
</div>

2
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

Loading…
Cancel
Save