Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of single page notification button #3073

Closed
wants to merge 4 commits into from

Conversation

hannako
Copy link
Contributor

@hannako hannako commented Nov 18, 2022

Predominantly the work of @hannalaakso

What

There is a tight coupling between the single page notification button component and the Account API which this PR unpicks. The account API is returning the full button HTML when there is a govuk_account session present. This makes it hard to extend the functionality of the component as any changes would need to be duplicated in account api.

How

This PR modifies the JavaScript that receives the API response to now only update the necessary button attributes instead of expecting to receive the entire button HTML and replacing the whole button. Specifically, the PR:

  • Updates the tracking attribute on the button HTML according to the value of active in the API response (active indicates whether the user has subscribed to the relevant notifications)
  • Appends the value of data-button-location , if it is provided in the markup, to the above tracking attribute value
  • Updates the button text if both data-button-text-unsubscribe and data-button-text-subscribe are provided in the markup attributes
  • Adds a new wrapper class .gem-c-single-page-notication-button__text to more reliably target the button text
  • Adds tests to check whether the tracking attribute, including optional location, and the custom button text are updated correctly upon receiving the API response. Removes some no longer relevant test classes (.old-button-for-test and .new-button-for-test) since the whole button is no longer being replaced.
  • Adds an additional test for checking that the button is rendered visible when the API call completes (the button is initially hidden when JS runs)

NB: this will not be a breaking change, as we are using an attribute that is already being sent in the JSON response.

Why

We currently receive the entire notification button HTML from the Personalisation API to replace the notification button markup rendered on page load - this is done to make the button reflect whether the user has subscribed to notifications for the particular topic or document collection.

This PR is in preparation for some later work to refactor the API. Once this PR has been merged, and we are no longer using the button_location or button_html from the API response, we will remove them from the API and only return the active flag.

Left to do later

Once the API response has been refactored on the backend to not return the entire button HTML, we should also remove passing the button location to the personalisation API in the JS and the corresponding test for it.

Visual changes

None

HTML for subscribe button

Screenshot 2022-11-18 at 14 18 16

HTML for unsubscribe button

Screenshot 2022-11-18 at 14 17 37

Trello https://trello.com/c/qjGJ7b5P/1507-set-tracking-attribute-and-location-for-the-single-page-notification-button-component-based-on-simplified-api-response

We need to update the button text via an ajax call to the account
API. Storing the button text as data attributes makes them available
to the js function that is amended in the next commit.
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 18, 2022 12:28 Inactive
No longer replace the entire single page notification button HTML from the personalisation API response. Instead, check the boolean "active" in the response: if active is true, user has subscribed to notifications. Update the tracking attribute on the button HTML according to the value of active in the API response. Append the value of data-button-location, if it is provided in the markup, to the tracking attribute value. Update the button text if both data-button-text-unsubscribe and data-button-text-subscribe are provided in the markup attributes

Add a new wrapper class .gem-c-single-page-notication-button__text to more reliably target the button text.

Add tests to check whether the tracking attribute, including optional location, and the custom button text are updated correctly upon receiving the API response. Remove some no longer relevant test classes (.old-button-for-test and .new-button-for-test) since the whole button is no longer being replaced. Add an additional test for checking that the button is rendered visible when the API call completes (the button is initially hidden when JS runs)

(cherry picked from commit dd0a733)
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 18, 2022 12:33 Inactive
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 18, 2022 12:59 Inactive
@hannako hannako force-pushed the simplify_button_component branch from dad6351 to 5e2119d Compare November 18, 2022 13:21
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 18, 2022 13:21 Inactive
@hannako hannako force-pushed the simplify_button_component branch from 5e2119d to 2cad684 Compare November 18, 2022 13:23
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 18, 2022 13:24 Inactive
@hannako hannako changed the title Small refactor of single page notification button Refactor of single page notification button Nov 18, 2022
@hannako hannako marked this pull request as ready for review November 18, 2022 14:28
@hannako hannako force-pushed the simplify_button_component branch from 2cad684 to 5a16f0d Compare November 18, 2022 15:14
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 18, 2022 15:14 Inactive
Copy link
Contributor

@AgaDufrat AgaDufrat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Tracking attributes appear to be well tested 👍

@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 21, 2022 12:08 Inactive
@hannako hannako force-pushed the simplify_button_component branch from 8c095e2 to 729ac90 Compare November 21, 2022 12:12
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 21, 2022 12:13 Inactive
@hannako hannako force-pushed the simplify_button_component branch from 729ac90 to d295aed Compare November 21, 2022 13:38
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 21, 2022 13:38 Inactive
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3073 November 21, 2022 18:03 Inactive
@hannalaakso
Copy link
Member

Superseded by #3071

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants