Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt before exiting tab with unsaved changes #815

Merged
merged 1 commit into from
Nov 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/containers/EntryPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,24 @@ class EntryPage extends React.Component {
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')) {
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.
// (The confirmation above will run first.)
Expand Down Expand Up @@ -93,6 +106,7 @@ class EntryPage extends React.Component {

componentWillUnmount() {
this.props.discardDraft();
window.removeEventListener('beforeunload', this.exitBlocker);
}

createDraft = (entry) => {
Expand Down