@@ -14,6 +14,8 @@ import {
14
14
} from "../processConsentChanges" ;
15
15
import { exclude } from "tsafe/exclude" ;
16
16
import { useTranslation } from "./translation" ;
17
+ import { useRerenderOnChange , createStatefulObservable } from "../../tools/StatefulObservable" ;
18
+ import { useConst } from "../../tools/powerhooks/useConst" ;
17
19
18
20
export function createConsentManagement <
19
21
FinalityDescription extends Record <
@@ -62,23 +64,28 @@ export function createConsentManagement<
62
64
const { processLocalConsentChange, localFinalityConsent } = ( function useClosure ( ) {
63
65
const realFinalityConsent = useFinalityConsent ( ) ;
64
66
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 ) ;
68
74
69
75
useEffect ( ( ) => {
70
76
if ( realFinalityConsent === undefined ) {
71
77
return ;
72
78
}
73
- setLocalFinalityConsent ( realFinalityConsent ) ;
79
+
80
+ $localFinalityConsent . current = realFinalityConsent ;
74
81
} , [ realFinalityConsent ] ) ;
75
82
76
83
const { processConsentChanges } = createProcessConsentChanges ( {
77
84
"consentCallback" : undefined ,
78
85
finalities,
79
- "getFinalityConsent" : ( ) => localFinalityConsent ,
86
+ "getFinalityConsent" : ( ) => $ localFinalityConsent. current ,
80
87
"setFinalityConsent" : ( { finalityConsent } ) =>
81
- setLocalFinalityConsent ( finalityConsent )
88
+ ( $localFinalityConsent . current = finalityConsent )
82
89
} ) ;
83
90
84
91
const processLocalConsentChange : (
@@ -93,7 +100,10 @@ export function createConsentManagement<
93
100
}
94
101
) => void = processConsentChanges ;
95
102
96
- return { processLocalConsentChange, localFinalityConsent } ;
103
+ return {
104
+ processLocalConsentChange,
105
+ "localFinalityConsent" : $localFinalityConsent . current
106
+ } ;
97
107
} ) ( ) ;
98
108
99
109
const [ isProcessingChanges , setIsProcessingChanges ] = useState ( false ) ;
@@ -175,13 +185,21 @@ export function createConsentManagement<
175
185
description = { wrap . description }
176
186
subFinalities = { wrap . subFinalities }
177
187
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
+ )
185
203
}
186
204
finalityConsent = {
187
205
localFinalityConsent [
@@ -266,8 +284,6 @@ export function createConsentManagement<
266
284
[ finalityConsent ]
267
285
) ;
268
286
269
- console . log ( "bite" ) ;
270
-
271
287
const [ isSubFinalityDivCollapsed , setIsSubFinalityDivCollapsed ] = useState ( true ) ;
272
288
273
289
return (
0 commit comments