From 074334e14aee62ff3b7aff76bc49e26ee094932d Mon Sep 17 00:00:00 2001 From: ghalestrilo Date: Thu, 13 Aug 2020 18:57:42 -0300 Subject: [PATCH] :bug: fix hook not detecting user authenticated --- client/modules/App/App.jsx | 3 +-- client/modules/IDE/pages/MobileIDEView.jsx | 8 +++++--- client/utils/custom-hooks.js | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/client/modules/App/App.jsx b/client/modules/App/App.jsx index 1da42325..af441a9d 100644 --- a/client/modules/App/App.jsx +++ b/client/modules/App/App.jsx @@ -34,8 +34,7 @@ class App extends React.Component { render() { return (
- {false && - this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && } + {this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && } {this.props.children}
); diff --git a/client/modules/IDE/pages/MobileIDEView.jsx b/client/modules/IDE/pages/MobileIDEView.jsx index 2aa653b2..0ecdfc14 100644 --- a/client/modules/IDE/pages/MobileIDEView.jsx +++ b/client/modules/IDE/pages/MobileIDEView.jsx @@ -73,6 +73,9 @@ const getNatOptions = (username = undefined) => const isUserOwner = ({ project, user }) => project && project.owner && project.owner.id === user.id; +const canSaveProject = (project, user) => + isUserOwner({ project, user }) || (user.authenticated && !project.owner); + // TODO: This could go into const handleGlobalKeydown = (props, cmController) => (e) => { const { @@ -113,7 +116,7 @@ const handleGlobalKeydown = (props, cmController) => (e) => { // 83 === s e.preventDefault(); e.stopPropagation(); - if (isUserOwner(props) || (user.authenticated && !project.owner)) saveProject(cmController.getContent()); + if (canSaveProject(props)) saveProject(cmController.getContent()); else if (user.authenticated) cloneProject(); else showErrorModal('forceAuthentication'); @@ -205,8 +208,7 @@ const MobileIDEView = (props) => { autosaveProject, preferences, ide, selectedFile }); - // useEventListener('keydown', () => alert('haha')); - useEventListener('keydown', handleGlobalKeydown(props, cmController)); + useEventListener('keydown', handleGlobalKeydown(props, cmController), false, [props]); return ( diff --git a/client/utils/custom-hooks.js b/client/utils/custom-hooks.js index 7543555c..b0981ae0 100644 --- a/client/utils/custom-hooks.js +++ b/client/utils/custom-hooks.js @@ -53,7 +53,7 @@ export const useEffectWithComparison = (fn, props) => { }, Object.values(props)); }; -export const useEventListener = (event, callback, useCapture = false) => useEffect(() => { +export const useEventListener = (event, callback, useCapture = false, list = []) => useEffect(() => { document.addEventListener(event, callback, useCapture); return () => document.removeEventListener(event, callback, useCapture); -}, []); +}, list);