Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch user before pass it to the restricted function #6293

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/volto-slate/news/6293.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fetch `user` before pass it to the `restricted` function of the block settings. @wesleybl
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 { useUser } 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 = useUser();

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

Expand Down
1 change: 1 addition & 0 deletions packages/volto/news/6293.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fetch `user` before pass it to the `restricted` function of the block settings. @wesleybl
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -121,6 +122,9 @@ const store = mockStore({
locale: 'en',
messages: {},
},
userSession: {
token: jwt.sign({ fullname: 'John Doe' }, 'secret'),
},
});

describe('BlocksChooser', () => {
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 useUser } from '@plone/volto/hooks/user/useUser';
23 changes: 23 additions & 0 deletions packages/volto/src/hooks/user/useUser.js
Original file line number Diff line number Diff line change
@@ -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;
Loading