Skip to content

Commit

Permalink
set _dl_cookie_consent as an object
Browse files Browse the repository at this point in the history
this allows us to set the value, expiration date
and domain on the same cookie.
  • Loading branch information
alishaevn committed Nov 28, 2023
1 parent 3cb2484 commit 45be310
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pages/legal-notices/cookie-policy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CookiePreferencesCheck, Title } from '@scientist-softserv/webstore-component-library'
import { cookieConsent, disableCookies, enableCookies } from '../../utils'
import { cookieConsentValue, disableCookies, enableCookies } from '../../utils'

const CookiePolicy = () => (
<div className='container'>
Expand All @@ -9,7 +9,7 @@ const CookiePolicy = () => (
<p> Please provide your consent below to our use of non-essential cookies on our site.
You may withdraw your consent at any point by following the instructions above or by returning to this page and changing your selection.</p>
<CookiePreferencesCheck
cookieConsent={cookieConsent}
cookieConsentValue={cookieConsentValue}
disableCookies={disableCookies}
enableCookies={enableCookies}
/>
Expand Down
23 changes: 14 additions & 9 deletions utils/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,35 @@ import {
deleteCookie,
getCookie,
getCookies,
hasCookie,
setCookie,
} from 'cookies-next'

// once the feature is ready, uncomment the line below
// export const cookieConsent = getCookie('dl_cookie_consent')
const cookieConsentGiven = hasCookie('_dl_cookie_consent')
export const cookieConsentValue = cookieConsentGiven ? JSON.parse(getCookie('_dl_cookie_consent')).value : true
const cookieConsent = {
expires: new Date().setFullYear(new Date().getFullYear() + 1), // 1 year from now in milliseconds
domain: process.env.NEXT_PUBLIC_PROVIDER_NAME,
}

export const enableCookies = () => {
setCookie('_dl_cookie_consent', 'true', { path: '/' })
setCookie('_cookies_updated_at', new Date(), { path: '/' })
const cookie = JSON.stringify({ value: true, ...cookieConsent })
setCookie('_dl_cookie_consent', cookie, { path: '/' })
// set other cookies
}

export const disableCookies = () => {
const cookie = JSON.stringify({ value: false, ...cookieConsent })
// will account for this in a future pr
// Object.keys(getCookies()).forEach(cookie => deleteCookie(cookie))
setCookie('_dl_cookie_consent', 'false', { path: '/' })
setCookie('_cookies_updated_at', new Date(), { path: '/' })

setCookie('_dl_cookie_consent', cookie, { path: '/' })
}

export const getCookieConsent = () => {
const updatedAt = getCookie('_cookies_updated_at')
const oneYearInMilliseconds = 1000*60*60*24*365
const oneYearLapsed = (new Date() - updatedAt) >= oneYearInMilliseconds
const cookieExpiration = cookieConsentGiven ? JSON.parse(getCookie('_dl_cookie_consent')).expires : undefined

if ((cookieConsent === undefined) || oneYearLapsed) return true
if ((!cookieConsentGiven) || (new Date() > cookieExpiration)) return true
return false
}

0 comments on commit 45be310

Please sign in to comment.