Skip to content

Commit

Permalink
Store changedAttributes instead of attributes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
noisysocks committed Mar 22, 2018
1 parent 0523d71 commit 5a36e93
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions blocks/library/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ReusableBlockEdit extends Component {
this.state = {
isEditing: !! ( reusableBlock && reusableBlock.isTemporary ),
title: null,
attributes: null,
changedAttributes: null,
};
}

Expand All @@ -41,29 +41,28 @@ class ReusableBlockEdit extends Component {
}

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

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

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

setAttributes( attributes ) {
this.setState( ( prevState ) => {
if ( prevState.attributes !== null ) {
return { attributes: { ...prevState.attributes, ...attributes } };
if ( prevState.changedAttributes !== null ) {
return { changedAttributes: { ...prevState.changedAttributes, ...attributes } };
}
return null;
} );
}

Expand All @@ -73,21 +72,21 @@ class ReusableBlockEdit extends Component {

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

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

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

this.stopEditing();
}

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

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

0 comments on commit 5a36e93

Please sign in to comment.