Skip to content

Commit afe74e9

Browse files
authored
Merge pull request #1488 from saijatin28/refactorIDEView
refactor-warnIfUnsavedChanges #1458
2 parents 97c3b35 + decf4a9 commit afe74e9

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

Diff for: client/modules/IDE/pages/IDEView.jsx

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

47+
function warnIfUnsavedChanges(props) { // eslint-disable-line
48+
const { route } = props.route;
49+
if (route && (route.action === 'PUSH' && (route.pathname === '/login' || route.pathname === '/signup'))) {
50+
// don't warn
51+
props.persistState();
52+
window.onbeforeunload = null;
53+
} else if (route && (props.location.pathname === '/login' || props.location.pathname === '/signup')) {
54+
// don't warn
55+
props.persistState();
56+
window.onbeforeunload = null;
57+
} else if (props.ide.unsavedChanges) {
58+
if (!window.confirm(props.t('WarningUnsavedChanges'))) {
59+
return false;
60+
}
61+
props.setUnsavedChanges(false);
62+
return true;
63+
}
64+
}
65+
4766
class IDEView extends React.Component {
4867
constructor(props) {
4968
super(props);
5069
this.handleGlobalKeydown = this.handleGlobalKeydown.bind(this);
51-
this.warnIfUnsavedChanges = this.warnIfUnsavedChanges.bind(this);
5270

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

75-
this.props.router.setRouteLeaveHook(this.props.route, route => this.warnIfUnsavedChanges(route));
93+
this.props.router.setRouteLeaveHook(this.props.route, this.handleUnsavedChanges);
7694

77-
window.onbeforeunload = () => this.warnIfUnsavedChanges();
95+
window.onbeforeunload = this.handleUnsavedChanges;
7896

7997
this.autosaveInterval = null;
8098
}
@@ -123,7 +141,7 @@ class IDEView extends React.Component {
123141
}
124142

125143
if (this.props.route.path !== prevProps.route.path) {
126-
this.props.router.setRouteLeaveHook(this.props.route, route => this.warnIfUnsavedChanges(route));
144+
this.props.router.setRouteLeaveHook(this.props.route, () => warnIfUnsavedChanges(this.props));
127145
}
128146
}
129147

@@ -187,23 +205,7 @@ class IDEView extends React.Component {
187205
}
188206
}
189207

190-
warnIfUnsavedChanges(route) { // eslint-disable-line
191-
if (route && (route.action === 'PUSH' && (route.pathname === '/login' || route.pathname === '/signup'))) {
192-
// don't warn
193-
this.props.persistState();
194-
window.onbeforeunload = null;
195-
} else if (route && (this.props.location.pathname === '/login' || this.props.location.pathname === '/signup')) {
196-
// don't warn
197-
this.props.persistState();
198-
window.onbeforeunload = null;
199-
} else if (this.props.ide.unsavedChanges) {
200-
if (!window.confirm(this.props.t('WarningUnsavedChanges'))) {
201-
return false;
202-
}
203-
this.props.setUnsavedChanges(false);
204-
return true;
205-
}
206-
}
208+
handleUnsavedChanges = () => warnIfUnsavedChanges(this.props);
207209

208210
render() {
209211
return (
@@ -213,7 +215,7 @@ class IDEView extends React.Component {
213215
</Helmet>
214216
{this.props.toast.isVisible && <Toast />}
215217
<Nav
216-
warnIfUnsavedChanges={this.warnIfUnsavedChanges}
218+
warnIfUnsavedChanges={this.handleUnsavedChanges}
217219
cmController={this.cmController}
218220
/>
219221
<Toolbar key={this.props.project.id} />
@@ -605,7 +607,6 @@ IDEView.propTypes = {
605607
showErrorModal: PropTypes.func.isRequired,
606608
hideErrorModal: PropTypes.func.isRequired,
607609
clearPersistedState: PropTypes.func.isRequired,
608-
persistState: PropTypes.func.isRequired,
609610
showRuntimeErrorWarning: PropTypes.func.isRequired,
610611
hideRuntimeErrorWarning: PropTypes.func.isRequired,
611612
startSketch: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)