Skip to content

Commit 707db59

Browse files
committed
refactor-warnIfUnsavedChanges processing#1458
1 parent 69bd6cb commit 707db59

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

client/modules/IDE/pages/IDEView.jsx

+23-24
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,29 @@ function isUserOwner(props) {
4343
return props.project.owner && props.project.owner.id === props.user.id;
4444
}
4545

46+
function warnIfUnsavedChanges(props) { // eslint-disable-line
47+
const { route } = props.route;
48+
if (route && (route.action === 'PUSH' && (route.pathname === '/login' || route.pathname === '/signup'))) {
49+
// don't warn
50+
props.persistState();
51+
window.onbeforeunload = null;
52+
} else if (route && (props.location.pathname === '/login' || props.location.pathname === '/signup')) {
53+
// don't warn
54+
props.persistState();
55+
window.onbeforeunload = null;
56+
} else if (props.ide.unsavedChanges) {
57+
if (!window.confirm('Are you sure you want to leave this page? You have unsaved changes.')) {
58+
return false;
59+
}
60+
props.setUnsavedChanges(false);
61+
return true;
62+
}
63+
}
64+
4665
class IDEView extends React.Component {
4766
constructor(props) {
4867
super(props);
4968
this.handleGlobalKeydown = this.handleGlobalKeydown.bind(this);
50-
this.warnIfUnsavedChanges = this.warnIfUnsavedChanges.bind(this);
5169

5270
this.state = {
5371
consoleSize: props.ide.consoleIsExpanded ? 150 : 29,
@@ -71,9 +89,9 @@ class IDEView extends React.Component {
7189
this.isMac = navigator.userAgent.toLowerCase().indexOf('mac') !== -1;
7290
document.addEventListener('keydown', this.handleGlobalKeydown, false);
7391

74-
this.props.router.setRouteLeaveHook(this.props.route, route => this.warnIfUnsavedChanges(route));
92+
this.props.router.setRouteLeaveHook(this.props.route, () => warnIfUnsavedChanges(this.props));
7593

76-
window.onbeforeunload = () => this.warnIfUnsavedChanges();
94+
window.onbeforeunload = () => warnIfUnsavedChanges();
7795

7896
this.autosaveInterval = null;
7997
}
@@ -122,7 +140,7 @@ class IDEView extends React.Component {
122140
}
123141

124142
if (this.props.route.path !== prevProps.route.path) {
125-
this.props.router.setRouteLeaveHook(this.props.route, route => this.warnIfUnsavedChanges(route));
143+
this.props.router.setRouteLeaveHook(this.props.route, () => warnIfUnsavedChanges(this.props));
126144
}
127145
}
128146

@@ -186,24 +204,6 @@ class IDEView extends React.Component {
186204
}
187205
}
188206

189-
warnIfUnsavedChanges(route) { // eslint-disable-line
190-
if (route && (route.action === 'PUSH' && (route.pathname === '/login' || route.pathname === '/signup'))) {
191-
// don't warn
192-
this.props.persistState();
193-
window.onbeforeunload = null;
194-
} else if (route && (this.props.location.pathname === '/login' || this.props.location.pathname === '/signup')) {
195-
// don't warn
196-
this.props.persistState();
197-
window.onbeforeunload = null;
198-
} else if (this.props.ide.unsavedChanges) {
199-
if (!window.confirm('Are you sure you want to leave this page? You have unsaved changes.')) {
200-
return false;
201-
}
202-
this.props.setUnsavedChanges(false);
203-
return true;
204-
}
205-
}
206-
207207
render() {
208208
return (
209209
<div className="ide">
@@ -212,7 +212,7 @@ class IDEView extends React.Component {
212212
</Helmet>
213213
{this.props.toast.isVisible && <Toast />}
214214
<Nav
215-
warnIfUnsavedChanges={this.warnIfUnsavedChanges}
215+
warnIfUnsavedChanges={() => warnIfUnsavedChanges(this.props)}
216216
cmController={this.cmController}
217217
/>
218218
<Toolbar />
@@ -604,7 +604,6 @@ IDEView.propTypes = {
604604
showErrorModal: PropTypes.func.isRequired,
605605
hideErrorModal: PropTypes.func.isRequired,
606606
clearPersistedState: PropTypes.func.isRequired,
607-
persistState: PropTypes.func.isRequired,
608607
showRuntimeErrorWarning: PropTypes.func.isRequired,
609608
hideRuntimeErrorWarning: PropTypes.func.isRequired,
610609
startSketch: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)