@@ -21,6 +21,7 @@ import { Confirm } from 'src/features/processEnd/confirm/containers/Confirm';
21
21
import { Feedback } from 'src/features/processEnd/feedback/Feedback' ;
22
22
import { useNavigationParam } from 'src/hooks/navigation' ;
23
23
import { TaskKeys , useIsValidTaskId , useNavigateToTask , useStartUrl } from 'src/hooks/useNavigatePage' ;
24
+ import { useWaitForQueries } from 'src/hooks/useWaitForQueries' ;
24
25
import { getComponentDef , implementsSubRouting } from 'src/layout' ;
25
26
import { RedirectBackToMainForm } from 'src/layout/Subform/SubformWrapper' ;
26
27
import { ProcessTaskType } from 'src/types' ;
@@ -197,33 +198,34 @@ function useIsWrongTask(taskId: string | undefined) {
197
198
const isNavigating = useIsNavigating ( ) ;
198
199
const { data : process } = useProcessQuery ( ) ;
199
200
const currentTaskId = process ?. currentTask ?. elementId ;
201
+ const waitForQueries = useWaitForQueries ( ) ;
200
202
201
203
const [ isWrongTask , setIsWrongTask ] = useState < boolean | null > ( null ) ;
202
204
const isCurrentTask =
203
205
currentTaskId === undefined && taskId === TaskKeys . CustomReceipt ? true : currentTaskId === taskId ;
204
206
205
- const timeoutRef = React . useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
206
-
207
- // We intentionally delay this state from being set until after a useEffect(), so the navigation error does not
208
- // show up while we're navigating. Without this, the message will flash over the screen shortly in-between all the
209
- // <Loader /> components.
207
+ // We intentionally delay this state from being set until after queries/mutations finish, so the navigation error
208
+ // does not show up while we're navigating. Without this, the message will flash over the screen shortly
209
+ // in-between all the <Loader /> components.
210
210
useEffect ( ( ) => {
211
- if ( timeoutRef . current ) {
212
- clearTimeout ( timeoutRef . current ) ;
213
- timeoutRef . current = null ;
214
- }
215
211
if ( isCurrentTask ) {
216
212
setIsWrongTask ( false ) ;
217
213
} else {
218
- timeoutRef . current = setTimeout ( ( ) => {
219
- setIsWrongTask ( true ) ;
220
- timeoutRef . current = null ;
221
- } , 100 ) ;
214
+ let cancelled = false ;
215
+ const delayedCheck = async ( ) => {
216
+ await waitForQueries ( ) ;
217
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ; // Wait a bit longer, for navigation to maybe occur
218
+ if ( ! cancelled ) {
219
+ setIsWrongTask ( true ) ;
220
+ }
221
+ } ;
222
+ delayedCheck ( ) . then ( ) ;
223
+
224
+ return ( ) => {
225
+ cancelled = true ;
226
+ } ;
222
227
}
223
-
224
- const timeout = timeoutRef . current ;
225
- return ( ) => void ( timeout && clearTimeout ( timeout ) ) ;
226
- } , [ isCurrentTask ] ) ;
228
+ } , [ isCurrentTask , waitForQueries ] ) ;
227
229
228
230
return isWrongTask && ! isCurrentTask && ! isNavigating ;
229
231
}
0 commit comments