From ac17b821ff068860dbfa998310179fdd7f9dcac7 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 7 Feb 2024 11:22:52 +0000 Subject: [PATCH] Cookie settings use single consent API - 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 --- .../lib/cookie-settings.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/govuk_publishing_components/lib/cookie-settings.js b/app/assets/javascripts/govuk_publishing_components/lib/cookie-settings.js index 2a4a29c362..9a28e153d5 100644 --- a/app/assets/javascripts/govuk_publishing_components/lib/cookie-settings.js +++ b/app/assets/javascripts/govuk_publishing_components/lib/cookie-settings.js @@ -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') @@ -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 }