1
- import { Dispatch , FC , ReactNode , SetStateAction , useEffect , useState } from 'react'
1
+ import { Dispatch , FC , ReactNode , SetStateAction , useEffect , useRef , useState } from 'react'
2
2
3
3
import { userUpdatePasswordAsync } from '../../auth'
4
4
import { ChangePasswordRequest } from '../change-password-request.model'
@@ -18,13 +18,21 @@ export const ProfileProvider: FC<ProfileProviderProps> = (props: ProfileProvider
18
18
const [ profileContextData , setProfileContextData ] :
19
19
[ ProfileContextData , Dispatch < SetStateAction < ProfileContextData > > ]
20
20
= useState < ProfileContextData > ( defaultProfileContextData )
21
+ const isFetchingProfileRef = useRef < boolean > ( false )
21
22
22
23
function changePassword ( userId : number , request : ChangePasswordRequest ) : Promise < void > {
23
24
return userUpdatePasswordAsync ( userId , request . password , request . newPassword )
24
25
}
25
26
26
27
async function getAndSetProfileAsync ( ) : Promise < void > {
27
- const profile : UserProfile | undefined = await profileGetLoggedInAsync ( )
28
+ isFetchingProfileRef . current = true
29
+ let profile : UserProfile | undefined
30
+ try {
31
+ profile = await profileGetLoggedInAsync ( )
32
+ } catch ( error ) {
33
+ }
34
+
35
+ isFetchingProfileRef . current = false
28
36
const contextData : ProfileContextData = {
29
37
changePassword,
30
38
initialized : true ,
@@ -67,7 +75,7 @@ export const ProfileProvider: FC<ProfileProviderProps> = (props: ProfileProvider
67
75
useEffect ( ( ) => {
68
76
69
77
// if our profile is already initialized, no need to continue
70
- if ( profileContextData . initialized ) {
78
+ if ( profileContextData . initialized || isFetchingProfileRef . current ) {
71
79
return
72
80
}
73
81
0 commit comments