Skip to content

Commit

Permalink
Get up-to-date logoBlockCount when removing block
Browse files Browse the repository at this point in the history
Previously we were getting the logoBlockCount on block creation, but
this value could become out of date if more Site Logo blocks were added
afterward.

Now we count the number of blocks at the time of block removal, so that it
is guaranteed up-to-date, and we ensure that changes are only discarded
if there are no other Site Logo blocks on the page.
  • Loading branch information
stacimc committed Dec 20, 2021
1 parent a8a28c9 commit e3d32bf
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,10 @@ export default function LogoEdit( {
siteIconId,
mediaItemData,
isRequestingMediaItem,
logoBlockCount,
} = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEditedEntityRecord } = select(
coreStore
);
const { getGlobalBlockCount } = select( blockEditorStore );
const siteSettings = getEditedEntityRecord( 'root', 'site' );
const siteData = getEntityRecord( 'root', '__unstableBase' );
const _siteLogo = siteSettings?.site_logo;
Expand Down Expand Up @@ -404,10 +402,10 @@ export default function LogoEdit( {
},
isRequestingMediaItem: _isRequestingMediaItem,
siteIconId: _siteIconId,
logoBlockCount: getGlobalBlockCount( 'core/site-logo' ),
};
}, [] );

const { getGlobalBlockCount } = useSelect( blockEditorStore );
const { editEntityRecord } = useDispatch( coreStore );

useEffect( () => {
Expand All @@ -427,9 +425,11 @@ export default function LogoEdit( {
return;
}

const logoBlockCount = getGlobalBlockCount( 'core/site-logo' );

// Only discard unsaved changes if we are removing the last Site Logo block
// on the page.
if ( logoBlockCount === 1 ) {
if ( logoBlockCount === 0 ) {
editEntityRecord( 'root', 'site', undefined, {
site_logo: undefined,
site_icon: undefined,
Expand Down

0 comments on commit e3d32bf

Please sign in to comment.