Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/consentManagement/createConsentManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -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 = defaultLocalStorageKeyPrefix
} = params;

const finalities = getFinalitiesFromFinalityDescription({
"finalityDescription":
Expand Down
4 changes: 2 additions & 2 deletions stories/ConsentManagement.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -284,7 +284,7 @@ function Story() {
<Button
onClick={() => {
Object.keys(localStorage)
.filter(key => key.startsWith(localStorageKeyPrefix))
.filter(key => key.startsWith(defaultLocalStorageKeyPrefix))
.forEach(key => localStorage.removeItem(key));

location.reload();
Expand Down
Loading