-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: get button from personalisation API
- Loading branch information
1 parent
9abaf54
commit 0daaec7
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...ets/javascripts/govuk_publishing_components/components/single-page-notification-button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* global XMLHttpRequest */ | ||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
function SinglePageNotificationButton ($module) { | ||
this.$module = $module | ||
this.basePath = this.$module.querySelector('[name="base_path"]').value | ||
} | ||
|
||
SinglePageNotificationButton.prototype.init = function () { | ||
var xhr = new XMLHttpRequest() | ||
var url = '/api/personalisation/check-email-subscription?base_path=' + this.basePath | ||
xhr.open('GET', url, true) | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4 && xhr.status === 200) { | ||
var responseText = xhr.responseText | ||
// if response text exists and is JSON parse-able, parse the response and get the button html | ||
if (responseText && this.responseIsJSON(responseText)) { | ||
var newButton = JSON.parse(responseText).button_html | ||
var html = document.createElement('div') | ||
html.innerHTML = newButton | ||
// test that the html returned contains the button component; if yes, swap the button for the updated version | ||
var responseHasButton = html.querySelector('.gem-c-single-page-notification-button') | ||
if (responseHasButton) { | ||
this.$module.outerHTML = newButton | ||
} | ||
} | ||
} | ||
}.bind(this) | ||
xhr.send() | ||
} | ||
|
||
SinglePageNotificationButton.prototype.responseIsJSON = function (string) { | ||
try { | ||
JSON.parse(string) | ||
} catch (e) { | ||
return false | ||
} | ||
return true | ||
} | ||
Modules.SinglePageNotificationButton = SinglePageNotificationButton | ||
})(window.GOVUK.Modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters