Overwrite changes when using sidebar

This commit is contained in:
Mike Jolley 2019-05-24 12:01:24 +01:00
parent 0d3bce262f
commit bff9b8bf00
1 changed files with 12 additions and 8 deletions

View File

@ -63,15 +63,13 @@ class ProductByCategoryBlock extends Component {
stopEditing() { stopEditing() {
this.setState( { this.setState( {
isEditing: false, isEditing: false,
changedAttributes: null, changedAttributes: {},
} ); } );
} }
setChangedAttributes( attributes ) { setChangedAttributes( attributes ) {
this.setState( ( prevState ) => { this.setState( ( prevState ) => {
if ( prevState.changedAttributes !== null ) { return { changedAttributes: { ...prevState.changedAttributes, ...attributes } };
return { changedAttributes: { ...prevState.changedAttributes, ...attributes } };
}
} ); } );
} }
@ -133,12 +131,18 @@ class ProductByCategoryBlock extends Component {
selected={ attributes.categories } selected={ attributes.categories }
onChange={ ( value = [] ) => { onChange={ ( value = [] ) => {
const ids = value.map( ( { id } ) => id ); const ids = value.map( ( { id } ) => id );
setAttributes( { categories: ids } ); const changes = { categories: ids };
// Changes in the sidebar save instantly and overwrite any unsaved changes.
setAttributes( changes );
this.setChangedAttributes( changes );
} } } }
operator={ catOperator } operator={ catOperator }
onOperatorChange={ ( value = 'any' ) => onOperatorChange={ ( value = 'any' ) => {
setAttributes( { catOperator: value } ) const changes = { catOperator: value };
} setAttributes( changes );
this.setChangedAttributes( changes );
} }
/> />
</PanelBody> </PanelBody>
<PanelBody <PanelBody