Skip to content

Commit

Permalink
reaname rgpt -> consentManagement
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Jun 27, 2023
1 parent f015361 commit 21790bb
Show file tree
Hide file tree
Showing 18 changed files with 352 additions and 174 deletions.
1 change: 1 addition & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const { getHardCodedWeight } = (() => {
//"components",
"components/Header",
"components/Footer",
"components/consentManagement",
"components/Alert",
"components/Tabs",
"components/Stepper",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function createFooterPersonalDataPolicyItem(params: {
if (personalDataPolicyLinkProps === undefined) {
throw new Error(
[
"You should provide a personalDataPolicyLinkProps to createGdprApi if",
"You should provide a personalDataPolicyLinkProps to createConsentManagement if",
"you want to add a link to the personal data policy in the footer"
].join(" ")
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./ConsentBannerAndConsentManagement";
export { addConsentManagementTranslations } from "./translation";
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from "react";
import { createComponentI18nApi } from "../../i18n";
import { getLink, type RegisteredLinkProps } from "../../link";

export const { useTranslation, addGdprTranslations } = createComponentI18nApi({
"componentName": "Gdpr",
export const { useTranslation, addConsentManagementTranslations } = createComponentI18nApi({
"componentName": "ConsentManagement",
"frMessages": {
/** cspell: disable */
"all services pref": "Préférences pour tous les services.",
Expand Down Expand Up @@ -70,7 +70,7 @@ export const { useTranslation, addGdprTranslations } = createComponentI18nApi({
}
});

addGdprTranslations({
addConsentManagementTranslations({
"lang": "en",
"messages": {
"all services pref": "Preferences for all services.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { useReducer, useEffect, type ReactNode } from "react";
import type { ExtractFinalityFromFinalityDescription } from "./types";
import type { RegisteredLinkProps } from "../link";
import { createUseGdpr } from "./useGdpr";
import { createProcessConsentChanges, type GdprConsentCallback } from "./processConsentChanges";
import { createUseConsent } from "./useConsent";
import { createProcessConsentChanges, type ConsentCallback } from "./processConsentChanges";
import { createStatefulObservable } from "../tools/StatefulObservable";
import type { FinalityConsent } from "./types";
import { useRerenderOnChange } from "../tools/StatefulObservable/hooks";
import { createConsentBannerAndConsentManagement } from "./ConsentBannerAndConsentManagement";
import { isBrowser } from "../tools/isBrowser";

export const localStorageKey = "@codegouvfr/react-dsfr gdpr finalityConsent";
export const localStorageKey = "@codegouvfr/react-dsfr consent management finalityConsent";

export function createGdprApi<
export function createConsentManagement<
FinalityDescription extends Record<
string,
{ title: ReactNode; description?: ReactNode; subFinalities?: Record<string, ReactNode> }
>
>(params: {
finalityDescription: ((params: { lang: string }) => FinalityDescription) | FinalityDescription;
consentCallback?: GdprConsentCallback<
ExtractFinalityFromFinalityDescription<FinalityDescription>
>;
consentCallback?: ConsentCallback<ExtractFinalityFromFinalityDescription<FinalityDescription>>;
/** Optional: If you have a dedicated page that provides comprehensive information about your website's GDPR policies. */
personalDataPolicyLinkProps?: RegisteredLinkProps;
}) {
Expand Down Expand Up @@ -76,7 +74,7 @@ export function createGdprApi<
return $finalityConsent.current;
}

const { useGdpr } = createUseGdpr({
const { useConsent } = createUseConsent({
useFinalityConsent,
processConsentChanges,
useConsentCallback
Expand All @@ -95,7 +93,7 @@ export function createGdprApi<
});

return {
useGdpr,
useConsent,
ConsentBannerAndConsentManagement,
FooterConsentManagementItem,
FooterPersonalDataPolicyItem
Expand Down
2 changes: 2 additions & 0 deletions src/consentManagement/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createConsentManagement } from "./createConsentManagement";
export { addConsentManagementTranslations } from "./ConsentBannerAndConsentManagement";
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { assert } from "tsafe/assert";
import { is } from "tsafe/is";
import { useConstCallback } from "../tools/powerhooks/useConstCallback";

export type GdprConsentCallback<Finality extends string> = (params: {
export type ConsentCallback<Finality extends string> = (params: {
finalityConsent: FinalityConsent<Finality>;
finalityConsent_prev: FinalityConsent<Finality> | undefined;
}) => Promise<void> | void;
Expand Down Expand Up @@ -61,22 +61,22 @@ export function createProcessConsentChanges<Finality extends string>(params: {
finalityConsent: FinalityConsent<Finality>;
prAllConsentCallbacksRun: Promise<void>;
}) => void;
consentCallback: GdprConsentCallback<Finality> | undefined;
consentCallback: ConsentCallback<Finality> | undefined;
}) {
const { finalities, getFinalityConsent, setFinalityConsent, consentCallback } = params;

const consentCallbacks: GdprConsentCallback<Finality>[] = [];
const consentCallbacks: ConsentCallback<Finality>[] = [];

if (consentCallback !== undefined) {
consentCallbacks.push(consentCallback);
}

function useConsentCallback(params: {
consentCallback: GdprConsentCallback<Finality> | undefined;
consentCallback: ConsentCallback<Finality> | undefined;
}) {
const { consentCallback } = params;

const onConsentChange_const = useConstCallback<GdprConsentCallback<Finality>>(params =>
const onConsentChange_const = useConstCallback<ConsentCallback<Finality>>(params =>
consentCallback?.(params)
);

Expand Down
2 changes: 1 addition & 1 deletion src/gdpr/types.ts → src/consentManagement/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { ReactNode } from "react";

//See: test/types/gdpr.ts to understand theses types
//See: test/types/consentManagement.ts to understand theses types

export type FinalityConsent<Finality extends string> = {
readonly [K in Finality as K extends `${infer _P}.${infer _C}` ? never : K]: boolean;
Expand Down
20 changes: 10 additions & 10 deletions src/gdpr/useGdpr.ts → src/consentManagement/useConsent.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { isBrowser } from "../tools/isBrowser";
import { useConstCallback } from "../tools/powerhooks/useConstCallback";
import type { FinalityConsent } from "./types";
import type { GdprConsentCallback, ProcessConsentChanges } from "./processConsentChanges";
import type { ConsentCallback, ProcessConsentChanges } from "./processConsentChanges";

export type UseGdpr<Finality extends string> = (params?: {
consentCallback: GdprConsentCallback<Finality>;
export type UseConsent<Finality extends string> = (params?: {
consentCallback: ConsentCallback<Finality>;
}) => {
finalityConsent: FinalityConsent<Finality> | undefined;
assumeConsent: (finality: Finality) => void;
};

export function createUseGdpr<Finality extends string>(params: {
export function createUseConsent<Finality extends string>(params: {
useFinalityConsent: () => FinalityConsent<Finality> | undefined;
processConsentChanges: ProcessConsentChanges<Finality>;
useConsentCallback: (params: {
consentCallback: GdprConsentCallback<Finality> | undefined;
consentCallback: ConsentCallback<Finality> | undefined;
}) => void;
}): { useGdpr: UseGdpr<Finality> } {
}): { useConsent: UseConsent<Finality> } {
const { useFinalityConsent, processConsentChanges, useConsentCallback } = params;

const useGdprClientSide: UseGdpr<Finality> = params => {
const useConsentManagementClientSide: UseConsent<Finality> = params => {
const { consentCallback } = params ?? {};

useConsentCallback({ consentCallback });
Expand All @@ -40,7 +40,7 @@ export function createUseGdpr<Finality extends string>(params: {
};
};

const useGdprServerSide: UseGdpr<Finality> = () => {
const useConsentManagementServerSide: UseConsent<Finality> = () => {
return {
"finalityConsent": undefined,
"assumeConsent": () => {
Expand All @@ -49,7 +49,7 @@ export function createUseGdpr<Finality extends string>(params: {
};
};

const useGdpr = isBrowser ? useGdprClientSide : useGdprServerSide;
const useConsent = isBrowser ? useConsentManagementClientSide : useConsentManagementServerSide;

return { useGdpr };
return { useConsent };
}
2 changes: 0 additions & 2 deletions src/gdpr/ConsentBannerAndConsentManagement/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/gdpr/index.ts

This file was deleted.

131 changes: 0 additions & 131 deletions stories/ConsentBanner.stories.tsx

This file was deleted.

Loading

0 comments on commit 21790bb

Please sign in to comment.