@@ -15,6 +15,8 @@ import {
15
15
} from '@tanstack/react-query' ;
16
16
import { ACTIVITIES_QUERY_KEY } from './useActivities' ;
17
17
import cloneDeep from 'lodash/cloneDeep' ;
18
+ import { useDeveloperConfig } from './useDeveloperConfig' ;
19
+ import { useEffect } from 'react' ;
18
20
19
21
type PatchConsentDirectives =
20
22
RestAPIEndpoints [ 'PATCH /v1/consent/directives/me/:directiveId' ] ;
@@ -95,15 +97,36 @@ export const useConsent = () => {
95
97
data : directivesData ,
96
98
isLoading : loadingDirectives ,
97
99
isFetched : fetchedDirectives ,
100
+ refetch : refetchDirectives ,
101
+ isRefetching : refetchingDirectives ,
98
102
} = useConsentDirectives ( ) ;
99
-
103
+ const { activeConsentRequired } = useDeveloperConfig ( ) ;
104
+ const activeConsents = directivesData ?. items ?. filter (
105
+ ( c ) => c . status === 'active' ,
106
+ ) ;
107
+ const hasActiveConsent = ! ! activeConsents ?. length ;
100
108
const consentDirectives = directivesData ?. items ?. filter (
101
109
( c ) => c . status === 'proposed' || c . status === 'rejected' ,
102
110
) ;
103
111
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 ] ) ;
104
126
105
127
return {
106
- isLoading : ! fetchedDirectives || loadingDirectives ,
128
+ isLoading :
129
+ ! fetchedDirectives || loadingDirectives || consentCheckNotSatisfied ,
107
130
consentDirectives,
108
131
shouldRenderConsentScreen,
109
132
} ;
@@ -142,6 +165,8 @@ export const useConsent = () => {
142
165
isLoading : boolean ;
143
166
consentDirectives : ConsentAndForm [ ] | undefined ;
144
167
shouldRenderConsentScreen : boolean ;
168
+ hasActiveConsent : boolean ;
169
+ refetchDirectives : ( ) => void ;
145
170
} ;
146
171
} ;
147
172
} ;
0 commit comments