From 11dcf8b555c6db7b21088425888e277190af7d30 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sat, 8 Jul 2017 13:42:01 -0700 Subject: [PATCH] Add AYS dialog when leaving editor in dirty state --- editor/effects.js | 12 ++++++++---- editor/index.js | 13 +++++++++++++ editor/state.js | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/editor/effects.js b/editor/effects.js index 948c8ead3163c..857ccb818072e 100644 --- a/editor/effects.js +++ b/editor/effects.js @@ -141,10 +141,14 @@ export default { }, TRASH_POST_SUCCESS( action ) { const { postId, postType } = action; - window.location.href = getWPAdminURL( 'edit.php', { - trashed: 1, - post_type: postType, - ids: postId, + + // Delay redirect to ensure store has been updated with the successful trash. + setTimeout( () => { + window.location.href = getWPAdminURL( 'edit.php', { + trashed: 1, + post_type: postType, + ids: postId, + } ); } ); }, MERGE_BLOCKS( action, store ) { diff --git a/editor/index.js b/editor/index.js index 9d75de3991580..3d33e29e4c58d 100644 --- a/editor/index.js +++ b/editor/index.js @@ -13,6 +13,7 @@ import 'moment-timezone/moment-timezone-utils'; import { parse } from 'blocks'; import { render } from 'element'; import { settings } from 'date'; +import { __ } from 'i18n'; /** * Internal dependencies @@ -20,6 +21,9 @@ import { settings } from 'date'; import './assets/stylesheets/main.scss'; import Layout from './layout'; import { createReduxStore } from './state'; +import { + isEditedPostDirty, +} from './selectors'; // Configure moment globally moment.locale( settings.l10n.locale ); @@ -82,6 +86,15 @@ export function createEditorInstance( id, post ) { preparePostState( store, post ); + const warnDirtyBeforeUnload = ( event ) => { + const state = store.getState(); + if ( isEditedPostDirty( state ) ) { + event.returnValue = __( 'You have unsaved changes. If you proceed, they will be lost.' ); + return event.returnValue; + } + }; + window.addEventListener( 'beforeunload', warnDirtyBeforeUnload ); + render( diff --git a/editor/state.js b/editor/state.js index 97c41a0226c43..49fdac52558f6 100644 --- a/editor/state.js +++ b/editor/state.js @@ -64,6 +64,7 @@ export const editor = combineUndoableReducers( { switch ( action.type ) { case 'RESET_BLOCKS': case 'REQUEST_POST_UPDATE_SUCCESS': + case 'TRASH_POST_SUCCESS': return false; case 'UPDATE_BLOCK':