-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from DFE-Digital/cookie-banner
Cookies banner with javascript
- Loading branch information
Showing
4 changed files
with
116 additions
and
26 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { initAll } from "govuk-frontend"; | ||
|
||
initAll(); | ||
import "./cookie-banner" | ||
|
||
initAll(); |
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,49 @@ | ||
const cookieBannerEl = document.querySelector(".js-cookie-banner"); | ||
if (cookieBannerEl) { | ||
const cookieFormEl = document.querySelector(".js-cookie-form"); | ||
|
||
cookieFormEl.addEventListener("click", (e) => { | ||
if (e.target.tagName !== "BUTTON") { | ||
return; | ||
} | ||
|
||
const body = new FormData(cookieFormEl); | ||
body.append("cookie_preferences[consent]", e.target.value); | ||
|
||
fetch(cookieFormEl.action, { | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
}, | ||
body, | ||
}) | ||
.then((res) => { | ||
if (res.status >= 200 && res.status < 300) { | ||
return res; | ||
} | ||
|
||
throw new Error(res); | ||
}) | ||
.then((res) => res.json()) | ||
.then(({ message }) => { | ||
const messageEl = cookieBannerEl.querySelector(".js-cookie-message"); | ||
messageEl.textContent = message; | ||
|
||
cookieBannerEl | ||
.querySelector(".js-cookie-banner__form") | ||
.setAttribute("hidden", ""); | ||
cookieBannerEl | ||
.querySelector(".js-cookie-banner__success") | ||
.removeAttribute("hidden"); | ||
}); | ||
|
||
e.preventDefault(); | ||
}); | ||
|
||
const hideBannerEl = document.querySelector(".js-hide-cookie-banner"); | ||
hideBannerEl.addEventListener("click", (e) => { | ||
e.preventDefault(); | ||
|
||
cookieBannerEl.setAttribute("hidden", ""); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,12 +1,48 @@ | ||
<% case cookies["consented-to-cookies"] %> | ||
<% when "reject" %> | ||
<% unless cookies["hide-cookies-banner"] == "1" %> | ||
<%= render partial: "shared/cookies/rejected" %> | ||
<% end %> | ||
<% when "accept" %> | ||
<% unless cookies["hide-cookies-banner"] == "1" %> | ||
<%= render partial: "shared/cookies/accepted" %> | ||
<% end %> | ||
<% else %> | ||
<%= render partial: "shared/cookies/not_set" %> | ||
<% if cookies["consented-to-cookies"].nil? %> | ||
<div class="govuk-cookie-banner js-cookie-banner" data-nosnippet role="region" aria-label="Cookies on Teacher School Hub Finder"> | ||
<div class="govuk-cookie-banner__message govuk-width-container js-cookie-banner__form"> | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<h2 class="govuk-cookie-banner__heading govuk-heading-m">Cookies on Teacher School Hub Finder</h2> | ||
|
||
<div class="govuk-cookie-banner__content"> | ||
<p>We use some essential cookies to make this service work.</p> | ||
<p>We’d like to set additional cookies so we can remember your settings, understand how people use the service and make improvements.</p> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<%= form_with model: CookiePreferences.new, scope: "cookie_preferences", url: cookie_preferences_path, class: "js-cookie-form" do |f| %> | ||
<div class="govuk-button-group"> | ||
<%= f.button "Accept additional cookies", name: "cookie_preferences[consent]", value: "accept", class: "govuk-button", data: { module: "govuk-button" } %> | ||
|
||
<%= f.button "Reject additional cookies", name: "cookie_preferences[consent]", value: "reject", class: "govuk-button", data: { module: "govuk-button" } %> | ||
|
||
<%= govuk_link_to "View cookies", pages_cookies_path %> | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
<div class="govuk-cookie-banner__message govuk-width-container js-cookie-banner__success" role="alert" hidden> | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
|
||
<div class="govuk-cookie-banner__content"> | ||
<p class="govuk-body"> | ||
<span class="js-cookie-message"> | ||
You’ve set your cookie preferences. | ||
</span> | ||
You can <%= govuk_link_to "change your cookie settings", pages_cookies_path %> at any time. | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="govuk-button-group"> | ||
<button class="govuk-button js-hide-cookie-banner"> | ||
Hide this message | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
<% end %> |