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

Fix reusable block cancelling #5723

Merged
merged 2 commits into from
Mar 26, 2018
Merged

Fix reusable block cancelling #5723

merged 2 commits into from
Mar 26, 2018

Conversation

noisysocks
Copy link
Member

@noisysocks noisysocks commented Mar 21, 2018

dog

Makes it so that pressing Cancel while editing a reusable block discards any unsaved changes that have been made to that block.

This fixes #5719, which is a regression introduced by #5228.

To test:

  1. Insert a reusable block
  2. Select it and click Edit
  3. Make some changes to the block
  4. Click Cancel
  5. The block should appear how it did before you edited it

Pressing Cancel while editing a reusable block should discard any
unsaved changes that have been made to that block.
@noisysocks noisysocks added [Type] Bug An existing feature does not function as intended [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) labels Mar 21, 2018
@noisysocks noisysocks requested a review from aduth March 21, 2018 05:51
@@ -30,6 +30,7 @@ class ReusableBlockEdit extends Component {
this.state = {
isEditing: !! ( reusableBlock && reusableBlock.isTemporary ),
title: null,
attributes: null,
Copy link
Member

Choose a reason for hiding this comment

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

I think the name of the state property could be improved to better indicate that it's the transient updates which have yet to be saved. Otherwise it's a bit difficult to discern the difference between this.state.attributes and this.props.block.attributes.

Copy link
Member Author

Choose a reason for hiding this comment

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

👌 fixed in 5a36e93.

if ( prevState.attributes !== null ) {
return { attributes: { ...prevState.attributes, ...attributes } };
}
return null;
Copy link
Member

Choose a reason for hiding this comment

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

Is an explicit return value necessary here? Or could we drop this line altogether?

Copy link
Member Author

Choose a reason for hiding this comment

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

👌 fixed in 5a36e93.


this.setState( {
isEditing: true,
title: reusableBlock.title,
attributes: block.attributes,
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to track all of the block's original attributes in state? Or could we simply track the state as the object which assigns into block.attributes as the base?

this.setState( {
	attributes: {},
} );

// ... 

const attributes = { ...block.attributes, ...this.state.attributes };

Also has the added advantage that when we call updateAttributes in save, we're only applying the patch changes to state (likely to save a couple iterations when aggregating into state value and maybe avoiding some false positives when overriding state value for undo).

Copy link
Member Author

Choose a reason for hiding this comment

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

👌 fixed in 5a36e93.

Avoid storing all of the referenced block's attributes in state by
storing only the changed attributes, and name this state property
changedAttributes to reflect this and to avoid ambiguity.
@noisysocks noisysocks merged commit bbdab4c into master Mar 26, 2018
@noisysocks noisysocks deleted the fix/reusable-block-cancel branch March 26, 2018 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pressing Cancel while editing a reusable block does not reset the block preview
2 participants