-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: customize localStorage key for analytics consents #302 #310
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,7 +9,7 @@ import { useRerenderOnChange } from "../tools/StatefulObservable/hooks"; | |||||||||
import { createConsentBannerAndConsentManagement } from "./ConsentBannerAndConsentManagement"; | ||||||||||
import { isBrowser } from "../tools/isBrowser"; | ||||||||||
|
||||||||||
export const localStorageKeyPrefix = "@codegouvfr/react-dsfr finalityConsent"; | ||||||||||
export const defaultLocalStorageKeyPrefix = "@codegouvfr/react-dsfr finalityConsent"; | ||||||||||
|
||||||||||
export function createConsentManagement< | ||||||||||
FinalityDescription extends Record< | ||||||||||
|
@@ -21,10 +21,16 @@ export function createConsentManagement< | |||||||||
consentCallback?: ConsentCallback<ExtractFinalityFromFinalityDescription<FinalityDescription>>; | ||||||||||
/** Optional: If you have a dedicated page that provides comprehensive information about your website's GDPR policies. */ | ||||||||||
personalDataPolicyLinkProps?: RegisteredLinkProps; | ||||||||||
localStorageKeyPrefix?: string; | ||||||||||
}) { | ||||||||||
type Finality = ExtractFinalityFromFinalityDescription<FinalityDescription>; | ||||||||||
|
||||||||||
const { finalityDescription, personalDataPolicyLinkProps, consentCallback } = params; | ||||||||||
const { | ||||||||||
finalityDescription, | ||||||||||
personalDataPolicyLinkProps, | ||||||||||
consentCallback, | ||||||||||
localStorageKeyPrefix | ||||||||||
} = params; | ||||||||||
|
||||||||||
const finalities = getFinalitiesFromFinalityDescription({ | ||||||||||
"finalityDescription": | ||||||||||
|
@@ -33,7 +39,9 @@ export function createConsentManagement< | |||||||||
: finalityDescription | ||||||||||
}); | ||||||||||
|
||||||||||
const localStorageKey = `${localStorageKeyPrefix} ${finalities.join("-")}`; | ||||||||||
const localStorageKey = `${ | ||||||||||
localStorageKeyPrefix ?? defaultLocalStorageKeyPrefix | ||||||||||
} ${finalities.join("-")}`; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
const $finalityConsent = createStatefulObservable<FinalityConsent<Finality> | undefined>(() => { | ||||||||||
if (!isBrowser) { | ||||||||||
|
@@ -98,7 +106,8 @@ export function createConsentManagement< | |||||||||
useConsent, | ||||||||||
ConsentBannerAndConsentManagement, | ||||||||||
FooterConsentManagementItem, | ||||||||||
FooterPersonalDataPolicyItem | ||||||||||
FooterPersonalDataPolicyItem, | ||||||||||
consentLocalStorageKey: localStorageKey | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Will you actually use this value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah so I see, you exported it for Storybook. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok after thinking, the use case we have won't be covered by just returning the computed key. So, I removed it from the return. |
||||||||||
}; | ||||||||||
} | ||||||||||
|
||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import React from "react"; | |
import { sectionName } from "./sectionName"; | ||
import { getStoryFactory } from "./getStory"; | ||
import { createConsentManagement } from "../dist/consentManagement"; | ||
import { localStorageKeyPrefix } from "../dist/consentManagement/createConsentManagement"; | ||
import { defaultLocalStorageKeyPrefix } from "../dist/consentManagement/createConsentManagement"; | ||
import { Placeholder } from "../dist/consentManagement/Placeholder"; | ||
import { Footer } from "../dist/Footer"; | ||
import { Button } from "../dist/Button"; | ||
|
@@ -34,7 +34,8 @@ export const { | |
ConsentBannerAndConsentManagement, | ||
FooterConsentManagementItem, | ||
FooterPersonalDataPolicyItem, | ||
useConsent | ||
useConsent, | ||
consentLocalStorageKey | ||
} = createConsentManagement({ | ||
/* | ||
Can be an object or a function that take the current language as argument. | ||
|
@@ -121,7 +122,13 @@ export const { | |
} | ||
*/ | ||
|
||
} | ||
}, | ||
|
||
/* | ||
This optional parameter let's you personalise the key that is used to store user's consents in the localStorage. | ||
The default value is "${defaultLocalStorageKeyPrefix}" | ||
*/ | ||
"localStorageKeyPrefix": "company-name/app-name" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to keep this undocumented. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I removed all mentions in the documentation. |
||
}); | ||
\`\`\` | ||
|
||
|
@@ -213,7 +220,8 @@ const { | |
ConsentBannerAndConsentManagement, | ||
FooterConsentManagementItem, | ||
FooterPersonalDataPolicyItem, | ||
useConsent | ||
useConsent, | ||
consentLocalStorageKey | ||
} = createConsentManagement({ | ||
"finalityDescription": { | ||
"advertising": { | ||
|
@@ -284,7 +292,7 @@ function Story() { | |
<Button | ||
onClick={() => { | ||
Object.keys(localStorage) | ||
.filter(key => key.startsWith(localStorageKeyPrefix)) | ||
.filter(key => key.startsWith(consentLocalStorageKey)) | ||
.forEach(key => localStorage.removeItem(key)); | ||
|
||
location.reload(); | ||
|
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.