From 0585c2fc468a61f522b3494d1222d20d800069b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Szab=C3=B3?= Date: Tue, 19 Jan 2021 17:08:12 +0100 Subject: [PATCH] Site Editor: Add template part missing state (#28277) --- .../src/template-part/edit/index.js | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/template-part/edit/index.js b/packages/block-library/src/template-part/edit/index.js index e258a9701d31e..da36745fd4b3c 100644 --- a/packages/block-library/src/template-part/edit/index.js +++ b/packages/block-library/src/template-part/edit/index.js @@ -6,6 +6,7 @@ import { BlockControls, InspectorAdvancedControls, useBlockProps, + Warning, } from '@wordpress/block-editor'; import { SelectControl, @@ -30,26 +31,32 @@ 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 ] @@ -57,6 +64,19 @@ export default function TemplatePartEdit( { const blockProps = useBlockProps(); const isPlaceholder = ! slug; + const isEntityAvailable = ! isPlaceholder && ! isMissing; + + if ( ! isPlaceholder && isMissing ) { + return ( + + + { __( + 'Template part has been deleted or is unavailable.' + ) } + + + ); + } const inspectorAdvancedControls = ( @@ -87,7 +107,7 @@ export default function TemplatePartEdit( { innerBlocks={ innerBlocks } /> ) } - { ! isPlaceholder && isResolved && ( + { isEntityAvailable && ( @@ -118,7 +138,7 @@ export default function TemplatePartEdit( { ) } - { ! isPlaceholder && isResolved && ( + { isEntityAvailable && ( 0 }