Skip to content

Instantly share code, notes, and snippets.

@tomlagier
Created March 15, 2016 02:10
Show Gist options
  • Select an option

  • Save tomlagier/ded50cfe056d947f1371 to your computer and use it in GitHub Desktop.

Select an option

Save tomlagier/ded50cfe056d947f1371 to your computer and use it in GitHub Desktop.
import { showSaveModal } from '../../controllers/saveModalController';
import store from '../../store';
...
MyComponent extends React.Component<P, S> {
public context: {
router: any
};
static contextTypes = {
router: React.PropTypes.object.isRequired
}
componentDidMount() {
//NOTE: Component needs props passed down from parent connected component, or change this.props.route to
//this.props.location.pathname
this.context.router.setRouteLeaveHook(this.props.route, this.routerWillLeave.bind(this))
}
routerWillLeave(nextLocation) {
if (store.getState().addGuestForm.form.dirty && !store.getState().saveModal.confirmed) {
showSaveModal({
//This is the function to trigger when we click 'save'. The 3rd boolean is a flag to tell
//the save functions to bubble errors. I didn't find a better way to notify the modal of errors
//while not disrupting existing behavior
saveFn: saveGuestFromForm.bind(this, this.props.addGuestForm, true),
//Form to reset when modal actions are taken - successful save or navigation away
form: 'addGuestForm.model',
redirect: nextLocation.pathname
})
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment