Sha256: 0486e2dfd7f8f2329cf35969f7096f8670e80835817516a21a435d97a7bcf07c

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

module GovukPublishingComponents
  module Presenters
    class SinglePageNotificationButtonHelper
      attr_reader :already_subscribed, :data_attributes, :base_path, :js_enhancement, :button_type, :button_location

      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"
      end

      def data
        @data_attributes[:track_label] = base_path
        # data-action for tracking should have the format of e.g. "Unsubscribe-button-top", or "Subscribe-button-bottom"
        # when button_location is not present data-action will fall back to "Unsubscribe-button"/"Subscribe-button"
        @data_attributes[:track_action] = [button_type, "button", button_location].compact.join("-")
        @data_attributes[:module] = "single-page-notification-button" if js_enhancement
        @data_attributes[:track_category] = "Single-page-notification-button"
        # 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
      end

      def button_location_is_valid?
        %w[bottom top].include? @local_assigns[:button_location]
      end

      def button_text
        @already_subscribed ? I18n.t("components.single_page_notification_button.unsubscribe_text") : I18n.t("components.single_page_notification_button.subscribe_text")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
govuk_publishing_components-27.17.0 lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb
govuk_publishing_components-27.16.0 lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb
govuk_publishing_components-27.15.0 lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb
govuk_publishing_components-27.14.2 lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb