-
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
- Loading branch information
1 parent
ec66d6c
commit b4fde4c
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