Skip to content

Commit

Permalink
Fix reusable block cancelling
Browse files Browse the repository at this point in the history
Pressing Cancel while editing a reusable block should discard any
unsaved changes that have been made to that block.
  • Loading branch information
noisysocks committed Mar 21, 2018
1 parent f4cc3bf commit 0523d71
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions blocks/library/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ReusableBlockEdit extends Component {
this.state = {
isEditing: !! ( reusableBlock && reusableBlock.isTemporary ),
title: null,
attributes: null,
};
}

Expand All @@ -40,46 +41,53 @@ class ReusableBlockEdit extends Component {
}

startEditing() {
const { reusableBlock } = this.props;
const { reusableBlock, block } = this.props;

this.setState( {
isEditing: true,
title: reusableBlock.title,
attributes: block.attributes,
} );
}

stopEditing() {
this.setState( {
isEditing: false,
title: null,
attributes: null,
} );
}

setAttributes( attributes ) {
const { updateAttributes, block } = this.props;
updateAttributes( block.uid, attributes );
this.setState( ( prevState ) => {
if ( prevState.attributes !== null ) {
return { attributes: { ...prevState.attributes, ...attributes } };
}
return null;
} );
}

setTitle( title ) {
this.setState( { title } );
}

save() {
const { reusableBlock, onUpdateTitle, onSave } = this.props;
const { reusableBlock, onUpdateTitle, updateAttributes, block, onSave } = this.props;
const { title, attributes } = this.state;

const { title } = this.state;
if ( title !== reusableBlock.title ) {
onUpdateTitle( title );
}

updateAttributes( block.uid, attributes );
onSave();

this.stopEditing();
}

render() {
const { isSelected, reusableBlock, block, isFetching, isSaving } = this.props;
const { isEditing, title } = this.state;
const { isEditing, title, attributes } = this.state;

if ( ! reusableBlock && isFetching ) {
return <Placeholder><Spinner /></Placeholder>;
Expand All @@ -95,7 +103,7 @@ class ReusableBlockEdit extends Component {
isSelected={ isEditing && isSelected }
id={ block.uid }
name={ block.name }
attributes={ block.attributes }
attributes={ attributes !== null ? attributes : block.attributes }
setAttributes={ isEditing ? this.setAttributes : noop }
/>
);
Expand Down

0 comments on commit 0523d71

Please sign in to comment.