You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.5 KiB
58 lines
1.5 KiB
2 years ago
|
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 window.FormData(cookieFormEl)
|
||
|
body.append('cookies_form[accept_analytics_cookies]', e.target.value)
|
||
|
|
||
|
fetch(cookieFormEl.action, {
|
||
|
method: 'PUT',
|
||
|
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')
|
||
|
})
|
||
|
|
||
|
const gaSrc = window.analyticsScript
|
||
|
if (e.target.value === 'on' && gaSrc) {
|
||
|
const scriptEl = document.createElement('script')
|
||
|
scriptEl.src = gaSrc
|
||
|
document.body.appendChild(scriptEl)
|
||
|
}
|
||
|
|
||
|
e.preventDefault()
|
||
|
})
|
||
|
|
||
|
const hideBannerEl = document.querySelector('.js-hide-cookie-banner')
|
||
|
hideBannerEl.addEventListener('click', (e) => {
|
||
|
e.preventDefault()
|
||
|
|
||
|
cookieBannerEl.setAttribute('hidden', '')
|
||
|
})
|
||
|
}
|