Skip to content

Commit

Permalink
Prompt before exiting tab with unsaved changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Benaiah committed Nov 13, 2017
1 parent 1acdb8d commit 03ded66
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/containers/EntryPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ class EntryPage extends React.Component {
loadEntry(collection, slug);
}

const unblock = history.block((location) => {
this.blocker = () => {
if (this.props.entryDraft.get('hasChanged')) {
return "Are you sure you want to leave this page?";
}
});
}

const unblock = history.block(this.blocker);
window.onbeforeunload = window.onbeforeunload || this.blocker;

// This will run as soon as the location actually changes.
// (The confirmation above will run first.)
Expand Down Expand Up @@ -93,6 +96,9 @@ class EntryPage extends React.Component {

componentWillUnmount() {
this.props.discardDraft();
if (window.onbeforeunload === this.blocker) {
window.onbeforeunload = undefined
}
}

createDraft = (entry) => {
Expand Down

0 comments on commit 03ded66

Please sign in to comment.