Skip to content

Commit 01294db

Browse files
committed
Use signal for internal state of the modal
1 parent f933915 commit 01294db

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/gdpr/ConsentBannerAndConsentManagement/ConsentManagement.tsx

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
} from "../processConsentChanges";
1515
import { exclude } from "tsafe/exclude";
1616
import { useTranslation } from "./translation";
17+
import { useRerenderOnChange, createStatefulObservable } from "../../tools/StatefulObservable";
18+
import { useConst } from "../../tools/powerhooks/useConst";
1719

1820
export function createConsentManagement<
1921
FinalityDescription extends Record<
@@ -62,23 +64,28 @@ export function createConsentManagement<
6264
const { processLocalConsentChange, localFinalityConsent } = (function useClosure() {
6365
const realFinalityConsent = useFinalityConsent();
6466

65-
const [localFinalityConsent, setLocalFinalityConsent] = useState<
66-
FinalityConsent<ExtractFinalityFromFinalityDescription<FinalityDescription>>
67-
>(() => realFinalityConsent ?? createFullDenyFinalityConsent(finalities));
67+
const $localFinalityConsent = useConst(() =>
68+
createStatefulObservable(
69+
() => realFinalityConsent ?? createFullDenyFinalityConsent(finalities)
70+
)
71+
);
72+
73+
useRerenderOnChange($localFinalityConsent);
6874

6975
useEffect(() => {
7076
if (realFinalityConsent === undefined) {
7177
return;
7278
}
73-
setLocalFinalityConsent(realFinalityConsent);
79+
80+
$localFinalityConsent.current = realFinalityConsent;
7481
}, [realFinalityConsent]);
7582

7683
const { processConsentChanges } = createProcessConsentChanges({
7784
"consentCallback": undefined,
7885
finalities,
79-
"getFinalityConsent": () => localFinalityConsent,
86+
"getFinalityConsent": () => $localFinalityConsent.current,
8087
"setFinalityConsent": ({ finalityConsent }) =>
81-
setLocalFinalityConsent(finalityConsent)
88+
($localFinalityConsent.current = finalityConsent)
8289
});
8390

8491
const processLocalConsentChange: (
@@ -93,7 +100,10 @@ export function createConsentManagement<
93100
}
94101
) => void = processConsentChanges;
95102

96-
return { processLocalConsentChange, localFinalityConsent };
103+
return {
104+
processLocalConsentChange,
105+
"localFinalityConsent": $localFinalityConsent.current
106+
};
97107
})();
98108

99109
const [isProcessingChanges, setIsProcessingChanges] = useState(false);
@@ -175,13 +185,21 @@ export function createConsentManagement<
175185
description={wrap.description}
176186
subFinalities={wrap.subFinalities}
177187
onChange={({ subFinality, isConsentGiven }) =>
178-
processLocalConsentChange({
179-
"type": "atomic change",
180-
"finality": (subFinality === undefined
181-
? finality
182-
: `${finality}.${subFinality}`) as Finality,
183-
isConsentGiven
184-
})
188+
(subFinality !== undefined
189+
? [`${finality}.${subFinality}` as Finality]
190+
: wrap.subFinalities === undefined
191+
? [finality as Finality]
192+
: Object.keys(wrap.subFinalities).map(
193+
subFinality =>
194+
`${finality}.${subFinality}` as Finality
195+
)
196+
).forEach(finality =>
197+
processLocalConsentChange({
198+
"type": "atomic change",
199+
finality,
200+
isConsentGiven
201+
})
202+
)
185203
}
186204
finalityConsent={
187205
localFinalityConsent[
@@ -266,8 +284,6 @@ export function createConsentManagement<
266284
[finalityConsent]
267285
);
268286

269-
console.log("bite");
270-
271287
const [isSubFinalityDivCollapsed, setIsSubFinalityDivCollapsed] = useState(true);
272288

273289
return (

src/i18n.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ export function setUseLang(params: { useLang: () => string }) {
7272
useLang_glob = params.useLang;
7373
}
7474

75-
export function useLang(){
75+
export function useLang() {
7676
return useLang_glob();
7777
}
7878

79-
8079
export function createComponentI18nApi<
8180
ComponentName extends string,
8281
FrMessages extends Record<string, ReactNode | ((params: any) => ReactNode)>

src/tools/powerhooks/useConst.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useState } from "react";
2+
3+
/**
4+
* Compute a value on first render and never again,
5+
* Equivalent of const [x] = useState(()=> ...)
6+
*/
7+
export function useConst<T>(getValue: () => T): T {
8+
const [value] = useState(getValue);
9+
return value;
10+
}

0 commit comments

Comments
 (0)