@@ -15,6 +15,8 @@ import {
1515} from '@tanstack/react-query' ;
1616import { ACTIVITIES_QUERY_KEY } from './useActivities' ;
1717import cloneDeep from 'lodash/cloneDeep' ;
18+ import { useDeveloperConfig } from './useDeveloperConfig' ;
19+ import { useEffect } from 'react' ;
1820
1921type PatchConsentDirectives =
2022 RestAPIEndpoints [ 'PATCH /v1/consent/directives/me/:directiveId' ] ;
@@ -95,15 +97,36 @@ export const useConsent = () => {
9597 data : directivesData ,
9698 isLoading : loadingDirectives ,
9799 isFetched : fetchedDirectives ,
100+ refetch : refetchDirectives ,
101+ isRefetching : refetchingDirectives ,
98102 } = useConsentDirectives ( ) ;
99-
103+ const { activeConsentRequired } = useDeveloperConfig ( ) ;
104+ const activeConsents = directivesData ?. items ?. filter (
105+ ( c ) => c . status === 'active' ,
106+ ) ;
107+ const hasActiveConsent = ! ! activeConsents ?. length ;
100108 const consentDirectives = directivesData ?. items ?. filter (
101109 ( c ) => c . status === 'proposed' || c . status === 'rejected' ,
102110 ) ;
103111 const shouldRenderConsentScreen = ! ! consentDirectives ?. length ;
112+ const consentCheckNotSatisfied =
113+ activeConsentRequired && ! hasActiveConsent && ! shouldRenderConsentScreen ;
114+
115+ useEffect ( ( ) => {
116+ const intervalId = setInterval ( ( ) => {
117+ if ( consentCheckNotSatisfied && ! refetchingDirectives ) {
118+ refetchDirectives ( ) ;
119+ } else {
120+ clearInterval ( intervalId ) ;
121+ }
122+ } , 2000 ) ;
123+
124+ return ( ) => clearInterval ( intervalId ) ;
125+ } , [ consentCheckNotSatisfied , refetchDirectives , refetchingDirectives ] ) ;
104126
105127 return {
106- isLoading : ! fetchedDirectives || loadingDirectives ,
128+ isLoading :
129+ ! fetchedDirectives || loadingDirectives || consentCheckNotSatisfied ,
107130 consentDirectives,
108131 shouldRenderConsentScreen,
109132 } ;
@@ -142,6 +165,8 @@ export const useConsent = () => {
142165 isLoading : boolean ;
143166 consentDirectives : ConsentAndForm [ ] | undefined ;
144167 shouldRenderConsentScreen : boolean ;
168+ hasActiveConsent : boolean ;
169+ refetchDirectives : ( ) => void ;
145170 } ;
146171 } ;
147172} ;
0 commit comments