@@ -101,14 +101,20 @@ function createConsentBanner<Finality extends string>(params: {
101101
102102 const [ hostname , setHostname ] = useState ( "" ) ;
103103
104- const [ isApplying , notifyStartApplying ] = useReducer ( ( ) => true , false ) ;
104+ const [ isProcessingChanges , setIsProcessingChanges ] = useState ( false ) ;
105+
106+ console . log ( "consent banner render" , { isProcessingChanges } ) ;
105107
106108 useEffect ( ( ) => {
107109 if ( ! isBrowser ) {
108110 return ;
109111 }
110112
111113 setHostname ( location . host ) ;
114+
115+ return ( ) => {
116+ console . log ( "consent banner cleanup" ) ;
117+ } ;
112118 } , [ ] ) ;
113119
114120 return (
@@ -133,11 +139,12 @@ function createConsentBanner<Finality extends string>(params: {
133139 < button
134140 className = { fr . cx ( "fr-btn" ) }
135141 title = { t ( "accept all - title" ) }
136- onClick = { ( ) => {
137- processConsentChanges ( { "type" : "grantAll" } ) ;
138- notifyStartApplying ( ) ;
142+ onClick = { async ( ) => {
143+ setIsProcessingChanges ( true ) ;
144+ await processConsentChanges ( { "type" : "grantAll" } ) ;
145+ setIsProcessingChanges ( false ) ;
139146 } }
140- disabled = { isApplying }
147+ disabled = { isProcessingChanges }
141148 >
142149 { t ( "accept all" ) }
143150 </ button >
@@ -147,10 +154,11 @@ function createConsentBanner<Finality extends string>(params: {
147154 className = { fr . cx ( "fr-btn" ) }
148155 title = { t ( "refuse all - title" ) }
149156 onClick = { ( ) => {
157+ setIsProcessingChanges ( true ) ;
150158 processConsentChanges ( { "type" : "denyAll" } ) ;
151- notifyStartApplying ( ) ;
159+ setIsProcessingChanges ( false ) ;
152160 } }
153- disabled = { isApplying }
161+ disabled = { isProcessingChanges }
154162 >
155163 { t ( "refuse all" ) }
156164 </ button >
@@ -159,7 +167,7 @@ function createConsentBanner<Finality extends string>(params: {
159167 < button
160168 className = { fr . cx ( "fr-btn" , "fr-btn--secondary" ) }
161169 title = { t ( "customize cookies - title" ) }
162- disabled = { isApplying }
170+ disabled = { isProcessingChanges }
163171 { ...consentModalButtonProps }
164172 >
165173 { t ( "customize" ) }
@@ -216,11 +224,20 @@ function createConsentManagement<
216224
217225 const finalityConsent = useFinalityConsent ( ) ;
218226
227+ const [ isProcessingChanges , setIsProcessingChanges ] = useState ( false ) ;
228+
219229 // eslint-disable-next-line no-constant-condition
220230 if ( 1 + 0 === 1 + 1 ) {
221231 return < pre > { JSON . stringify ( { finalityDescription, finalityConsent } , null , 2 ) } </ pre > ;
222232 }
223233
234+ const createOnAcceptOrRefuseAll = ( type : "grantAll" | "denyAll" ) => async ( ) => {
235+ setIsProcessingChanges ( true ) ;
236+ await processConsentChanges ( { type } ) ;
237+ setIsProcessingChanges ( false ) ;
238+ modal . close ( ) ;
239+ } ;
240+
224241 return (
225242 < modal . Component title = { t ( "consent modal title" ) } size = "large" >
226243 < div >
@@ -240,21 +257,16 @@ function createConsentManagement<
240257 < button
241258 title = { t ( "accept all - title" ) }
242259 className = { fr . cx ( "fr-btn" ) }
243- onClick = { async ( ) => {
244- await processConsentChanges ( { "type" : "grantAll" } ) ;
245- //TODO: implement loading feedback
246- modal . close ( ) ;
247- } }
260+ onClick = { createOnAcceptOrRefuseAll ( "grantAll" ) }
261+ disabled = { isProcessingChanges }
248262 >
249263 { t ( "accept all" ) }
250264 </ button > { " " }
251265 < button
252266 title = { t ( "refuse all - title" ) }
253267 className = { fr . cx ( "fr-btn" , "fr-btn--secondary" ) }
254- onClick = { async ( ) => {
255- await processConsentChanges ( { "type" : "denyAll" } ) ;
256- modal . close ( ) ;
257- } }
268+ disabled = { isProcessingChanges }
269+ onClick = { createOnAcceptOrRefuseAll ( "denyAll" ) }
258270 >
259271 { t ( "refuse all" ) }
260272 </ button >
@@ -314,7 +326,9 @@ function createConsentManagement<
314326 ) }
315327 >
316328 < li >
317- < button className = { fr . cx ( "fr-btn" ) } > Confirmer mes choix</ button >
329+ < button className = { fr . cx ( "fr-btn" ) } disabled = { isProcessingChanges } >
330+ Confirmer mes choix
331+ </ button >
318332 </ li >
319333 </ ul >
320334 </ div >
0 commit comments