Commit a8b862d3 authored by nicolasdular's avatar nicolasdular

Add ability to dismiss notification messages

Users are capable of hiding notification messages forever because
these messages can overlap on content.
parent bad14e03
import Cookies from 'js-cookie';
const handleOnDismiss = ({ currentTarget }) => {
currentTarget.removeEventListener('click', handleOnDismiss);
const {
dataset: { id },
} = currentTarget;
Cookies.set(`hide_broadcast_notification_message_${id}`, true);
const notification = document.querySelector(`.js-broadcast-notification-${id}`);
notification.parentNode.removeChild(notification);
};
export default () => {
const dismissButton = document.querySelector('.js-dismiss-current-broadcast-notification');
if (dismissButton) {
dismissButton.addEventListener('click', handleOnDismiss);
}
};
...@@ -35,6 +35,7 @@ import initPerformanceBar from './performance_bar'; ...@@ -35,6 +35,7 @@ import initPerformanceBar from './performance_bar';
import initSearchAutocomplete from './search_autocomplete'; import initSearchAutocomplete from './search_autocomplete';
import GlFieldErrors from './gl_field_errors'; import GlFieldErrors from './gl_field_errors';
import initUserPopovers from './user_popovers'; import initUserPopovers from './user_popovers';
import initBroadcastNotifications from './broadcast_notification';
import { initUserTracking } from './tracking'; import { initUserTracking } from './tracking';
import { __ } from './locale'; import { __ } from './locale';
...@@ -105,6 +106,7 @@ function deferredInitialisation() { ...@@ -105,6 +106,7 @@ function deferredInitialisation() {
initUsagePingConsent(); initUsagePingConsent();
initUserPopovers(); initUserPopovers();
initUserTracking(); initUserTracking();
initBroadcastNotifications();
if (document.querySelector('.search')) initSearchAutocomplete(); if (document.querySelector('.search')) initSearchAutocomplete();
......
...@@ -6,13 +6,16 @@ module BroadcastMessagesHelper ...@@ -6,13 +6,16 @@ module BroadcastMessagesHelper
end end
def current_broadcast_notification_message def current_broadcast_notification_message
BroadcastMessage.current_notification_messages(request.path).last not_hidden_messages = BroadcastMessage.current_notification_messages(request.path).select do |message|
cookies["hide_broadcast_notification_message_#{message.id}"].blank?
end
not_hidden_messages.last
end end
def broadcast_message(message, opts = {}) def broadcast_message(message, opts = {})
return unless message.present? return unless message.present?
render 'shared/broadcast_message', { message: message, opts: opts } render "shared/broadcast_message", { message: message, opts: opts }
end end
def broadcast_message_style(broadcast_message) def broadcast_message_style(broadcast_message)
......
- classes = "broadcast-#{message.broadcast_type}-message #{opts[:preview] && 'preview'}" %div{ class: "broadcast-#{message.broadcast_type}-message #{opts[:preview] && 'preview'} js-broadcast-notification-#{message.id} d-flex",
style: broadcast_message_style(message), dir: 'auto' }
%div{classs: classes, style: broadcast_message_style(message), dir: 'auto'} %div
= sprite_icon('bullhorn', size: 16, css_class: 'vertical-align-text-top') = sprite_icon('bullhorn', size: 16, css_class: 'vertical-align-text-top')
= ' ' = render_broadcast_message(message)
= render_broadcast_message(message) - if message.notification? && opts[:preview].blank?
%button.js-dismiss-current-broadcast-notification.btn.btn-link.text-dark.pl-2.pr-2{ 'aria-label' => _('Close'), :type => 'button', data: { id: message.id } }
%i.fa.fa-times
# frozen_string_literal: true
require 'spec_helper'
describe 'Broadcast Messages' do
let!(:broadcast_message) { create(:broadcast_message, broadcast_type: 'notification', message: 'SampleMessage') }
it 'shows broadcast message' do
visit root_path
expect(page).to have_content 'SampleMessage'
end
it 'hides broadcast message after dismiss', :js do
visit root_path
find('.js-dismiss-current-broadcast-notification').click
expect(page).not_to have_content 'SampleMessage'
end
it 'broadcast message is still hidden after refresh', :js do
visit root_path
find('.js-dismiss-current-broadcast-notification').click
visit root_path
expect(page).not_to have_content 'SampleMessage'
end
end
...@@ -3,6 +3,29 @@ ...@@ -3,6 +3,29 @@
require 'spec_helper' require 'spec_helper'
describe BroadcastMessagesHelper do describe BroadcastMessagesHelper do
describe 'current_broadcast_notification_message' do
subject { helper.current_broadcast_notification_message }
context 'with available broadcast notification messages' do
let!(:broadcast_message_1) { create(:broadcast_message, broadcast_type: 'notification', starts_at: Time.now - 1.day) }
let!(:broadcast_message_2) { create(:broadcast_message, broadcast_type: 'notification', starts_at: Time.now) }
it { is_expected.to eq broadcast_message_2 }
context 'when last broadcast message is hidden' do
before do
helper.request.cookies["hide_broadcast_notification_message_#{broadcast_message_2.id}"] = 'true'
end
it { is_expected.to eq broadcast_message_1 }
end
end
context 'without broadcast notification messages' do
it { is_expected.to be_nil }
end
end
describe 'broadcast_message' do describe 'broadcast_message' do
let(:current_broadcast_message) { BroadcastMessage.new(message: 'Current Message') } let(:current_broadcast_message) { BroadcastMessage.new(message: 'Current Message') }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment