Skip to content

Commit

Permalink
Fetch user before pass it to the restricted function
Browse files Browse the repository at this point in the history
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
wesleybl committed Sep 16, 2024
1 parent 08aab5a commit 5dd6e3d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/volto-slate/src/blocks/Text/SlashMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { filter, isEmpty } from 'lodash';
import { Menu } from 'semantic-ui-react';
import { useIntl, FormattedMessage } from 'react-intl';
import { Icon } from '@plone/volto/components';
import { useSelector } from 'react-redux';
import { useFetchUser } from '@plone/volto/hooks';

const emptySlateBlock = () => ({
value: [
Expand Down Expand Up @@ -111,7 +111,7 @@ const PersistentSlashMenu = ({ editor }) => {
} = props;
const disableNewBlocks = data?.disableNewBlocks || detached;

const user = useSelector((state) => state.users?.user);
const user = useFetchUser();

const [slashMenuSelected, setSlashMenuSelected] = React.useState(0);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { useFetchUser } from '@plone/volto/hooks';
import PropTypes from 'prop-types';
import { filter, map, groupBy, isEmpty } from 'lodash';
import { Accordion, Button } from 'semantic-ui-react';
Expand Down Expand Up @@ -36,7 +36,7 @@ const BlockChooser = ({
contentType,
}) => {
const intl = useIntl();
const user = useSelector((state) => state.users?.user);
const user = useFetchUser();
const hasAllowedBlocks = !isEmpty(allowedBlocks);

const filteredBlocksConfig = filter(blocksConfig, (item) => {
Expand Down
1 change: 1 addition & 0 deletions packages/volto/src/hooks/index.js
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';
22 changes: 22 additions & 0 deletions packages/volto/src/hooks/user/useFetchUser.js
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;

0 comments on commit 5dd6e3d

Please sign in to comment.