-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix missing Add Template Part button in Template Parts page (#52542)
- Loading branch information
1 parent
a2bcf95
commit 2f05440
Showing
2 changed files
with
60 additions
and
22 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/edit-site/src/components/page-template-parts/add-new-template-part.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) } | ||
/> | ||
) } | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2f05440
There was a problem hiding this comment.
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:
specs/editor/plugins/block-variations.test.js
/test/e2e/specs/editor/various/autocomplete-and-mentions.spec.js