-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sessionStorage and cookies, some refactoring
- Loading branch information
Showing
3 changed files
with
143 additions
and
89 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,141 @@ | ||
import { storage, getCookie, setCookie } from '@guardian/libs'; | ||
import { onConsent } from '.'; | ||
|
||
export const UseCaseOptions = [ | ||
"Targeted advertising", | ||
"Essential" | ||
] as const; | ||
export type UseCases = typeof UseCaseOptions[number]; | ||
|
||
const hasConsentForUseCase = async (useCase: UseCases): Promise<boolean> => | ||
{ | ||
/*console.log(`consentState.tcfv2?.consents['1']: ${consentState.tcfv2?.consents['1']}`); | ||
console.log(`consentState.tcfv2?.consents['2']: ${consentState.tcfv2?.consents['2']}`); | ||
console.log(`consentState.tcfv2?.consents['3']: ${consentState.tcfv2?.consents['3']}`); | ||
console.log(`consentState.tcfv2?.consents['4']: ${consentState.tcfv2?.consents['4']}`); | ||
console.log(`consentState.tcfv2?.consents['5']: ${consentState.tcfv2?.consents['5']}`); | ||
console.log(`consentState.tcfv2?.consents['6']: ${consentState.tcfv2?.consents['6']}`); | ||
console.log(`consentState.tcfv2?.consents['7']: ${consentState.tcfv2?.consents['7']}`); | ||
console.log(`consentState.tcfv2?.consents['8']: ${consentState.tcfv2?.consents['8']}`); | ||
console.log(`consentState.tcfv2?.consents['9']: ${consentState.tcfv2?.consents['9']}`); | ||
console.log(`consentState.tcfv2?.consents['10']: ${consentState.tcfv2?.consents['10']}`); | ||
console.log(`consentState.tcfv2?.consents['11']: ${consentState.tcfv2?.consents['11']}`); | ||
console.log(`consentState.canTarget: ${consentState.canTarget}`); | ||
*/ | ||
|
||
const consentState = await onConsent(); | ||
|
||
switch(useCase) { | ||
case "Targeted advertising": return(consentState.canTarget) | ||
//could be more granular than this, for example by using explicit tcf purposes: | ||
//(consentState.tcfv2?.consents['1'] | ||
// && consentState.tcfv2.consents['2'] | ||
// && consentState.tcfv2.consents['3'])//Need the correct list of consents, this is just an example | ||
//|| (!consentState.ccpa?.doNotSell) | ||
//|| (consentState.aus?.personalisedAdvertising) | ||
case "Essential": return(true) | ||
default: return(false) | ||
} | ||
|
||
} | ||
|
||
export const cmpGetLocalStorageItem = async (useCase: UseCases, storageItem: string): Promise<unknown> => | ||
{ | ||
console.log('in cmpGetLocalStorageItem'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
return storage.local.get(storageItem) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot get local storage item ${storageItem} due to missing consent for use-case ${useCase}`) | ||
return(null) | ||
} | ||
}; | ||
|
||
export const cmpSetLocalStorageItem = async (useCase: UseCases, storageItem: string, value:unknown, expires?: string | number | Date): Promise<void> => | ||
{ | ||
console.log('in cmpSetLocalStorageItem'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
return storage.local.set(storageItem, expires) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot set local storage item ${storageItem} due to missing consent for use-case ${useCase}`) | ||
} | ||
}; | ||
|
||
export const cmpGetSessionStorageItem = async (useCase: UseCases, storageItem: string): Promise<unknown> => | ||
{ | ||
console.log('in cmpGetSessionStorageItem'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
return storage.session.get(storageItem) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot get session storage item ${storageItem} due to missing consent for use-case ${useCase}`) | ||
return(null) | ||
} | ||
}; | ||
|
||
export const cmpSetSessionStorageItem = async (useCase: UseCases, storageItem: string, value:unknown, expires?: string | number | Date): Promise<void> => | ||
{ | ||
console.log('in cmpSetSessionStorageItem'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
return storage.session.set(storageItem, expires) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot set session storage item ${storageItem} due to missing consent for use-case ${useCase}`) | ||
} | ||
}; | ||
|
||
export const cmpGetCookie = async({ useCase, name, shouldMemoize, }: { | ||
useCase: UseCases, | ||
name: string; | ||
shouldMemoize?: boolean | undefined; | ||
}): Promise<string | null> => | ||
{ | ||
console.log('in cmpGetCookie'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
return getCookie({name: name, shouldMemoize: shouldMemoize}) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot get cookie ${name} due to missing consent for use-case ${useCase}`) | ||
return(null) | ||
} | ||
}; | ||
|
||
export const cmpSetCookie = async ({ useCase, name, value, daysToLive, isCrossSubdomain, }: { | ||
useCase: UseCases, | ||
name: string; | ||
value: string; | ||
daysToLive?: number | undefined; | ||
isCrossSubdomain?: boolean | undefined; | ||
}): Promise<void> => | ||
{ | ||
console.log('in cmpSetCookie'); | ||
|
||
if(await hasConsentForUseCase(useCase)) | ||
{ | ||
setCookie({name:name, value:value, daysToLive:daysToLive, isCrossSubdomain:isCrossSubdomain}) | ||
} | ||
else | ||
{ | ||
console.error('cmp', `Cannot set cookie ${name} due to missing consent for use-case ${useCase}`) | ||
} | ||
}; | ||
|
||
|
||
//await cmpGetLocalStorageItem("Targeted advertising", "dep") | ||
//await cmpGetLocalStorageItem("invalid", "dep") |
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
55bf627
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report
Test suite run success
328 tests passing in 16 suites.
Report generated by 🧪jest coverage report action from 55bf627