Skip to content

Commit

Permalink
Use signal for internal state of the modal
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Jun 25, 2023
1 parent f933915 commit 01294db
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
48 changes: 32 additions & 16 deletions src/gdpr/ConsentBannerAndConsentManagement/ConsentManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
} from "../processConsentChanges";
import { exclude } from "tsafe/exclude";
import { useTranslation } from "./translation";
import { useRerenderOnChange, createStatefulObservable } from "../../tools/StatefulObservable";
import { useConst } from "../../tools/powerhooks/useConst";

export function createConsentManagement<
FinalityDescription extends Record<
Expand Down Expand Up @@ -62,23 +64,28 @@ export function createConsentManagement<
const { processLocalConsentChange, localFinalityConsent } = (function useClosure() {
const realFinalityConsent = useFinalityConsent();

const [localFinalityConsent, setLocalFinalityConsent] = useState<
FinalityConsent<ExtractFinalityFromFinalityDescription<FinalityDescription>>
>(() => realFinalityConsent ?? createFullDenyFinalityConsent(finalities));
const $localFinalityConsent = useConst(() =>
createStatefulObservable(
() => realFinalityConsent ?? createFullDenyFinalityConsent(finalities)
)
);

useRerenderOnChange($localFinalityConsent);

useEffect(() => {
if (realFinalityConsent === undefined) {
return;
}
setLocalFinalityConsent(realFinalityConsent);

$localFinalityConsent.current = realFinalityConsent;
}, [realFinalityConsent]);

const { processConsentChanges } = createProcessConsentChanges({
"consentCallback": undefined,
finalities,
"getFinalityConsent": () => localFinalityConsent,
"getFinalityConsent": () => $localFinalityConsent.current,
"setFinalityConsent": ({ finalityConsent }) =>
setLocalFinalityConsent(finalityConsent)
($localFinalityConsent.current = finalityConsent)
});

const processLocalConsentChange: (
Expand All @@ -93,7 +100,10 @@ export function createConsentManagement<
}
) => void = processConsentChanges;

return { processLocalConsentChange, localFinalityConsent };
return {
processLocalConsentChange,
"localFinalityConsent": $localFinalityConsent.current
};
})();

const [isProcessingChanges, setIsProcessingChanges] = useState(false);
Expand Down Expand Up @@ -175,13 +185,21 @@ export function createConsentManagement<
description={wrap.description}
subFinalities={wrap.subFinalities}
onChange={({ subFinality, isConsentGiven }) =>
processLocalConsentChange({
"type": "atomic change",
"finality": (subFinality === undefined
? finality
: `${finality}.${subFinality}`) as Finality,
isConsentGiven
})
(subFinality !== undefined
? [`${finality}.${subFinality}` as Finality]
: wrap.subFinalities === undefined
? [finality as Finality]
: Object.keys(wrap.subFinalities).map(
subFinality =>
`${finality}.${subFinality}` as Finality
)
).forEach(finality =>
processLocalConsentChange({
"type": "atomic change",
finality,
isConsentGiven
})
)
}
finalityConsent={
localFinalityConsent[
Expand Down Expand Up @@ -266,8 +284,6 @@ export function createConsentManagement<
[finalityConsent]
);

console.log("bite");

const [isSubFinalityDivCollapsed, setIsSubFinalityDivCollapsed] = useState(true);

return (
Expand Down
3 changes: 1 addition & 2 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ export function setUseLang(params: { useLang: () => string }) {
useLang_glob = params.useLang;
}

export function useLang(){
export function useLang() {
return useLang_glob();
}


export function createComponentI18nApi<
ComponentName extends string,
FrMessages extends Record<string, ReactNode | ((params: any) => ReactNode)>
Expand Down
10 changes: 10 additions & 0 deletions src/tools/powerhooks/useConst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useState } from "react";

/**
* Compute a value on first render and never again,
* Equivalent of const [x] = useState(()=> ...)
*/
export function useConst<T>(getValue: () => T): T {
const [value] = useState(getValue);
return value;
}

0 comments on commit 01294db

Please sign in to comment.