Skip to content

Commit d082e83

Browse files
author
Anders Rognstad
committed
Migrate to react query v5
1 parent ed6f595 commit d082e83

File tree

8 files changed

+101
-11961
lines changed

8 files changed

+101
-11961
lines changed

package-lock.json

Lines changed: 25 additions & 11907 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"@navikt/ds-react": "5.11.1",
2727
"@navikt/fnrvalidator": "1.1.4",
2828
"@navikt/navspa": "3.0.1",
29-
"@tanstack/react-query": "4.22.0",
30-
"@tanstack/react-query-devtools": "4.22.0",
29+
"@tanstack/react-query": "5.8.6",
30+
"@tanstack/react-query-devtools": "5.8.6",
3131
"@types/connect-redis": "0.0.19",
3232
"@types/express": "4.17.15",
3333
"@types/express-http-proxy": "1.6.3",

src/containers/OversiktContainer.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ const OversiktContainer = ({ tabType }: Props): ReactElement => {
3131
}, [setTabType, tabType]);
3232

3333
const ContainerContent = (): ReactElement => {
34-
if (
35-
personoversiktQuery.isInitialLoading ||
36-
personregisterQuery.isInitialLoading
37-
) {
34+
if (personoversiktQuery.isLoading || personregisterQuery.isLoading) {
3835
return <AppSpinner />;
3936
}
4037

src/data/personoversiktHooks.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { FetchPersonoversiktFailed } from '@/context/notification/Notifications'
1010
import { ApiErrorException } from '@/api/errors';
1111
import { useAsyncError } from '@/data/useAsyncError';
1212
import { minutesToMillis } from '@/utils/timeUtils';
13-
import { useMemo } from 'react';
13+
import { useEffect, useMemo } from 'react';
1414
import { PERSONOVERSIKT_ROOT } from '@/apiConstants';
1515

1616
const isUbehandlet = (ubehandletStatus: PersonOversiktUbehandletStatusDTO) => {
@@ -59,22 +59,28 @@ export const usePersonoversiktQuery = () => {
5959
return personoversiktData || [];
6060
};
6161

62-
const query = useQuery({
62+
const query = useQuery<PersonOversiktStatusDTO[], ApiErrorException>({
6363
queryKey: personoversiktQueryKeys.personoversiktEnhet(aktivEnhet),
6464
queryFn: fetchPersonoversikt,
6565
enabled: !!aktivEnhet,
6666
staleTime: minutesToMillis(5),
67-
onError: (error) => {
68-
if (error instanceof ApiErrorException && error.code === 403) {
69-
throwError(error);
67+
});
68+
69+
useEffect(() => {
70+
if (query.isSuccess) {
71+
clearNotification('fetchPersonoversiktFailed');
72+
}
73+
}, [query.isSuccess, clearNotification]);
74+
75+
useEffect(() => {
76+
if (query.isError) {
77+
if (query.error.code === 403) {
78+
throwError(query.error);
7079
} else {
7180
displayNotification(FetchPersonoversiktFailed);
7281
}
73-
},
74-
onSuccess: () => {
75-
clearNotification('fetchPersonoversiktFailed');
76-
},
77-
});
82+
}
83+
}, [throwError, query.isError, query.error, displayNotification]);
7884

7985
return {
8086
...query,

src/data/personregisterHooks.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ApiErrorException } from '@/api/errors';
77
import { FetchPersonregisterFailed } from '@/context/notification/Notifications';
88
import { useNotifications } from '@/context/notification/NotificationContext';
99
import { useAsyncError } from '@/data/useAsyncError';
10+
import { useEffect } from 'react';
1011

1112
export const personregisterQueryKeys = {
1213
personregister: ['personregister'],
@@ -34,19 +35,27 @@ export const usePersonregisterQuery = () => {
3435
return personregisterData || [];
3536
};
3637

37-
return useQuery({
38+
const query = useQuery<PersonregisterData[], ApiErrorException>({
3839
queryKey: personregisterQueryKeys.personregister,
3940
queryFn: fetchPersonregister,
4041
enabled: fnrForPersonerUtenNavnListe.length > 0,
41-
onError: (error) => {
42-
if (error instanceof ApiErrorException && error.code === 403) {
43-
throwError(error);
42+
});
43+
44+
useEffect(() => {
45+
if (query.isSuccess) {
46+
clearNotification('fetchPersonregisterFailed');
47+
}
48+
}, [query.isSuccess, clearNotification]);
49+
50+
useEffect(() => {
51+
if (query.isError) {
52+
if (query.error.code === 403) {
53+
throwError(query.error);
4454
} else {
4555
displayNotification(FetchPersonregisterFailed);
4656
}
47-
},
48-
onSuccess: () => {
49-
clearNotification('fetchPersonregisterFailed');
50-
},
51-
});
57+
}
58+
}, [throwError, query.isError, query.error, displayNotification]);
59+
60+
return query;
5261
};

src/data/veiledereQueryHooks.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '@/context/notification/Notifications';
1616
import { useAsyncError } from '@/data/useAsyncError';
1717
import { ApiErrorException } from '@/api/errors';
18+
import { useEffect } from 'react';
1819

1920
export const veiledereQueryKeys = {
2021
veiledereInfo: ['veiledereInfo'],
@@ -33,21 +34,29 @@ export const useVeiledereQuery = () => {
3334
const fetchVeiledere = () =>
3435
get<Veileder[]>(`${SYFOVEILEDER_ROOT}/veiledere/enhet/${aktivEnhet}`);
3536

36-
return useQuery({
37+
const query = useQuery<Veileder[], ApiErrorException>({
3738
queryKey: veiledereQueryKeys.veiledereForEnhet(aktivEnhet),
3839
queryFn: fetchVeiledere,
3940
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);
4353
} else {
4454
displayNotification(FetchVeiledereFailed);
4555
}
46-
},
47-
onSuccess: () => {
48-
clearNotification('fetchVeiledereFailed');
49-
},
50-
});
56+
}
57+
}, [throwError, query.isError, query.error, displayNotification]);
58+
59+
return query;
5160
};
5261

5362
export const useAktivVeilederQuery = () => {
@@ -57,20 +66,28 @@ export const useAktivVeilederQuery = () => {
5766
const fetchVeilederInfo = () =>
5867
get<VeilederinfoDTO>(`${SYFOVEILEDER_ROOT}/veileder/self`);
5968

60-
return useQuery({
69+
const query = useQuery<VeilederinfoDTO, ApiErrorException>({
6170
queryKey: veiledereQueryKeys.veiledereInfo,
6271
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);
6684
} else {
6785
displayNotification(FetchAktivVeilederFailed);
6886
}
69-
},
70-
onSuccess: () => {
71-
clearNotification('fetchAktivVeilederFailed');
72-
},
73-
});
87+
}
88+
}, [throwError, query.isError, query.error, displayNotification]);
89+
90+
return query;
7491
};
7592

