-
-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch user before pass it to the restricted function
It may not be in the Redux store yet when we try to pass it in. So we dispatch if it's not in the store.
- Loading branch information
Showing
4 changed files
with
27 additions
and
4 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,2 +1,3 @@ | ||
export { default as useClipboard } from '@plone/volto/hooks/clipboard/useClipboard'; | ||
export { useClient } from '@plone/volto/hooks/client/useClient'; | ||
export { default as useFetchUser } from '@plone/volto/hooks/user/useFetchUser'; |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useEffect } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import jwtDecode from 'jwt-decode'; | ||
import { getUser } from '@plone/volto/actions'; | ||
|
||
const useFetchUser = () => { | ||
const user = useSelector((state) => state.users?.user); | ||
const userId = useSelector((state) => | ||
state.userSession.token ? jwtDecode(state.userSession.token).sub : '', | ||
); | ||
const dispatch = useDispatch(); | ||
|
||
useEffect(() => { | ||
if (!user?.id) { | ||
dispatch(getUser(userId)); | ||
} | ||
}, [dispatch, userId, user]); | ||
|
||
return user; | ||
}; | ||
|
||
export default useFetchUser; |