@@ -15,6 +15,7 @@ import {
15
15
} from '@/context/notification/Notifications' ;
16
16
import { useAsyncError } from '@/data/useAsyncError' ;
17
17
import { ApiErrorException } from '@/api/errors' ;
18
+ import { useEffect } from 'react' ;
18
19
19
20
export const veiledereQueryKeys = {
20
21
veiledereInfo : [ 'veiledereInfo' ] ,
@@ -33,21 +34,29 @@ export const useVeiledereQuery = () => {
33
34
const fetchVeiledere = ( ) =>
34
35
get < Veileder [ ] > ( `${ SYFOVEILEDER_ROOT } /veiledere/enhet/${ aktivEnhet } ` ) ;
35
36
36
- return useQuery ( {
37
+ const query = useQuery < Veileder [ ] , ApiErrorException > ( {
37
38
queryKey : veiledereQueryKeys . veiledereForEnhet ( aktivEnhet ) ,
38
39
queryFn : fetchVeiledere ,
39
40
enabled : ! ! aktivEnhet ,
40
- onError : ( error ) => {
41
- if ( error instanceof ApiErrorException && error . code === 403 ) {
42
- throwError ( error ) ;
41
+ } ) ;
42
+
43
+ useEffect ( ( ) => {
44
+ if ( query . isSuccess ) {
45
+ clearNotification ( 'fetchVeiledereFailed' ) ;
46
+ }
47
+ } , [ query . isSuccess , clearNotification ] ) ;
48
+
49
+ useEffect ( ( ) => {
50
+ if ( query . isError ) {
51
+ if ( query . error . code === 403 ) {
52
+ throwError ( query . error ) ;
43
53
} else {
44
54
displayNotification ( FetchVeiledereFailed ) ;
45
55
}
46
- } ,
47
- onSuccess : ( ) => {
48
- clearNotification ( 'fetchVeiledereFailed' ) ;
49
- } ,
50
- } ) ;
56
+ }
57
+ } , [ throwError , query . isError , query . error , displayNotification ] ) ;
58
+
59
+ return query ;
51
60
} ;
52
61
53
62
export const useAktivVeilederQuery = ( ) => {
@@ -57,20 +66,28 @@ export const useAktivVeilederQuery = () => {
57
66
const fetchVeilederInfo = ( ) =>
58
67
get < VeilederinfoDTO > ( `${ SYFOVEILEDER_ROOT } /veileder/self` ) ;
59
68
60
- return useQuery ( {
69
+ const query = useQuery < VeilederinfoDTO , ApiErrorException > ( {
61
70
queryKey : veiledereQueryKeys . veiledereInfo ,
62
71
queryFn : fetchVeilederInfo ,
63
- onError : ( error ) => {
64
- if ( error instanceof ApiErrorException && error . code === 403 ) {
65
- throwError ( error ) ;
72
+ } ) ;
73
+
74
+ useEffect ( ( ) => {
75
+ if ( query . isSuccess ) {
76
+ clearNotification ( 'fetchAktivVeilederFailed' ) ;
77
+ }
78
+ } , [ query . isSuccess , clearNotification ] ) ;
79
+
80
+ useEffect ( ( ) => {
81
+ if ( query . isError ) {
82
+ if ( query . error . code === 403 ) {
83
+ throwError ( query . error ) ;
66
84
} else {
67
85
displayNotification ( FetchAktivVeilederFailed ) ;
68
86
}
69
- } ,
70
- onSuccess : ( ) => {
71
- clearNotification ( 'fetchAktivVeilederFailed' ) ;
72
- } ,
73
- } ) ;
87
+ }
88
+ } , [ throwError , query . isError , query . error , displayNotification ] ) ;
89
+
90
+ return query ;
74
91
} ;
75
92
76
93
export const useTildelVeileder = ( ) => {
@@ -126,9 +143,9 @@ export const useTildelVeileder = () => {
126
143
) ;
127
144
} ,
128
145
onSettled : ( ) => {
129
- queryClient . invalidateQueries (
130
- personoversiktQueryKeys . personoversiktEnhet ( aktivEnhet )
131
- ) ;
146
+ queryClient . invalidateQueries ( {
147
+ queryKey : personoversiktQueryKeys . personoversiktEnhet ( aktivEnhet ) ,
148
+ } ) ;
132
149
} ,
133
150
} ) ;
134
151
} ;
0 commit comments