ccs-digitalmarketplace-govuk-frontend
Version:
Digital Marketplace GOV.UK Frontend contains the code you need to start building a user interface for Digital Marketplace.
135 lines (109 loc) • 3.66 kB
JavaScript
import Cookies from 'js-cookie'
// used by the cookie banner component
const DEFAULT_COOKIE_CONSENT = {
analytics: false
}
const COOKIE_CATEGORIES = {
_ga: 'analytics',
_gid: 'analytics',
_gat_govuk_shared: 'analytics'
}
const DOMAINS = [
'.digitalmarketplace.service.gov.uk',
'www.applytosupply.digitalmarketplace.service.gov.uk'
]
const cookiesAPI = Cookies.withAttributes({ path: '/' })
const COOKIE_SETTINGS_COOKIE_NAME = 'dm_cookies_policy'
export function getConsentCookie () {
const consentCookie = cookiesAPI.get(COOKIE_SETTINGS_COOKIE_NAME)
let consentCookieObj
if (consentCookie) {
try {
consentCookieObj = JSON.parse(consentCookie)
} catch (err) {
return null
}
if (typeof consentCookieObj !== 'object' && consentCookieObj !== null) {
consentCookieObj = JSON.parse(consentCookieObj)
}
} else {
return null
}
return consentCookieObj
}
export function setConsentCookie (options) {
let cookieConsent = getConsentCookie()
if (!cookieConsent) {
cookieConsent = JSON.parse(JSON.stringify(DEFAULT_COOKIE_CONSENT))
}
for (const cookieType in options) {
cookieConsent[cookieType] = options[cookieType]
// Delete cookies of that type if consent being set to false
if (!options[cookieType]) {
const cookiePrefixes = []
Object.entries(COOKIE_CATEGORIES).forEach(([cookieName, cookieCategory]) => {
if (cookieCategory === cookieType) {
cookiePrefixes.push(cookieName)
}
})
const cookieList = Object.keys(cookiesAPI.get()).filter((cookieName) => cookiePrefixes.some((cookiePrefix) => cookieName.startsWith(cookiePrefix)))
for (const cookie of cookieList) {
DOMAINS.forEach((domain) => cookiesAPI.remove(cookie, { domain: domain }))
cookiesAPI.remove(cookie, { domain: window.location.hostname.replace(/^www\./, '.') })
}
}
}
setCookie(COOKIE_SETTINGS_COOKIE_NAME, JSON.stringify(cookieConsent), { days: 365 })
}
function checkConsentCookieCategory (cookieName, cookieCategory) {
let currentConsentCookie = getConsentCookie()
// If the consent cookie doesn't exist, but the cookie is in our known list, return true
if (!currentConsentCookie && COOKIE_CATEGORIES[cookieName]) {
return true
}
currentConsentCookie = getConsentCookie()
// Sometimes currentConsentCookie is malformed in some of the tests, so we need to handle these
try {
return currentConsentCookie[cookieCategory]
} catch (e) {
console.error(e)
return false
}
}
function checkConsentCookie (cookieName, cookieValue) {
// If we're setting the consent cookie OR deleting a cookie, allow by default
if (cookieName === COOKIE_SETTINGS_COOKIE_NAME || (cookieValue === null || cookieValue === false)) {
return true
}
if (COOKIE_CATEGORIES[cookieName]) {
const cookieCategory = COOKIE_CATEGORIES[cookieName]
return checkConsentCookieCategory(cookieName, cookieCategory)
} else {
// Deny the cookie if it is not known to us
return false
}
}
// Usage :
// Setting a cookie:
// Cookie('hobnob', 'tasty', { days: 30 })
export function setCookie (name, value, options) {
if (checkConsentCookie(name, value)) {
const cookieOptions = {}
if (typeof options === 'undefined') {
options = {}
}
if (options.days) {
const date = new Date()
date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000))
cookieOptions.expires = date
}
if (document.location.protocol === 'https:') {
cookieOptions.secure = true
}
cookiesAPI.set(
name,
value,
cookieOptions
)
}
}