Skip to content

Commit

Permalink
Refactor single page notification component
Browse files Browse the repository at this point in the history
- move most of the component attributes (class, data attributes) to be handled by the component wrapper on the parent element
- update the helper and tests to reflect this change
  • Loading branch information
andysellick committed Dec 13, 2024
1 parent e45c502 commit 3e00b03
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
add_gem_component_stylesheet("single-page-notification-button")

local_assigns[:margin_bottom] ||= 3
js_enhancement ||= false

spnb_helper = GovukPublishingComponents::Presenters::SinglePageNotificationButtonHelper.new(local_assigns)
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns)
component_helper.add_class("gem-c-single-page-notification-button")
component_helper.add_class("js-personalisation-enhancement") if local_assigns[:js_enhancement]
component_helper.add_data_attribute({ module: "single-page-notification-button" }) if js_enhancement
component_helper.add_data_attribute({ button_location: spnb_helper.button_location })
component_helper.add_data_attribute({ button_text_subscribe: spnb_helper.button_text_subscribe })
component_helper.add_data_attribute({ button_text_unsubscribe: spnb_helper.button_text_unsubscribe })

ga4_data_attributes = ga4_data_attributes ||= nil
ga4_link_data_attributes = ga4_data_attributes[:ga4_link] if ga4_data_attributes
Expand All @@ -18,7 +24,7 @@
<svg class="gem-c-single-page-notification-button__icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" height="18" width="18" viewBox="0 0 459.334 459.334"><path fill="currentColor" d="M177.216 404.514c-.001.12-.009.239-.009.359 0 30.078 24.383 54.461 54.461 54.461s54.461-24.383 54.461-54.461c0-.12-.008-.239-.009-.359H175.216zM403.549 336.438l-49.015-72.002v-89.83c0-60.581-43.144-111.079-100.381-122.459V24.485C254.152 10.963 243.19 0 229.667 0s-24.485 10.963-24.485 24.485v27.663c-57.237 11.381-100.381 61.879-100.381 122.459v89.83l-49.015 72.002a24.76 24.76 0 0 0 20.468 38.693H383.08a24.761 24.761 0 0 0 20.469-38.694z"/></svg><span class="gem-c-single-page-notication-button__text"><%= spnb_helper.button_text %></span>
<% end %>
<%= tag.div(**component_helper.all_attributes) do %>
<%= tag.form class: spnb_helper.classes, action: spnb_helper.form_action, method: "POST", data: spnb_helper.data do %>
<%= tag.form action: spnb_helper.form_action, method: "POST" do %>
<input type="hidden" name="base_path" value="<%= spnb_helper.base_path %>">
<% if spnb_helper.skip_the_gov_uk_account? %>
<input type="hidden" name="<%= spnb_helper.skip_account_param %>" value="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
module GovukPublishingComponents
module Presenters
class SinglePageNotificationButtonHelper
attr_reader :already_subscribed, :data_attributes, :base_path, :js_enhancement, :button_type, :button_location, :classes, :skip_account
attr_reader :already_subscribed, :data_attributes, :base_path, :button_type, :button_location, :skip_account, :button_text_subscribe, :button_text_unsubscribe

def initialize(local_assigns)
@local_assigns = local_assigns
@data_attributes = @local_assigns[:data_attributes] || {}
@js_enhancement = @local_assigns[:js_enhancement] || false
@already_subscribed = @local_assigns[:already_subscribed] || false
@base_path = @local_assigns[:base_path] || nil
@button_location = button_location_is_valid? ? @local_assigns[:button_location] : nil
@button_type = @local_assigns[:already_subscribed] ? "Unsubscribe" : "Subscribe"
@classes = %w[gem-c-single-page-notification-button]
@classes << "js-personalisation-enhancement" if js_enhancement
@button_text_subscribe = custom_button_text_is_valid? ? custom_subscribe_text : default_subscribe_text
@button_text_unsubscribe = custom_button_text_is_valid? ? custom_unsubscribe_text : default_unsubscribe_text
@skip_account = @local_assigns[:skip_account] || nil
end

def data
@data_attributes[:module] = "single-page-notification-button" if js_enhancement
# This attribute is passed through to the personalisation API to ensure when a new button is returned from the API, it has the same button_location
@data_attributes[:button_location] = button_location
@data_attributes[:button_text_subscribe] = @button_text_subscribe
@data_attributes[:button_text_unsubscribe] = @button_text_unsubscribe
@data_attributes
end

def button_location_is_valid?
%w[bottom top].include? @local_assigns[:button_location]
end
Expand Down
6 changes: 3 additions & 3 deletions spec/components/single_page_notification_button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def component_name

it "renders with the correct markup if base path is present" do
render_component({ base_path: "/the-current-page" })
assert_select "form.gem-c-single-page-notification-button"
assert_select ".gem-c-single-page-notification-button form"
assert_select "input[type='hidden']", value: "/the-current-page"
assert_select ".gem-c-single-page-notification-button button.gem-c-single-page-notification-button__submit[type='submit']"
end
Expand Down Expand Up @@ -90,12 +90,12 @@ def component_name

it "sets a default bottom margin to its wrapper" do
render_component({ base_path: "/the-current-page" })
assert_select 'div.govuk-\!-margin-bottom-3 .gem-c-single-page-notification-button'
assert_select '.gem-c-single-page-notification-button.govuk-\!-margin-bottom-3'
end

it "adds bottom margin to its wrapper if margin_bottom is specified" do
render_component({ base_path: "/the-current-page", margin_bottom: 9 })
assert_select 'div.govuk-\!-margin-bottom-9 .gem-c-single-page-notification-button'
assert_select '.gem-c-single-page-notification-button.govuk-\!-margin-bottom-9'
end

it "has a js-enhancement class and a data-module attribute if the js-enhancement flag is present" do
Expand Down

0 comments on commit 3e00b03

Please sign in to comment.