Skip to content

Commit

Permalink
Cookie settings use single consent API
Browse files Browse the repository at this point in the history
- updates the JavaScript for the cookie settings page to use the single consent API code
- stores an internal variable to check whether or not to use the consent API
- if false, behaviour should be as normal
- if true, should yield setting of consent cookies entirely to the consent API code
- note that this code sends its own callback function for the consent API, which sets the initial form values on page load, this means that any delay in the API call will result in these fields being unfilled until it responds
  • Loading branch information
andysellick committed Feb 8, 2024
1 parent b3461e6 commit ac17b82
Showing 1 changed file with 15 additions and 6 deletions.
Expand Up @@ -12,12 +12,19 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
document.querySelector('form[data-module=cookie-settings]')
.addEventListener('submit', this.$module.submitSettingsForm)

this.setInitialFormValues()
this.$module.useConsentApi = window.GOVUK.singleConsent.useConsentApi()
if (this.$module.useConsentApi) {
window.GOVUK.singleConsent.init(this.setInitialFormValues())
} else {
this.setInitialFormValues()
}
}

CookieSettings.prototype.setInitialFormValues = function () {
if (!window.GOVUK.cookie('cookies_policy')) {
window.GOVUK.setDefaultConsentCookie()
if (!this.$module.useConsentApi) {
window.GOVUK.setDefaultConsentCookie()
}
}

var currentConsentCookie = window.GOVUK.cookie('cookies_policy')
Expand Down Expand Up @@ -57,13 +64,15 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
}
}

window.GOVUK.setConsentCookie(options)
window.GOVUK.setCookie('cookies_preferences_set', true, { days: 365 })
if (this.$module.useConsentApi) {
window.GOVUK.singleConsent.setPreferences(null, options)
} else {
window.GOVUK.setConsentCookie(options)
window.GOVUK.setCookie('cookies_preferences_set', true, { days: 365 })
}

this.fireAnalyticsEvent(options)

this.showConfirmationMessage()

return false
}

Expand Down

0 comments on commit ac17b82

Please sign in to comment.