From 8856f3121da43d07d1e346f5ef7f16656a3f6182 Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Thu, 17 Jun 2021 13:50:29 +0200 Subject: [PATCH] Merge pull request #4118 from overleaf/msm-fix-nav-toolbar-anonymous Fix problems with React Navigation Toolbar on anonymous access GitOrigin-RevId: 29b652ac2765129e3adf2f9bca2309e8a18ac823 --- .../js/features/chat/context/chat-context.js | 6 +++--- .../components/editor-navigation-toolbar-root.js | 8 ++++++++ .../components/toolbar-header.js | 8 +++++++- .../frontend/js/shared/context/root-context.js | 8 +------- .../components/toolbar-header.test.js | 16 ++++++++++++++++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/services/web/frontend/js/features/chat/context/chat-context.js b/services/web/frontend/js/features/chat/context/chat-context.js index dca8854d32..f4bdcabf18 100644 --- a/services/web/frontend/js/features/chat/context/chat-context.js +++ b/services/web/frontend/js/features/chat/context/chat-context.js @@ -118,7 +118,7 @@ ChatContext.Provider.propTypes = { export function ChatProvider({ children }) { const { user } = useApplicationContext({ - user: PropTypes.shape({ id: PropTypes.string.isRequired }.isRequired), + user: PropTypes.shape({ id: PropTypes.string.isRequired }), }) const { projectId } = useEditorContext({ projectId: PropTypes.string.isRequired, @@ -222,7 +222,7 @@ export function ChatProvider({ children }) { // If the message is from the current user and they just sent a message, // then we are receiving the sent message back from the socket. Ignore it // to prevent double message - const messageIsFromSelf = message?.user?.id === user.id + const messageIsFromSelf = message?.user?.id === user?.id if (messageIsFromSelf && state.messageWasJustSent) return dispatch({ type: 'RECEIVE_MESSAGE', message }) @@ -242,7 +242,7 @@ export function ChatProvider({ children }) { // We're adding and removing the socket listener every time we send a // message (and messageWasJustSent changes). Not great, but no good way // around it - }, [socket, state.messageWasJustSent, state.unreadMessageCount, user.id]) + }, [socket, state.messageWasJustSent, state.unreadMessageCount, user]) // Handle unread messages useEffect(() => { diff --git a/services/web/frontend/js/features/editor-navigation-toolbar/components/editor-navigation-toolbar-root.js b/services/web/frontend/js/features/editor-navigation-toolbar/components/editor-navigation-toolbar-root.js index 82f0c91569..d1b468363c 100644 --- a/services/web/frontend/js/features/editor-navigation-toolbar/components/editor-navigation-toolbar-root.js +++ b/services/web/frontend/js/features/editor-navigation-toolbar/components/editor-navigation-toolbar-root.js @@ -4,6 +4,11 @@ import ToolbarHeader from './toolbar-header' import { useEditorContext } from '../../../shared/context/editor-context' import { useChatContext } from '../../chat/context/chat-context' import { useLayoutContext } from '../../../shared/context/layout-context' +import { useApplicationContext } from '../../../shared/context/application-context' + +const applicationContextPropTypes = { + user: PropTypes.object, +} const editorContextPropTypes = { cobranding: PropTypes.object, @@ -35,6 +40,8 @@ function EditorNavigationToolbarRoot({ openDoc, openShareProjectModal, }) { + const { user } = useApplicationContext(applicationContextPropTypes) + const { cobranding, loading, @@ -111,6 +118,7 @@ function EditorNavigationToolbarRoot({ onlineUsers={onlineUsersArray} goToUser={goToUser} isRestrictedTokenMember={isRestrictedTokenMember} + isAnonymousUser={user == null} projectName={projectName} renameProject={renameProject} openShareModal={openShareModal} diff --git a/services/web/frontend/js/features/editor-navigation-toolbar/components/toolbar-header.js b/services/web/frontend/js/features/editor-navigation-toolbar/components/toolbar-header.js index 7450494231..cb0d0ee7fc 100644 --- a/services/web/frontend/js/features/editor-navigation-toolbar/components/toolbar-header.js +++ b/services/web/frontend/js/features/editor-navigation-toolbar/components/toolbar-header.js @@ -28,6 +28,7 @@ function ToolbarHeader({ onlineUsers, goToUser, isRestrictedTokenMember, + isAnonymousUser, projectName, renameProject, openShareModal, @@ -35,6 +36,8 @@ function ToolbarHeader({ pdfButtonIsVisible, togglePdfView, }) { + const shouldDisplayPublishButton = !isAnonymousUser && PublishButton + return (
@@ -68,7 +71,9 @@ function ToolbarHeader({ /> )} - {PublishButton && } + {shouldDisplayPublishButton && ( + + )} {!isRestrictedTokenMember && ( <> - {isAnonymousUser ? ( - children - ) : ( - {children} - )} + {children} diff --git a/services/web/test/frontend/features/editor-navigation-toolbar/components/toolbar-header.test.js b/services/web/test/frontend/features/editor-navigation-toolbar/components/toolbar-header.test.js index f7420c365b..9eec283ba0 100644 --- a/services/web/test/frontend/features/editor-navigation-toolbar/components/toolbar-header.test.js +++ b/services/web/test/frontend/features/editor-navigation-toolbar/components/toolbar-header.test.js @@ -103,4 +103,20 @@ describe('', function () { expect(screen.queryByText('Chat')).to.not.exist }) }) + + describe('Publish button', function () { + it('is displayed by default', function () { + render() + screen.getByText('Submit') + }) + + it('is not displayed for anonymous users', function () { + const props = { + ...defaultProps, + isAnonymousUser: true, + } + render() + expect(screen.queryByText('Submit')).to.not.exist + }) + }) })