Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TemplatePanel: Fixed a problem that when a new template is created, the template is not displayed in the select box. #32744

Merged
merged 11 commits into from
Jun 22, 2021
Merged
21 changes: 18 additions & 3 deletions packages/edit-post/src/components/sidebar/template/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { partial, isEmpty, map } from 'lodash';
import { partial, isEmpty, map, fromPairs } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -43,18 +43,32 @@ export function TemplatePanel() {
getEditorSettings,
getCurrentPostType,
} = select( editorStore );
const { getPostType } = select( coreStore );
const { getPostType, getEntityRecords } = select( coreStore );
const _isViewable =
getPostType( getCurrentPostType() )?.viewable ?? false;
const _supportsTemplateMode =
select( editorStore ).getEditorSettings().supportsTemplateMode &&
_isViewable;

const templates = getEntityRecords( 'postType', 'wp_template', {
per_page: -1,
} );

const newAvailableTemplates = fromPairs(
( templates || [] ).map( ( { slug, title } ) => [
slug,
title.rendered,
] )
);

return {
isEnabled: isEditorPanelEnabled( PANEL_NAME ),
isOpened: isEditorPanelOpened( PANEL_NAME ),
selectedTemplate: getEditedPostAttribute( 'template' ),
availableTemplates: getEditorSettings().availableTemplates,
availableTemplates: {
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
...getEditorSettings().availableTemplates,
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
...newAvailableTemplates,
},
template: _supportsTemplateMode && getEditedPostTemplate(),
isViewable: _isViewable,
supportsTemplateMode: _supportsTemplateMode,
Expand All @@ -75,6 +89,7 @@ export function TemplatePanel() {
const onTogglePanel = partial( toggleEditorPanelOpened, PANEL_NAME );

let panelTitle = __( 'Template' );

if ( !! template ) {
panelTitle = sprintf(
/* translators: %s: template title */
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export const getEditedPostTemplate = createRegistrySelector(
const currentTemplate = select( editorStore ).getEditedPostAttribute(
'template'
);
if ( currentTemplate ) {
if ( currentTemplate !== null ) {
const templateWithSameSlug = select( coreStore )
.getEntityRecords( 'postType', 'wp_template' )
?.find( ( template ) => template.slug === currentTemplate );
Expand Down