-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
46 lines (39 loc) · 1.45 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { ACCESS_COOKIE_NAME, decodeJWT } from '@baseapp-frontend/utils'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import Cookies from 'js-cookie'
import UserApi, { USER_API_KEY } from '../../../services/user'
import { IUser } from '../../../types/user'
import { IUseJWTUser } from './types'
/**
* Fetches the user data using the JWT token data as placeholder data.
*
* This makes user data available before fetching it from the server.
*/
const useJWTUser = <TUser extends Partial<IUser>>({
options,
cookieName = ACCESS_COOKIE_NAME,
ApiClass = UserApi,
}: IUseJWTUser<TUser> = {}) => {
const token = Cookies.get(cookieName) || ''
const placeholderData = decodeJWT<TUser>(token) || undefined
const queryClient = useQueryClient()
const { data: user, ...rest } = useQuery({
queryFn: () => ApiClass.getUser<TUser>(),
queryKey: USER_API_KEY.getUser(),
staleTime: Infinity, // makes cache never expire automatically
enabled: !!token,
useErrorBoundary: false,
placeholderData,
...options, // needs to be placed bellow all overridable options
onError: (error: any) => {
if (error?.response?.status === 401) {
// we don't need to remove cookies here since this should be done by the interceptor
// making sure to reset the cache
queryClient.resetQueries(USER_API_KEY.getUser())
}
options?.onError?.(error)
},
})
return { user, ...rest }
}
export default useJWTUser