From 6e498dec450ffe747b5bae7308362d879e7e59f4 Mon Sep 17 00:00:00 2001 From: Wesley Barroso Lopes Date: Thu, 3 Oct 2024 03:51:55 -0300 Subject: [PATCH] Fetch user before pass it to the restricted function (#6293) --- packages/volto-slate/news/6293.bugfix | 1 + .../volto-slate/src/blocks/Text/SlashMenu.jsx | 4 ++-- packages/volto/news/6293.bugfix | 1 + .../manage/BlockChooser/BlockChooser.jsx | 4 ++-- .../manage/BlockChooser/BlockChooser.test.jsx | 4 ++++ packages/volto/src/hooks/index.js | 1 + packages/volto/src/hooks/user/useUser.js | 23 +++++++++++++++++++ 7 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 packages/volto-slate/news/6293.bugfix create mode 100644 packages/volto/news/6293.bugfix create mode 100644 packages/volto/src/hooks/user/useUser.js diff --git a/packages/volto-slate/news/6293.bugfix b/packages/volto-slate/news/6293.bugfix new file mode 100644 index 0000000000..ea8503c548 --- /dev/null +++ b/packages/volto-slate/news/6293.bugfix @@ -0,0 +1 @@ +Fetch `user` before pass it to the `restricted` function of the block settings. @wesleybl diff --git a/packages/volto-slate/src/blocks/Text/SlashMenu.jsx b/packages/volto-slate/src/blocks/Text/SlashMenu.jsx index 2a26c39cfe..9ec1612304 100644 --- a/packages/volto-slate/src/blocks/Text/SlashMenu.jsx +++ b/packages/volto-slate/src/blocks/Text/SlashMenu.jsx @@ -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 { useUser } from '@plone/volto/hooks'; const emptySlateBlock = () => ({ value: [ @@ -111,7 +111,7 @@ const PersistentSlashMenu = ({ editor }) => { } = props; const disableNewBlocks = data?.disableNewBlocks || detached; - const user = useSelector((state) => state.users?.user); + const user = useUser(); const [slashMenuSelected, setSlashMenuSelected] = React.useState(0); diff --git a/packages/volto/news/6293.bugfix b/packages/volto/news/6293.bugfix new file mode 100644 index 0000000000..ea8503c548 --- /dev/null +++ b/packages/volto/news/6293.bugfix @@ -0,0 +1 @@ +Fetch `user` before pass it to the `restricted` function of the block settings. @wesleybl diff --git a/packages/volto/src/components/manage/BlockChooser/BlockChooser.jsx b/packages/volto/src/components/manage/BlockChooser/BlockChooser.jsx index 80c5142e81..2ae878e5ba 100644 --- a/packages/volto/src/components/manage/BlockChooser/BlockChooser.jsx +++ b/packages/volto/src/components/manage/BlockChooser/BlockChooser.jsx @@ -1,5 +1,5 @@ import React from 'react'; -import { useSelector } from 'react-redux'; +import { useUser } from '@plone/volto/hooks'; import PropTypes from 'prop-types'; import { filter, map, groupBy, isEmpty } from 'lodash'; import { Accordion, Button } from 'semantic-ui-react'; @@ -36,7 +36,7 @@ const BlockChooser = ({ contentType, }) => { const intl = useIntl(); - const user = useSelector((state) => state.users?.user); + const user = useUser(); const hasAllowedBlocks = !isEmpty(allowedBlocks); const filteredBlocksConfig = filter(blocksConfig, (item) => { diff --git a/packages/volto/src/components/manage/BlockChooser/BlockChooser.test.jsx b/packages/volto/src/components/manage/BlockChooser/BlockChooser.test.jsx index 447c20f61d..d2bd1bbe5a 100644 --- a/packages/volto/src/components/manage/BlockChooser/BlockChooser.test.jsx +++ b/packages/volto/src/components/manage/BlockChooser/BlockChooser.test.jsx @@ -4,6 +4,7 @@ import { Provider } from 'react-intl-redux'; import configureStore from 'redux-mock-store'; import BlockChooser from './BlockChooser'; import config from '@plone/volto/registry'; +import jwt from 'jsonwebtoken'; const blockSVG = {}; @@ -121,6 +122,9 @@ const store = mockStore({ locale: 'en', messages: {}, }, + userSession: { + token: jwt.sign({ fullname: 'John Doe' }, 'secret'), + }, }); describe('BlocksChooser', () => { diff --git a/packages/volto/src/hooks/index.js b/packages/volto/src/hooks/index.js index a955aa8285..f8e327c0e6 100644 --- a/packages/volto/src/hooks/index.js +++ b/packages/volto/src/hooks/index.js @@ -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 useUser } from '@plone/volto/hooks/user/useUser'; diff --git a/packages/volto/src/hooks/user/useUser.js b/packages/volto/src/hooks/user/useUser.js new file mode 100644 index 0000000000..3232ed0a8e --- /dev/null +++ b/packages/volto/src/hooks/user/useUser.js @@ -0,0 +1,23 @@ +import { useEffect } from 'react'; +import { useDispatch, useSelector } from 'react-redux'; +import jwtDecode from 'jwt-decode'; +import { getUser } from '@plone/volto/actions'; + +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 && users?.get.loading === false) { + dispatch(getUser(userId)); + } + }, [dispatch, userId, user, users?.get.loading]); + + return user; +}; + +export default useUser;