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

Fix missing Add Template Part button in Template Parts page #52542

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading