Commit b48221bc authored by Nicolas Dular's avatar Nicolas Dular Committed by Fatih Acet

Add selection for broadcast notification type

We separate between banner and notification broadcast messages. For both
types we render a preview when creating or editing them, although
notifications don't have different colors.
parent bc0af2f3
...@@ -6,21 +6,31 @@ import { __ } from '~/locale'; ...@@ -6,21 +6,31 @@ import { __ } from '~/locale';
import { textColorForBackground } from '~/lib/utils/color_utils'; import { textColorForBackground } from '~/lib/utils/color_utils';
export default () => { export default () => {
const $broadcastMessageColor = $('input#broadcast_message_color'); const $broadcastMessageColor = $('.js-broadcast-message-color');
const $broadcastMessagePreview = $('div.broadcast-message-preview'); const $broadcastMessageType = $('.js-broadcast-message-type');
const $broadcastBannerMessagePreview = $('.js-broadcast-banner-message-preview');
const $broadcastMessage = $('.js-broadcast-message-message');
const previewPath = $broadcastMessage.data('previewPath');
const $jsBroadcastMessagePreview = $('.js-broadcast-message-preview');
$broadcastMessageColor.on('input', function onMessageColorInput() { $broadcastMessageColor.on('input', function onMessageColorInput() {
const previewColor = $(this).val(); const previewColor = $(this).val();
$broadcastMessagePreview.css('background-color', previewColor); $broadcastBannerMessagePreview.css('background-color', previewColor);
}); });
$('input#broadcast_message_font').on('input', function onMessageFontInput() { $('input#broadcast_message_font').on('input', function onMessageFontInput() {
const previewColor = $(this).val(); const previewColor = $(this).val();
$broadcastMessagePreview.css('color', previewColor); $broadcastBannerMessagePreview.css('color', previewColor);
}); });
const $broadcastMessage = $('textarea#broadcast_message_message'); $broadcastMessageType.on('change', () => {
const previewPath = $broadcastMessage.data('previewPath'); const $broadcastMessageColorFormGroup = $('.js-broadcast-message-background-color-form-group');
const $jsBroadcastMessagePreview = $('.js-broadcast-message-preview'); const $broadcastNotificationMessagePreview = $('.js-broadcast-notification-message-preview');
$broadcastMessageColorFormGroup.toggleClass('hidden');
$broadcastBannerMessagePreview.toggleClass('hidden');
$broadcastNotificationMessagePreview.toggleClass('hidden');
});
$broadcastMessage.on( $broadcastMessage.on(
'input', 'input',
...@@ -58,7 +68,7 @@ export default () => { ...@@ -58,7 +68,7 @@ export default () => {
$('.label-color-preview').css(selectedColorStyle); $('.label-color-preview').css(selectedColorStyle);
return $broadcastMessagePreview.css(selectedColorStyle); return $jsBroadcastMessagePreview.css(selectedColorStyle);
}; };
const setSuggestedColor = e => { const setSuggestedColor = e => {
...@@ -67,7 +77,10 @@ export default () => { ...@@ -67,7 +77,10 @@ export default () => {
.val(color) .val(color)
// Notify the form, that color has changed // Notify the form, that color has changed
.trigger('input'); .trigger('input');
updateColorPreview(); // Only banner supports colors
if ($broadcastMessageType === 'banner') {
updateColorPreview();
}
return e.preventDefault(); return e.preventDefault();
}; };
......
.broadcast-message { .broadcast-message {
@extend .alert-warning; padding: $gl-padding-8;
padding: 10px;
text-align: center;
div, div,
p { p {
...@@ -15,9 +13,29 @@ ...@@ -15,9 +13,29 @@
} }
} }
.broadcast-message-preview { .broadcast-banner-message {
@extend .broadcast-message;
@extend .alert-warning;
text-align: center;
}
.broadcast-notification-message {
@extend .broadcast-message; @extend .broadcast-message;
margin-bottom: 20px;
position: fixed;
bottom: $gl-padding;
right: $gl-padding;
max-width: 300px;
width: auto;
background: $white-light;
border: 1px solid $gray-200;
box-shadow: 0 1px 2px 0 rgba($black, 0.1);
border-radius: $border-radius-default;
z-index: 999;
&.preview {
position: static;
}
} }
.toggle-colors { .toggle-colors {
......
# frozen_string_literal: true # frozen_string_literal: true
module BroadcastMessagesHelper module BroadcastMessagesHelper
def current_broadcast_messages def current_broadcast_banner_messages
BroadcastMessage.current(request.path) BroadcastMessage.current_banner_messages(request.path)
end end
def broadcast_message(message) def current_broadcast_notification_message
BroadcastMessage.current_notification_messages(request.path).last
end
def broadcast_message(message, opts = {})
return unless message.present? return unless message.present?
content_tag :div, dir: 'auto', class: 'broadcast-message', style: broadcast_message_style(message) do classes = "broadcast-#{message.broadcast_type}-message #{opts[:preview] && 'preview'}"
sprite_icon('bullhorn', size: 16, css_class: 'vertical-align-text-top mr-2') << ' ' << render_broadcast_message(message)
content_tag :div, dir: 'auto', class: classes, style: broadcast_message_style(message) do
concat sprite_icon('bullhorn', size: 16, css_class: 'vertical-align-text-top')
concat ' '
concat render_broadcast_message(message)
end end
end end
def broadcast_message_style(broadcast_message) def broadcast_message_style(broadcast_message)
return '' if broadcast_message.notification?
style = [] style = []
if broadcast_message.color.present? if broadcast_message.color.present?
...@@ -40,4 +50,8 @@ module BroadcastMessagesHelper ...@@ -40,4 +50,8 @@ module BroadcastMessagesHelper
def render_broadcast_message(broadcast_message) def render_broadcast_message(broadcast_message)
Banzai.render_field(broadcast_message, :message).html_safe Banzai.render_field(broadcast_message, :message).html_safe
end end
def broadcast_type_options
BroadcastMessage.broadcast_types.keys.map { |w| [w.humanize, w] }
end
end end
.broadcast-message-preview{ style: broadcast_message_style(@broadcast_message) } .broadcast-banner-message.js-broadcast-banner-message-preview.mt-2{ style: broadcast_message_style(@broadcast_message), class: ('hidden' unless @broadcast_message.banner? ) }
= sprite_icon('bullhorn', size: 16, css_class:'vertical-align-text-top mr-2') = sprite_icon('bullhorn', size: 16, css_class:'vertical-align-text-top')
.js-broadcast-message-preview .js-broadcast-message-preview
- if @broadcast_message.message.present? - if @broadcast_message.message.present?
= render_broadcast_message(@broadcast_message) = render_broadcast_message(@broadcast_message)
- else - else
Your message here Your message here
- if Feature.enabled?(:broadcast_notification_type)
.d-flex.justify-content-center
.broadcast-notification-message.preview.js-broadcast-notification-message-preview.mt-2{ class: ('hidden' unless @broadcast_message.notification? ) }
= sprite_icon('bullhorn', size: 16, css_class:'vertical-align-text-top')
.js-broadcast-message-preview
- if @broadcast_message.message.present?
= render_broadcast_message(@broadcast_message)
- else
Your message here
= form_for [:admin, @broadcast_message], html: { class: 'broadcast-message-form js-quick-submit js-requires-input'} do |f| = form_for [:admin, @broadcast_message], html: { class: 'broadcast-message-form js-quick-submit js-requires-input'} do |f|
= form_errors(@broadcast_message) = form_errors(@broadcast_message)
.form-group.row .form-group.row.mt-4
.col-sm-2.col-form-label .col-sm-2.col-form-label
= f.label :message = f.label :message
.col-sm-10 .col-sm-10
= f.text_area :message, class: "form-control js-autosize", = f.text_area :message, class: "form-control js-autosize js-broadcast-message-message",
required: true, required: true,
dir: 'auto', dir: 'auto',
data: { preview_path: preview_admin_broadcast_messages_path } data: { preview_path: preview_admin_broadcast_messages_path }
.form-group.row - if Feature.enabled?(:broadcast_notification_type)
.form-group.row
.col-sm-2.col-form-label
= f.label :broadcast_type, _('Type')
.col-sm-10
= f.select :broadcast_type, broadcast_type_options, {}, class: 'form-control js-broadcast-message-type'
.form-group.row.js-broadcast-message-background-color-form-group{ class: ('hidden' unless @broadcast_message.banner? ) }
.col-sm-2.col-form-label .col-sm-2.col-form-label
= f.label :color, _("Background color") = f.label :color, _("Background color")
.col-sm-10 .col-sm-10
...@@ -25,7 +40,7 @@ ...@@ -25,7 +40,7 @@
.input-group-prepend .input-group-prepend
.input-group-text.label-color-preview{ :style => 'background-color: ' + @broadcast_message.color + '; color: ' + @broadcast_message.font } .input-group-text.label-color-preview{ :style => 'background-color: ' + @broadcast_message.color + '; color: ' + @broadcast_message.font }
= '&nbsp;'.html_safe = '&nbsp;'.html_safe
= f.text_field :color, class: "form-control" = f.text_field :color, class: "form-control js-broadcast-message-color"
.form-text.text-muted .form-text.text-muted
= _('Choose any color.') = _('Choose any color.')
%br %br
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
%th Starts %th Starts
%th Ends %th Ends
%th Target Path %th Target Path
%th Type
%th &nbsp; %th &nbsp;
%tbody %tbody
- @broadcast_messages.each do |message| - @broadcast_messages.each do |message|
...@@ -27,13 +28,15 @@ ...@@ -27,13 +28,15 @@
%td %td
= broadcast_message_status(message) = broadcast_message_status(message)
%td %td
= broadcast_message(message) = broadcast_message(message, preview: true)
%td %td
= message.starts_at = message.starts_at
%td %td
= message.ends_at = message.ends_at
%td %td
= message.target_path = message.target_path
%td
= message.broadcast_type.capitalize
%td %td
= link_to sprite_icon('pencil-square'), edit_admin_broadcast_message_path(message), title: 'Edit', class: 'btn' = link_to sprite_icon('pencil-square'), edit_admin_broadcast_message_path(message), title: 'Edit', class: 'btn'
= link_to sprite_icon('remove'), admin_broadcast_message_path(message), method: :delete, remote: true, title: 'Remove', class: 'js-remove-tr btn btn-danger' = link_to sprite_icon('remove'), admin_broadcast_message_path(message), method: :delete, remote: true, title: 'Remove', class: 'js-remove-tr btn btn-danger'
......
- current_broadcast_messages&.each do |message| - current_broadcast_banner_messages.each do |message|
= broadcast_message(message) = broadcast_message(message)
- if Feature.enabled?(:broadcast_notification_type)
= broadcast_message(current_broadcast_notification_message)
...@@ -13,7 +13,7 @@ describe 'Admin Broadcast Messages' do ...@@ -13,7 +13,7 @@ describe 'Admin Broadcast Messages' do
expect(page).to have_content 'Migration to new server' expect(page).to have_content 'Migration to new server'
end end
it 'Create a customized broadcast message' do it 'creates a customized broadcast banner message' do
fill_in 'broadcast_message_message', with: 'Application update from **4:00 CST to 5:00 CST**' fill_in 'broadcast_message_message', with: 'Application update from **4:00 CST to 5:00 CST**'
fill_in 'broadcast_message_color', with: '#f2dede' fill_in 'broadcast_message_color', with: '#f2dede'
fill_in 'broadcast_message_target_path', with: '*/user_onboarded' fill_in 'broadcast_message_target_path', with: '*/user_onboarded'
...@@ -28,6 +28,20 @@ describe 'Admin Broadcast Messages' do ...@@ -28,6 +28,20 @@ describe 'Admin Broadcast Messages' do
expect(page).to have_selector %(div[style="background-color: #f2dede; color: #b94a48"]) expect(page).to have_selector %(div[style="background-color: #f2dede; color: #b94a48"])
end end
it 'creates a customized broadcast notification message' do
fill_in 'broadcast_message_message', with: 'Application update from **4:00 CST to 5:00 CST**'
fill_in 'broadcast_message_target_path', with: '*/user_onboarded'
select 'Notification', from: 'broadcast_message_broadcast_type'
select Date.today.next_year.year, from: 'broadcast_message_ends_at_1i'
click_button 'Add broadcast message'
expect(current_path).to eq admin_broadcast_messages_path
expect(page).to have_content 'Application update from 4:00 CST to 5:00 CST'
expect(page).to have_content '*/user_onboarded'
expect(page).to have_content 'Notification'
expect(page).to have_selector 'strong', text: '4:00 CST to 5:00 CST'
end
it 'Edit an existing broadcast message' do it 'Edit an existing broadcast message' do
click_link 'Edit' click_link 'Edit'
fill_in 'broadcast_message_message', with: 'Application update RIGHT NOW' fill_in 'broadcast_message_message', with: 'Application update RIGHT NOW'
...@@ -44,10 +58,20 @@ describe 'Admin Broadcast Messages' do ...@@ -44,10 +58,20 @@ describe 'Admin Broadcast Messages' do
expect(page).not_to have_content 'Migration to new server' expect(page).not_to have_content 'Migration to new server'
end end
it 'Live preview a customized broadcast message', :js do it 'updates a preview of a customized broadcast banner message', :js do
fill_in 'broadcast_message_message', with: "Live **Markdown** previews. :tada:"
page.within('.js-broadcast-banner-message-preview') do
expect(page).to have_selector('strong', text: 'Markdown')
expect(page).to have_emoji('tada')
end
end
it 'updates a preview of a customized broadcast notification message', :js do
fill_in 'broadcast_message_message', with: "Live **Markdown** previews. :tada:" fill_in 'broadcast_message_message', with: "Live **Markdown** previews. :tada:"
select 'Notification', from: 'broadcast_message_broadcast_type'
page.within('.broadcast-message-preview') do page.within('.js-broadcast-notification-message-preview') do
expect(page).to have_selector('strong', text: 'Markdown') expect(page).to have_selector('strong', text: 'Markdown')
expect(page).to have_emoji('tada') expect(page).to have_emoji('tada')
end end
......
...@@ -4,24 +4,22 @@ require 'spec_helper' ...@@ -4,24 +4,22 @@ require 'spec_helper'
describe BroadcastMessagesHelper do describe BroadcastMessagesHelper do
describe 'broadcast_message' do describe 'broadcast_message' do
let(:current_broadcast_message) { BroadcastMessage.new(message: 'Current Message') }
it 'returns nil when no current message' do it 'returns nil when no current message' do
expect(helper.broadcast_message(nil)).to be_nil expect(helper.broadcast_message(nil)).to be_nil
end end
it 'includes the current message' do it 'includes the current message' do
current = BroadcastMessage.new(message: 'Current Message')
allow(helper).to receive(:broadcast_message_style).and_return(nil) allow(helper).to receive(:broadcast_message_style).and_return(nil)
expect(helper.broadcast_message(current)).to include 'Current Message' expect(helper.broadcast_message(current_broadcast_message)).to include 'Current Message'
end end
it 'includes custom style' do it 'includes custom style' do
current = BroadcastMessage.new(message: 'Current Message')
allow(helper).to receive(:broadcast_message_style).and_return('foo') allow(helper).to receive(:broadcast_message_style).and_return('foo')
expect(helper.broadcast_message(current)).to include 'style="foo"' expect(helper.broadcast_message(current_broadcast_message)).to include 'style="foo"'
end end
end end
...@@ -32,12 +30,18 @@ describe BroadcastMessagesHelper do ...@@ -32,12 +30,18 @@ describe BroadcastMessagesHelper do
expect(helper.broadcast_message_style(broadcast_message)).to eq '' expect(helper.broadcast_message_style(broadcast_message)).to eq ''
end end
it 'allows custom style' do it 'allows custom style for banner messages' do
broadcast_message = double(color: '#f2dede', font: '#b94a48') broadcast_message = BroadcastMessage.new(color: '#f2dede', font: '#b94a48', broadcast_type: "banner")
expect(helper.broadcast_message_style(broadcast_message)) expect(helper.broadcast_message_style(broadcast_message))
.to match('background-color: #f2dede; color: #b94a48') .to match('background-color: #f2dede; color: #b94a48')
end end
it 'does not add style for notification messages' do
broadcast_message = BroadcastMessage.new(color: '#f2dede', broadcast_type: "notification")
expect(helper.broadcast_message_style(broadcast_message)).to eq ''
end
end end
describe 'broadcast_message_status' do describe 'broadcast_message_status' do
......
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