11import React , { PropTypes , Component } from 'react' ;
22import { connect } from 'react-redux' ;
33import { bindActionCreators } from 'redux' ;
4- import { browserHistory } from 'react-router' ;
4+ import { browserHistory , withRouter } from 'react-router' ;
55import { Link } from 'react-router' ;
66import _ from 'underscore' ;
77
@@ -31,7 +31,8 @@ export class DocumentEdit extends Component {
3131 }
3232
3333 componentDidMount ( ) {
34- const { fetchDocument, params } = this . props ;
34+ const { fetchDocument, params, router, route } = this . props ;
35+ router . setRouteLeaveHook ( route , this . routerWillLeave . bind ( this ) ) ;
3536 fetchDocument ( params . collection_name , params . id ) ;
3637 }
3738
@@ -49,6 +50,12 @@ export class DocumentEdit extends Component {
4950 }
5051 }
5152
53+ routerWillLeave ( nextLocation ) {
54+ const { fieldChanged } = this . props ;
55+ if ( fieldChanged )
56+ return 'You have unsaved changes on this page. Are you sure you want to leave?' ;
57+ }
58+
5259 handleClickSave ( id , collection ) {
5360 const { putDocument, fieldChanged } = this . props ;
5461 if ( fieldChanged ) {
@@ -151,7 +158,9 @@ DocumentEdit.propTypes = {
151158 errors : PropTypes . array . isRequired ,
152159 updated : PropTypes . bool . isRequired ,
153160 fieldChanged : PropTypes . bool . isRequired ,
154- params : PropTypes . object . isRequired
161+ params : PropTypes . object . isRequired ,
162+ router : PropTypes . object . isRequired ,
163+ route : PropTypes . object . isRequired
155164} ;
156165
157166function mapStateToProps ( state ) {
@@ -178,4 +187,4 @@ function mapDispatchToProps(dispatch) {
178187 } , dispatch ) ;
179188}
180189
181- export default connect ( mapStateToProps , mapDispatchToProps ) ( DocumentEdit ) ;
190+ export default withRouter ( connect ( mapStateToProps , mapDispatchToProps ) ( DocumentEdit ) ) ;
0 commit comments