From 167828b8722a2aaf9264fe54f0c466dcf0af6945 Mon Sep 17 00:00:00 2001 From: Cassie Tarakajian Date: Thu, 10 Nov 2016 18:49:42 -0500 Subject: [PATCH] fix #185 --- client/modules/IDE/actions/project.js | 8 ++++++-- client/modules/IDE/pages/IDEView.jsx | 27 ++++++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/client/modules/IDE/actions/project.js b/client/modules/IDE/actions/project.js index d2de3c37..2659b4d5 100644 --- a/client/modules/IDE/actions/project.js +++ b/client/modules/IDE/actions/project.js @@ -136,13 +136,17 @@ export function exportProjectAsZip(projectId) { win.focus(); } -export function newProject() { - browserHistory.push('/'); +export function resetProject() { return { type: ActionTypes.RESET_PROJECT }; } +export function newProject() { + browserHistory.push('/'); + return resetProject(); +} + export function cloneProject() { return (dispatch, getState) => { const state = getState(); diff --git a/client/modules/IDE/pages/IDEView.jsx b/client/modules/IDE/pages/IDEView.jsx index 048db7fd..2876f49c 100644 --- a/client/modules/IDE/pages/IDEView.jsx +++ b/client/modules/IDE/pages/IDEView.jsx @@ -45,7 +45,9 @@ class IDEView extends React.Component { this.props.stopSketch(); if (this.props.params.project_id) { const id = this.props.params.project_id; - this.props.getProject(id); + if (id !== this.props.project.id) { + this.props.getProject(id); + } // if autosave is on and the user is the owner of the project if (this.props.preferences.autosave @@ -62,7 +64,7 @@ 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, () => this.warnIfUnsavedChanges()); + this.props.router.setRouteLeaveHook(this.props.route, (route) => this.warnIfUnsavedChanges(route)); window.onbeforeunload = () => this.warnIfUnsavedChanges(); @@ -85,7 +87,13 @@ class IDEView extends React.Component { } if (nextProps.params.project_id && !this.props.params.project_id) { - this.props.getProject(nextProps.params.project_id); + if (nextProps.params.project_id !== nextProps.project.id) { + this.props.getProject(nextProps.params.project_id); + } + } + + if (!nextProps.params.project_id && this.props.params.project_id) { + this.props.resetProject(); } if (nextProps.preferences.theme !== this.props.preferences.theme) { @@ -115,7 +123,7 @@ class IDEView extends React.Component { } if (this.props.route.path !== prevProps.route.path) { - this.props.router.setRouteLeaveHook(this.props.route, () => this.warnIfUnsavedChanges()); + this.props.router.setRouteLeaveHook(this.props.route, (route) => this.warnIfUnsavedChanges(route)); } } @@ -168,8 +176,12 @@ class IDEView extends React.Component { } } - warnIfUnsavedChanges() { // eslint-disable-line - if (this.props.ide.unsavedChanges) { + warnIfUnsavedChanges(route) { // eslint-disable-line + if (route && (route.action === 'PUSH' && (route.pathname === '/login' || route.pathname === '/signup'))) { + // don't warn + } else if (route && this.props.location.pathname === '/login' || this.props.location.pathname === '/signup') { + // don't warn + } else if (this.props.ide.unsavedChanges) { if (!window.confirm('Are you sure you want to leave this page? You have unsaved changes.')) { return false; } @@ -591,7 +603,8 @@ IDEView.propTypes = { endSketchRefresh: PropTypes.func.isRequired, startRefreshSketch: PropTypes.func.isRequired, setBlobUrl: PropTypes.func.isRequired, - setPreviousPath: PropTypes.func.isRequired + setPreviousPath: PropTypes.func.isRequired, + resetProject: PropTypes.func.isRequired }; function mapStateToProps(state) {