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

Set attributes for single page notification button based on Account API response #3071

Merged

Conversation

hannalaakso
Copy link
Member

@hannalaakso hannalaakso commented Nov 16, 2022

Co-authored with @hannako

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)

Why

We currently receive the entire notification button HTML from the Account 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.

A note about testing

Both @hannako and I have tested the button to work correctly by running it locally via goverment-frontend with a GOV.UK Account.

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 Account 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

@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 16, 2022 13:42 Inactive
@hannalaakso hannalaakso changed the title Update notification button HTML based on Personalisation API response Update notification button attributes based on Personalisation API response Nov 16, 2022
@hannalaakso hannalaakso force-pushed the amend_js_for_custom_text_single_page_notification_button branch from ae14c55 to 61b1082 Compare November 17, 2022 16:18
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 17, 2022 16:18 Inactive
@hannalaakso hannalaakso force-pushed the amend_js_for_custom_text_single_page_notification_button branch from 61b1082 to 42e9752 Compare November 17, 2022 17:06
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 17, 2022 17:07 Inactive
@hannalaakso hannalaakso force-pushed the amend_js_for_custom_text_single_page_notification_button branch from 42e9752 to ce4d1a5 Compare November 17, 2022 17:10
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 17, 2022 17:11 Inactive
@hannalaakso hannalaakso force-pushed the amend_js_for_custom_text_single_page_notification_button branch from ce4d1a5 to 2107a03 Compare November 17, 2022 18:22
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 17, 2022 18:23 Inactive
@hannalaakso hannalaakso force-pushed the amend_js_for_custom_text_single_page_notification_button branch from 2107a03 to dd0a733 Compare November 17, 2022 18:40
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 17, 2022 18:40 Inactive
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.
@hannako hannako closed this Nov 18, 2022
@hannalaakso hannalaakso reopened this Nov 22, 2022
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 22, 2022 14:11 Inactive
@hannalaakso hannalaakso force-pushed the amend_js_for_custom_text_single_page_notification_button branch from dd0a733 to 447ddec Compare November 22, 2022 14:32
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 22, 2022 14:32 Inactive
@hannalaakso hannalaakso changed the base branch from custom_text_single_page_notification_button to main November 22, 2022 14:32
@alphagov alphagov deleted a comment from hannako Nov 22, 2022
@hannalaakso hannalaakso force-pushed the amend_js_for_custom_text_single_page_notification_button branch from 447ddec to a3fddf9 Compare November 22, 2022 14:37
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 22, 2022 14:37 Inactive
@hannalaakso hannalaakso marked this pull request as ready for review November 22, 2022 14:45
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)
@hannalaakso hannalaakso force-pushed the amend_js_for_custom_text_single_page_notification_button branch from a3fddf9 to 209c34b Compare November 22, 2022 16:27
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 22, 2022 16:27 Inactive
@hannalaakso hannalaakso changed the title Update notification button attributes based on Personalisation API response Set attributes for single page notification button based on Account API response Nov 22, 2022
@govuk-ci govuk-ci temporarily deployed to components-gem-pr-3071 November 22, 2022 18:05 Inactive
@hannalaakso hannalaakso merged commit da622ce into main Nov 24, 2022
@hannalaakso hannalaakso deleted the amend_js_for_custom_text_single_page_notification_button branch November 24, 2022 10:49
@andysellick andysellick mentioned this pull request Nov 29, 2022
hannako added a commit to alphagov/account-api that referenced this pull request Jan 6, 2023
This API endpoint is only used by the single page notification button component
in the gem [1]. The gem has been updated to expect a different response [2].

1 https://github.com/search?q=org%3Aalphagov+check-email-subscription&type=code
2 alphagov/govuk_publishing_components#3071
hannako added a commit to alphagov/account-api that referenced this pull request Jan 6, 2023
This API endpoint is only used by the single page notification button component
in the gem [1]. The gem has been updated to expect a different response [2].

1 https://github.com/search?q=org%3Aalphagov+check-email-subscription&type=code
2 alphagov/govuk_publishing_components#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.

5 participants