Skip to content

Commit

Permalink
Editor: Refactor dirtiness condition on pending transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Apr 25, 2018
1 parent cd001fb commit 978f8d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
24 changes: 23 additions & 1 deletion editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function isEditedPostNew( state ) {
* @return {boolean} Whether unsaved values exist.
*/
export function isEditedPostDirty( state ) {
return state.editor.isDirty || isSavingPost( state );
return state.editor.isDirty || inSomeHistory( state, isEditedPostDirty );
}

/**
Expand Down Expand Up @@ -1568,3 +1568,25 @@ export function getPermalinkParts( state ) {
suffix,
};
}

/**
* Returns true if an optimistic transaction is pending commit, for which the
* before state satisfies the given predicate function.
*
* @param {Object} state Editor state.
* @param {Function} predicate Function given state, returning true if match.
*
* @return {boolean} Whether predicate matches for some history.
*/
export function inSomeHistory( state, predicate ) {
const { optimist } = state;

// In recursion, optimist state won't exist. Assume exhausted options.
if ( ! optimist ) {
return false;
}

return optimist.some( ( { beforeState } ) => (
beforeState && predicate( beforeState )
) );
}
14 changes: 10 additions & 4 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,20 @@ describe( 'selectors', () => {
expect( isEditedPostDirty( state ) ).toBe( true );
} );

it( 'should return true if mid-save', () => {
it( 'should return true if pending transaction with dirty state', () => {
const state = {
optimist: [
{
beforeState: {
editor: {
isDirty: true,
},
},
},
],
editor: {
isDirty: false,
},
saving: {
requesting: true,
},
};

expect( isEditedPostDirty( state ) ).toBe( true );
Expand Down

0 comments on commit 978f8d5

Please sign in to comment.