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

Chrome: Clear Post edits on save success #1092

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export default {
toSend.id = postId;
}

dispatch( { type: 'CLEAR_POST_EDITS' } );
new wp.api.models.Post( toSend ).save().done( ( newPost ) => {
dispatch( {
type: 'REQUEST_POST_UPDATE_SUCCESS',
post: newPost,
isNew,
} );
dispatch( { type: 'CLEAR_POST_EDITS', edits } );
} ).fail( ( err ) => {
dispatch( {
type: 'REQUEST_POST_UPDATE_FAILURE',
Expand Down
14 changes: 11 additions & 3 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ export const editor = combineUndoableReducers( {
}, state );

case 'CLEAR_POST_EDITS':
// Don't return a new object if there's not any edits
if ( ! Object.keys( state ).length ) {
// if we made other edits while saving, these new edits are kept in the store.
const keysToClear = reduce( action.edits, ( memo, value, key ) => {
if ( value === state[ key ] ) {
memo.push( key );
}
return memo;
}, [] );

// Don't return a new object if there are no keys to clear
if ( ! keysToClear.length ) {
return state;
}

return {};
return omit( state, keysToClear );
}

return state;
Expand Down
10 changes: 8 additions & 2 deletions editor/test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,21 @@ describe( 'state', () => {

const state = editor( original, {
type: 'CLEAR_POST_EDITS',
edits: {
status: 'draft',
title: 'post title',
},
} );

expect( state.edits ).to.eql( {} );
expect( state.edits ).to.eql( { tags: [ 1 ] } );
} );

it( 'should return same reference if clearing non-edited', () => {
const original = editor( undefined, {
type: 'EDIT_POST',
edits: {},
edits: {
status: 'draft',
},
} );

const state = editor( original, {
Expand Down