From 707db59458822763de2b9d72bd4c381ee83d4395 Mon Sep 17 00:00:00 2001 From: Sai Jatin K Date: Thu, 2 Jul 2020 17:58:45 +0530 Subject: [PATCH 1/3] refactor-warnIfUnsavedChanges #1458 --- client/modules/IDE/pages/IDEView.jsx | 47 ++++++++++++++-------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/client/modules/IDE/pages/IDEView.jsx b/client/modules/IDE/pages/IDEView.jsx index fbb3dafc..c2e8dc70 100644 --- a/client/modules/IDE/pages/IDEView.jsx +++ b/client/modules/IDE/pages/IDEView.jsx @@ -43,11 +43,29 @@ function isUserOwner(props) { return props.project.owner && props.project.owner.id === props.user.id; } +function warnIfUnsavedChanges(props) { // eslint-disable-line + const { route } = props.route; + if (route && (route.action === 'PUSH' && (route.pathname === '/login' || route.pathname === '/signup'))) { + // don't warn + props.persistState(); + window.onbeforeunload = null; + } else if (route && (props.location.pathname === '/login' || props.location.pathname === '/signup')) { + // don't warn + props.persistState(); + window.onbeforeunload = null; + } else if (props.ide.unsavedChanges) { + if (!window.confirm('Are you sure you want to leave this page? You have unsaved changes.')) { + return false; + } + props.setUnsavedChanges(false); + return true; + } +} + class IDEView extends React.Component { constructor(props) { super(props); this.handleGlobalKeydown = this.handleGlobalKeydown.bind(this); - this.warnIfUnsavedChanges = this.warnIfUnsavedChanges.bind(this); this.state = { consoleSize: props.ide.consoleIsExpanded ? 150 : 29, @@ -71,9 +89,9 @@ class IDEView extends React.Component { this.isMac = navigator.userAgent.toLowerCase().indexOf('mac') !== -1; document.addEventListener('keydown', this.handleGlobalKeydown, false); - this.props.router.setRouteLeaveHook(this.props.route, route => this.warnIfUnsavedChanges(route)); + this.props.router.setRouteLeaveHook(this.props.route, () => warnIfUnsavedChanges(this.props)); - window.onbeforeunload = () => this.warnIfUnsavedChanges(); + window.onbeforeunload = () => warnIfUnsavedChanges(); this.autosaveInterval = null; } @@ -122,7 +140,7 @@ class IDEView extends React.Component { } if (this.props.route.path !== prevProps.route.path) { - this.props.router.setRouteLeaveHook(this.props.route, route => this.warnIfUnsavedChanges(route)); + this.props.router.setRouteLeaveHook(this.props.route, () => warnIfUnsavedChanges(this.props)); } } @@ -186,24 +204,6 @@ class IDEView extends React.Component { } } - warnIfUnsavedChanges(route) { // eslint-disable-line - if (route && (route.action === 'PUSH' && (route.pathname === '/login' || route.pathname === '/signup'))) { - // don't warn - this.props.persistState(); - window.onbeforeunload = null; - } else if (route && (this.props.location.pathname === '/login' || this.props.location.pathname === '/signup')) { - // don't warn - this.props.persistState(); - window.onbeforeunload = null; - } else if (this.props.ide.unsavedChanges) { - if (!window.confirm('Are you sure you want to leave this page? You have unsaved changes.')) { - return false; - } - this.props.setUnsavedChanges(false); - return true; - } - } - render() { return (
@@ -212,7 +212,7 @@ class IDEView extends React.Component { {this.props.toast.isVisible && }