Skip to content

Commit d0f0372

Browse files
committed
Improve naming conventions
1 parent f3b05ff commit d0f0372

File tree

5 files changed

+55
-39
lines changed

5 files changed

+55
-39
lines changed

src/gdpr/ConsentBannerAndConsentManagement.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function createConsentBannerAndConsentManagement<
6464
const [isHydrated, setIsHydrated] = useReducer(() => true, false);
6565

6666
useEffect(() => {
67-
processConsentChanges({ "type": "no changes but trigger callbacks" });
67+
processConsentChanges({ "type": "no changes but trigger consent callbacks" });
6868

6969
setIsHydrated();
7070
}, []);
@@ -245,7 +245,7 @@ function createConsentManagement<
245245
}, [realFinalityConsent]);
246246

247247
const { processConsentChanges } = createProcessConsentChanges({
248-
"callback": undefined,
248+
"consentCallback": undefined,
249249
finalities,
250250
"getFinalityConsent": () => localFinalityConsent,
251251
"setFinalityConsent": ({ finalityConsent }) =>
@@ -269,7 +269,7 @@ function createConsentManagement<
269269

270270
const [isProcessingChanges, setIsProcessingChanges] = useState(false);
271271

272-
const createButtonCallback =
272+
const createOnClick =
273273
(type: "grantAll" | "denyAll" | "apply local changes") => async () => {
274274
setIsProcessingChanges(true);
275275

@@ -310,7 +310,7 @@ function createConsentManagement<
310310
<button
311311
title={t("accept all - title")}
312312
className={fr.cx("fr-btn")}
313-
onClick={createButtonCallback("grantAll")}
313+
onClick={createOnClick("grantAll")}
314314
disabled={isProcessingChanges}
315315
>
316316
{t("accept all")}
@@ -319,7 +319,7 @@ function createConsentManagement<
319319
title={t("refuse all - title")}
320320
className={fr.cx("fr-btn", "fr-btn--secondary")}
321321
disabled={isProcessingChanges}
322-
onClick={createButtonCallback("denyAll")}
322+
onClick={createOnClick("denyAll")}
323323
>
324324
{t("refuse all")}
325325
</button>
@@ -410,7 +410,7 @@ function createConsentManagement<
410410
<button
411411
className={fr.cx("fr-btn")}
412412
disabled={isProcessingChanges}
413-
onClick={createButtonCallback("apply local changes")}
413+
onClick={createOnClick("apply local changes")}
414414
>
415415
Confirmer mes choix
416416
</button>

src/gdpr/createGdprApi.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ export function createGdprApi<
1616
>
1717
>(params: {
1818
finalityDescription: ((params: { lang: string }) => FinalityDescription) | FinalityDescription;
19-
callback?: GdprConsentCallback<ExtractFinalityFromFinalityDescription<FinalityDescription>>;
19+
consentCallback?: GdprConsentCallback<
20+
ExtractFinalityFromFinalityDescription<FinalityDescription>
21+
>;
2022
/** Optional: If you have a dedicated page that provides comprehensive information about your website's GDPR policies. */
2123
personalDataPolicyLinkProps?: RegisteredLinkProps;
2224
}) {
2325
type Finality = ExtractFinalityFromFinalityDescription<FinalityDescription>;
2426

25-
const { finalityDescription, personalDataPolicyLinkProps, callback } = params;
27+
const { finalityDescription, personalDataPolicyLinkProps, consentCallback } = params;
2628

2729
const localStorageKey = "@codegouvfr/react-dsfr gdpr finalityConsent";
2830

@@ -47,14 +49,14 @@ export function createGdprApi<
4749
: finalityDescription
4850
});
4951

50-
const { processConsentChanges, useRegisterCallback } = createProcessConsentChanges<Finality>({
51-
callback,
52+
const { processConsentChanges, useConsentCallback } = createProcessConsentChanges<Finality>({
53+
consentCallback,
5254
finalities,
5355
"getFinalityConsent": () => $finalityConsent.current,
54-
"setFinalityConsent": ({ finalityConsent, prAllCallbacksRun }) => {
56+
"setFinalityConsent": ({ finalityConsent, prAllConsentCallbacksRun }) => {
5557
localStorage.setItem(localStorageKey, JSON.stringify(finalityConsent));
5658

57-
prAllCallbacksRun.then(() => ($finalityConsent.current = finalityConsent));
59+
prAllConsentCallbacksRun.then(() => ($finalityConsent.current = finalityConsent));
5860
}
5961
});
6062

@@ -77,7 +79,7 @@ export function createGdprApi<
7779
const { useGdpr } = createUseGdpr({
7880
useFinalityConsent,
7981
processConsentChanges,
80-
useRegisterCallback
82+
useConsentCallback
8183
});
8284

8385
const {

src/gdpr/processConsentChanges.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type GdprConsentCallback<Finality extends string> = (params: {
1313
export type ProcessConsentChanges<Finality extends string> = (
1414
params:
1515
| {
16-
type: "grantAll" | "denyAll" | "no changes but trigger callbacks";
16+
type: "grantAll" | "denyAll" | "no changes but trigger consent callbacks";
1717
}
1818
| {
1919
type: "atomic change";
@@ -59,48 +59,50 @@ export function createProcessConsentChanges<Finality extends string>(params: {
5959
getFinalityConsent: () => FinalityConsent<Finality> | undefined;
6060
setFinalityConsent: (params: {
6161
finalityConsent: FinalityConsent<Finality>;
62-
prAllCallbacksRun: Promise<void>;
62+
prAllConsentCallbacksRun: Promise<void>;
6363
}) => void;
64-
callback: GdprConsentCallback<Finality> | undefined;
64+
consentCallback: GdprConsentCallback<Finality> | undefined;
6565
}) {
66-
const { finalities, getFinalityConsent, setFinalityConsent, callback } = params;
66+
const { finalities, getFinalityConsent, setFinalityConsent, consentCallback } = params;
6767

68-
const callbacks: GdprConsentCallback<Finality>[] = [];
68+
const consentCallbacks: GdprConsentCallback<Finality>[] = [];
6969

70-
if (callback !== undefined) {
71-
callbacks.push(callback);
70+
if (consentCallback !== undefined) {
71+
consentCallbacks.push(consentCallback);
7272
}
7373

74-
function useRegisterCallback(params: { callback: GdprConsentCallback<Finality> | undefined }) {
75-
const { callback } = params;
74+
function useConsentCallback(params: {
75+
consentCallback: GdprConsentCallback<Finality> | undefined;
76+
}) {
77+
const { consentCallback } = params;
7678

7779
const onConsentChange_const = useConstCallback<GdprConsentCallback<Finality>>(params =>
78-
callback?.(params)
80+
consentCallback?.(params)
7981
);
8082

81-
if (!callbacks.includes(onConsentChange_const)) {
82-
callbacks.push(onConsentChange_const);
83+
if (!consentCallbacks.includes(onConsentChange_const)) {
84+
consentCallbacks.push(onConsentChange_const);
8385
}
8486

8587
useEffect(
8688
() => () => {
87-
callbacks.splice(callbacks.indexOf(onConsentChange_const), 1);
89+
consentCallbacks.splice(consentCallbacks.indexOf(onConsentChange_const), 1);
8890
},
8991
[]
9092
);
9193
}
9294

9395
const processConsentChanges: ProcessConsentChanges<Finality> = async params => {
94-
if (params.type === "no changes but trigger callbacks") {
96+
if (params.type === "no changes but trigger consent callbacks") {
9597
const finalityConsent = getFinalityConsent();
9698

9799
if (finalityConsent === undefined) {
98100
return;
99101
}
100102

101103
await Promise.all(
102-
callbacks.map(callback =>
103-
callback({
104+
consentCallbacks.map(consentCallback =>
105+
consentCallback({
104106
finalityConsent,
105107
"finalityConsent_prev": finalityConsent
106108
})
@@ -154,9 +156,9 @@ export function createProcessConsentChanges<Finality extends string>(params: {
154156

155157
setFinalityConsent({
156158
finalityConsent,
157-
"prAllCallbacksRun": Promise.all(
158-
callbacks.map(callback =>
159-
callback({
159+
"prAllConsentCallbacksRun": Promise.all(
160+
consentCallbacks.map(consentCallback =>
161+
consentCallback({
160162
finalityConsent,
161163
finalityConsent_prev
162164
})
@@ -165,7 +167,7 @@ export function createProcessConsentChanges<Finality extends string>(params: {
165167
});
166168
};
167169

168-
return { processConsentChanges, useRegisterCallback };
170+
return { processConsentChanges, useConsentCallback };
169171
}
170172

171173
/** Pure, exported for testing */

src/gdpr/useGdpr.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@ import { useConstCallback } from "../tools/powerhooks/useConstCallback";
33
import type { FinalityConsent } from "./types";
44
import type { GdprConsentCallback, ProcessConsentChanges } from "./processConsentChanges";
55

6-
export type UseGdpr<Finality extends string> = (callback?: GdprConsentCallback<Finality>) => {
6+
export type UseGdpr<Finality extends string> = (params?: {
7+
consentCallback: GdprConsentCallback<Finality>;
8+
}) => {
79
finalityConsent: FinalityConsent<Finality> | undefined;
810
assumeConsent: (finality: Finality) => void;
911
};
1012

1113
export function createUseGdpr<Finality extends string>(params: {
1214
useFinalityConsent: () => FinalityConsent<Finality> | undefined;
1315
processConsentChanges: ProcessConsentChanges<Finality>;
14-
useRegisterCallback: (params: { callback: GdprConsentCallback<Finality> | undefined }) => void;
16+
useConsentCallback: (params: {
17+
consentCallback: GdprConsentCallback<Finality> | undefined;
18+
}) => void;
1519
}): { useGdpr: UseGdpr<Finality> } {
16-
const { useFinalityConsent, processConsentChanges, useRegisterCallback } = params;
20+
const { useFinalityConsent, processConsentChanges, useConsentCallback } = params;
1721

18-
const useGdprClientSide: UseGdpr<Finality> = callback => {
19-
useRegisterCallback({ callback });
22+
const useGdprClientSide: UseGdpr<Finality> = params => {
23+
const { consentCallback } = params ?? {};
24+
25+
useConsentCallback({ consentCallback });
2026

2127
const finalityConsent = useFinalityConsent();
2228

test/integration/next-appdir/app/gdpr.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ export const {
3333
"personalDataPolicyLinkProps": {
3434
"href": "/politique-de-confidentialite",
3535
},
36-
"callback": async ()=> {
36+
"consentCallback": async ({ finalityConsent, finalityConsent_prev })=> {
37+
38+
if( finalityConsent_prev === undefined && !finalityConsent.isFullConsent ){
39+
location.reload();
40+
await new Promise(()=> {/*never*/});
41+
}
42+
3743
console.log("callback from gdpr hook");
3844
}
3945
});

0 commit comments

Comments
 (0)