Prompt before exiting tab with unsaved changes

This commit is contained in:
Benaiah Mischenko 2017-11-13 07:10:59 -08:00 committed by Shawn Erquhart
parent 2a9697db0a
commit aa813e8569

View File

@ -59,11 +59,24 @@ class EntryPage extends React.Component {
loadEntry(collection, slug); loadEntry(collection, slug);
} }
const unblock = history.block((location) => { const leaveMessage = 'Are you sure you want to leave this page?';
this.exitBlocker = (event) => {
if (this.props.entryDraft.get('hasChanged')) { if (this.props.entryDraft.get('hasChanged')) {
return "Are you sure you want to leave this page?"; // This message is ignored in most browsers, but its presence
// triggers the confirmation dialog
event.returnValue = leaveMessage;
return leaveMessage;
} }
}); };
window.addEventListener('beforeunload', this.exitBlocker);
const navigationBlocker = () => {
if (this.props.entryDraft.get('hasChanged')) {
return leaveMessage;
}
};
const unblock = history.block(navigationBlocker);
// This will run as soon as the location actually changes. // This will run as soon as the location actually changes.
// (The confirmation above will run first.) // (The confirmation above will run first.)
@ -93,6 +106,7 @@ class EntryPage extends React.Component {
componentWillUnmount() { componentWillUnmount() {
this.props.discardDraft(); this.props.discardDraft();
window.removeEventListener('beforeunload', this.exitBlocker);
} }
createDraft = (entry) => { createDraft = (entry) => {