Skip to content

Commit

Permalink
Site Editor: Add template part missing state (#28277)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-szabo97 authored Jan 19, 2021
1 parent 6eb5445 commit 0585c2f
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BlockControls,
InspectorAdvancedControls,
useBlockProps,
Warning,
} from '@wordpress/block-editor';
import {
SelectControl,
Expand All @@ -30,33 +31,52 @@ export default function TemplatePartEdit( {
setAttributes,
clientId,
} ) {
const templatePartId = theme + '//' + slug;
const templatePartId = theme && slug ? theme + '//' + slug : null;

// Set the postId block attribute if it did not exist,
// but wait until the inner blocks have loaded to allow
// new edits to trigger this.
const { isResolved, innerBlocks } = useSelect(
const { isResolved, innerBlocks, isMissing } = useSelect(
( select ) => {
const { getEntityRecord, hasFinishedResolution } = select( 'core' );
const { getBlocks } = select( 'core/block-editor' );
const entityRecord =
theme && slug
? select( 'core' ).getEntityRecord(
'postType',
'wp_template_part',
theme + '//' + slug
)
: null;

const getEntityArgs = [
'postType',
'wp_template_part',
templatePartId,
];
const entityRecord = templatePartId
? getEntityRecord( ...getEntityArgs )
: null;
const hasResolvedEntity = templatePartId
? hasFinishedResolution( 'getEntityRecord', getEntityArgs )
: false;

return {
innerBlocks: getBlocks( clientId ),
isResolved: !! entityRecord,
isResolved: hasResolvedEntity,
isMissing: hasResolvedEntity && ! entityRecord,
};
},
[ templatePartId, clientId ]
);

const blockProps = useBlockProps();
const isPlaceholder = ! slug;
const isEntityAvailable = ! isPlaceholder && ! isMissing;

if ( ! isPlaceholder && isMissing ) {
return (
<TagName { ...blockProps }>
<Warning>
{ __(
'Template part has been deleted or is unavailable.'
) }
</Warning>
</TagName>
);
}

const inspectorAdvancedControls = (
<InspectorAdvancedControls>
Expand Down Expand Up @@ -87,7 +107,7 @@ export default function TemplatePartEdit( {
innerBlocks={ innerBlocks }
/>
) }
{ ! isPlaceholder && isResolved && (
{ isEntityAvailable && (
<BlockControls>
<ToolbarGroup className="wp-block-template-part__block-control-group">
<TemplatePartNamePanel postId={ templatePartId } />
Expand Down Expand Up @@ -118,7 +138,7 @@ export default function TemplatePartEdit( {
</ToolbarGroup>
</BlockControls>
) }
{ ! isPlaceholder && isResolved && (
{ isEntityAvailable && (
<TemplatePartInnerBlocks
postId={ templatePartId }
hasInnerBlocks={ innerBlocks.length > 0 }
Expand Down

0 comments on commit 0585c2f

Please sign in to comment.