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

Remove entity edits when deleting an entity #36030

Closed
wants to merge 3 commits 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
14 changes: 14 additions & 0 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ function entity( entityConfig ) {
...state,
[ action.recordId ]: nextEdits,
};
case 'REMOVE_ITEMS':
// Remove edits for deleted items.
const updatedEdits = {
...state,
};
for ( const recordId of action.itemIds ) {
delete updatedEdits[ recordId ];
}
return updatedEdits;
}

return state;
Expand Down Expand Up @@ -498,6 +507,11 @@ export function undo( state = UNDO_INITIAL_STATE, action ) {
} );
}
return nextState;
case 'REMOVE_ITEMS':
// Remove undo records for a deleted entity.
return state.filter(
( undoRecord ) => undoRecord.recordId !== action.recordId
);
Comment on lines +512 to +514
Copy link
Contributor Author

@talldan talldan Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but unfortunately after the removal a new edit is pushed onto the stack for the deleted entity. It isn't undo-able though, the undo button is greyed out. Not sure why, but at least it prevents a buggy state.

I think we also need to avoid pushing edits on the stack for removed items to solve this.

}

return state;
Expand Down
11 changes: 11 additions & 0 deletions packages/core-data/src/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ describe( 'undo', () => {
};
} else if ( args[ 0 ] === 'isCreate' ) {
action = { type: 'CREATE_UNDO_LEVEL' };
} else if ( args[ 0 ] === 'isRemoveItems' ) {
action = { type: 'REMOVE_ITEMS', ...createEditActionPart() };
} else if ( args.length ) {
action = createNextEditAction( ...args );
}
Expand Down Expand Up @@ -343,6 +345,15 @@ describe( 'undo', () => {
expectedUndoState.push( createEditActionPart( { value } ) );
expect( undoState ).toEqual( expectedUndoState );
} );

it( 'removes undo records when an entity is deleted', () => {
undoState = createNextUndoState();
undoState = createNextUndoState( { value: 1 } );
undoState = createNextUndoState( { value: 2 } );
undoState = createNextUndoState( { value: 3 } );
undoState = createNextUndoState( 'isRemoveItems' );
expect( undoState ).toEqual( [] );
} );
Comment on lines +349 to +356
Copy link
Contributor Author

@talldan talldan Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test would ideally push a different record onto the undo stack to assert that only the deleted record is removed, but it took me so long to understand how these tests work, and it looks like the whole 'framework' for these tests would need to be re-written to support more than one record.

Maybe after WordPress 5.9 I'll take some time to look at it, but right now time is something I don't have.

} );

describe( 'embedPreviews()', () => {
Expand Down