-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(useUser): fewer 403 returns in API
- Loading branch information
1 parent
0443bfa
commit 87bef1c
Showing
4 changed files
with
87 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,62 @@ | ||
import useSWR from 'swr'; | ||
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; | ||
|
||
const UserContext = createContext(); | ||
|
||
export function UserProvider({ children }) { | ||
const [user, setUser] = useState({}); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [isValidating, setIsValidating] = useState(false); | ||
const [error, setError] = useState(undefined); | ||
|
||
useEffect(() => { | ||
setUser(JSON.parse(localStorage.getItem('user')) || {}); | ||
}, []); | ||
|
||
const fetchUser = useCallback(async () => { | ||
try { | ||
const response = await fetch('/api/v1/user'); | ||
if ([200, 304].includes(response.status)) { | ||
const fetchedUser = await response.json(); | ||
localStorage.setItem('user', JSON.stringify(fetchedUser)); | ||
setUser(fetchedUser); | ||
} else if (response.status == 403) { | ||
localStorage.removeItem('user'); | ||
setUser({}); | ||
} | ||
} catch (error) { | ||
setError(error); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (isValidating) { | ||
(async () => { | ||
if (user?.id) await fetchUser(); | ||
setIsValidating(false); | ||
setIsLoading(false); | ||
})(); | ||
} | ||
}, [fetchUser, isValidating, user]); | ||
|
||
useEffect(() => { | ||
if (isLoading) { | ||
setIsValidating(true); | ||
} | ||
}, [isLoading]); | ||
|
||
const userContextValue = useMemo(() => { | ||
return { | ||
user, | ||
isLoading, | ||
isValidating, | ||
error, | ||
fetchUser | ||
}; | ||
}, [user, isLoading, isValidating, error, fetchUser]); | ||
|
||
return <UserContext.Provider value={userContextValue}>{children}</UserContext.Provider>; | ||
} | ||
|
||
export default function useUser() { | ||
const { data, isLoading, isValidating, error } = useSWR('/api/v1/user', { | ||
revalidateOnFocus: false, | ||
revalidateOnReconnect: false, | ||
}); | ||
|
||
return { | ||
user: data, | ||
isLoading: isLoading, | ||
isValidating: isValidating, | ||
error: error, | ||
}; | ||
return useContext(UserContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters