Skip to content

Commit

Permalink
Avoid dispatching getUser multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleybl committed Sep 25, 2024
1 parent 5972336 commit bd5c4cc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
4 changes: 0 additions & 4 deletions packages/volto/src/components/theme/Login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { toast } from 'react-toastify';
import { Toast } from '@plone/volto/components';
import aheadSVG from '@plone/volto/icons/ahead.svg';
import clearSVG from '@plone/volto/icons/clear.svg';
import { useUser } from '@plone/volto/hooks';

const messages = defineMessages({
login: {
Expand Down Expand Up @@ -86,9 +85,6 @@ const Login = (props) => {

const previousToken = usePrevious(token);

// Get user and place their data in the Redux store
useUser();

useEffect(() => {
if (location?.state?.isLogout) {
// Execute a true Logout action
Expand Down
11 changes: 6 additions & 5 deletions packages/volto/src/hooks/user/useUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ 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 useUser = () => {
const users = useSelector((state) => state.users);
const user = users?.user;
const userId = useSelector((state) =>
state.userSession.token ? jwtDecode(state.userSession.token).sub : '',
);
const dispatch = useDispatch();

useEffect(() => {
if (!user?.id && userId) {
if (!user?.id && users?.get.loading === false) {
dispatch(getUser(userId));
}
}, [dispatch, userId, user]);
}, [dispatch, userId, user, users?.get.loading]);

return user;
};

export default useFetchUser;
export default useUser;

0 comments on commit bd5c4cc

Please sign in to comment.