7693
export const useTildelVeileder = () => {
@@ -126,9 +143,9 @@ export const useTildelVeileder = () => {
126143
);
127144
},
128145
onSettled: () => {
129-
queryClient.invalidateQueries(
130-
personoversiktQueryKeys.personoversiktEnhet(aktivEnhet)
131-
);
146+
queryClient.invalidateQueries({
147+
queryKey: personoversiktQueryKeys.personoversiktEnhet(aktivEnhet),
148+
});
132149
},
133150
});
134151
};

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const queryClient = new QueryClient({
2525
queries: {
2626
networkMode: 'offlineFirst',
2727
refetchOnWindowFocus: false,
28-
cacheTime: minutesToMillis(60),
28+
gcTime: minutesToMillis(60),
2929
staleTime: minutesToMillis(30),
3030
retry: (failureCount, error) => {
3131
if (isClientError(error)) {

test/testQueryClient.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,4 @@ import { QueryClient } from '@tanstack/react-query';
33
export const testQueryClient = () =>
44
new QueryClient({
55
defaultOptions: { queries: { retry: false } },
6-
logger: {
7-
log: console.log,
8-
warn: console.warn,
9-
error: () => {
10-
/*empty*/
11-
},
12-
},
136
});

0 commit comments

Comments
 (0)