Skip to content

Commit

Permalink
Fix missing Add Template Part button in Template Parts page (#52542)
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks authored Jul 12, 2023
1 parent a2bcf95 commit 2f05440
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* WordPress dependencies
*/
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useState } from '@wordpress/element';
import { Button } from '@wordpress/components';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import CreateTemplatePartModal from '../create-template-part-modal';

const { useHistory } = unlock( routerPrivateApis );

export default function AddNewTemplatePart() {
const { canCreate, postType } = useSelect( ( select ) => {
const { supportsTemplatePartsMode } =
select( editSiteStore ).getSettings();
return {
canCreate: ! supportsTemplatePartsMode,
postType: select( coreStore ).getPostType( 'wp_template_part' ),
};
}, [] );
const [ isModalOpen, setIsModalOpen ] = useState( false );
const history = useHistory();

if ( ! canCreate || ! postType ) {
return null;
}

return (
<>
<Button variant="primary" onClick={ () => setIsModalOpen( true ) }>
{ postType.labels.add_new_item }
</Button>
{ isModalOpen && (
<CreateTemplatePartModal
closeModal={ () => setIsModalOpen( false ) }
blocks={ [] }
onCreate={ ( templatePart ) => {
setIsModalOpen( false );
history.push( {
postId: templatePart.id,
postType: 'wp_template_part',
canvas: 'edit',
} );
} }
onError={ () => setIsModalOpen( false ) }
/>
) }
</>
);
}
25 changes: 3 additions & 22 deletions packages/edit-site/src/components/page-template-parts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore, useEntityRecords } from '@wordpress/core-data';
import { useEntityRecords } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';

/**
Expand All @@ -19,8 +18,7 @@ import Table from '../table';
import Link from '../routes/link';
import AddedBy from '../list/added-by';
import TemplateActions from '../template-actions';
import AddNewTemplate from '../add-new-template';
import { store as editSiteStore } from '../../store';
import AddNewTemplatePart from './add-new-template-part';

export default function PageTemplateParts() {
const { records: templateParts } = useEntityRecords(
Expand All @@ -31,15 +29,6 @@ export default function PageTemplateParts() {
}
);

const { canCreate } = useSelect( ( select ) => {
const { supportsTemplatePartsMode } =
select( editSiteStore ).getSettings();
return {
postType: select( coreStore ).getPostType( 'wp_template_part' ),
canCreate: ! supportsTemplatePartsMode,
};
} );

const columns = [
{
header: __( 'Template Part' ),
Expand Down Expand Up @@ -87,15 +76,7 @@ export default function PageTemplateParts() {
return (
<Page
title={ __( 'Template Parts' ) }
actions={
canCreate && (
<AddNewTemplate
templateType={ 'wp_template_part' }
showIcon={ false }
toggleProps={ { variant: 'primary' } }
/>
)
}
actions={ <AddNewTemplatePart /> }
>
{ templateParts && (
<Table data={ templateParts } columns={ columns } />
Expand Down

1 comment on commit 2f05440

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2f05440.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5537485438
📝 Reported issues:

Please sign in to comment.