@@ -43,11 +43,29 @@ function isUserOwner(props) {
43
43
return props . project . owner && props . project . owner . id === props . user . id ;
44
44
}
45
45
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
+
46
65
class IDEView extends React . Component {
47
66
constructor ( props ) {
48
67
super ( props ) ;
49
68
this . handleGlobalKeydown = this . handleGlobalKeydown . bind ( this ) ;
50
- this . warnIfUnsavedChanges = this . warnIfUnsavedChanges . bind ( this ) ;
51
69
52
70
this . state = {
53
71
consoleSize : props . ide . consoleIsExpanded ? 150 : 29 ,
@@ -71,9 +89,9 @@ class IDEView extends React.Component {
71
89
this . isMac = navigator . userAgent . toLowerCase ( ) . indexOf ( 'mac' ) !== - 1 ;
72
90
document . addEventListener ( 'keydown' , this . handleGlobalKeydown , false ) ;
73
91
74
- this . props . router . setRouteLeaveHook ( this . props . route , route => this . warnIfUnsavedChanges ( route ) ) ;
92
+ this . props . router . setRouteLeaveHook ( this . props . route , ( ) => warnIfUnsavedChanges ( this . props ) ) ;
75
93
76
- window . onbeforeunload = ( ) => this . warnIfUnsavedChanges ( ) ;
94
+ window . onbeforeunload = ( ) => warnIfUnsavedChanges ( ) ;
77
95
78
96
this . autosaveInterval = null ;
79
97
}
@@ -122,7 +140,7 @@ class IDEView extends React.Component {
122
140
}
123
141
124
142
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 ) ) ;
126
144
}
127
145
}
128
146
@@ -186,24 +204,6 @@ class IDEView extends React.Component {
186
204
}
187
205
}
188
206
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
-
207
207
render ( ) {
208
208
return (
209
209
< div className = "ide" >
@@ -212,7 +212,7 @@ class IDEView extends React.Component {
212
212
</ Helmet >
213
213
{ this . props . toast . isVisible && < Toast /> }
214
214
< Nav
215
- warnIfUnsavedChanges = { this . warnIfUnsavedChanges }
215
+ warnIfUnsavedChanges = { ( ) => warnIfUnsavedChanges ( this . props ) }
216
216
cmController = { this . cmController }
217
217
/>
218
218
< Toolbar />
@@ -604,7 +604,6 @@ IDEView.propTypes = {
604
604
showErrorModal : PropTypes . func . isRequired ,
605
605
hideErrorModal : PropTypes . func . isRequired ,
606
606
clearPersistedState : PropTypes . func . isRequired ,
607
- persistState : PropTypes . func . isRequired ,
608
607
showRuntimeErrorWarning : PropTypes . func . isRequired ,
609
608
hideRuntimeErrorWarning : PropTypes . func . isRequired ,
610
609
startSketch : PropTypes . func . isRequired ,
0 commit comments