Skip to content

Commit

Permalink
Add AYS dialog when leaving editor in dirty state
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jul 11, 2017
1 parent e74afaf commit 11dcf8b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
13 changes: 13 additions & 0 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import 'moment-timezone/moment-timezone-utils';
import { parse } from 'blocks';
import { render } from 'element';
import { settings } from 'date';
import { __ } from 'i18n';

/**
* Internal dependencies
*/
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 );
Expand Down Expand Up @@ -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(
<ReduxProvider store={ store }>
<SlotFillProvider>
Expand Down
1 change: 1 addition & 0 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit 11dcf8b

Please sign in to comment.