-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Template Parts: Add duplicate template part command
- Loading branch information
1 parent
fb1c039
commit 8ade2be
Showing
6 changed files
with
127 additions
and
35 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
packages/edit-site/src/components/duplicate-template-part-modal/index.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,54 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import CreateTemplatePartModal from '../create-template-part-modal'; | ||
|
||
export default function DuplicateTemplatePartModal( { | ||
templatePart, | ||
onClose, | ||
onSuccess, | ||
} ) { | ||
const { createSuccessNotice } = useDispatch( noticesStore ); | ||
|
||
function handleOnSuccess( newTemplatePart ) { | ||
createSuccessNotice( | ||
sprintf( | ||
// translators: %s: The new template part's title e.g. 'Call to action (copy)'. | ||
__( '"%s" duplicated.' ), | ||
templatePart.title | ||
), | ||
{ | ||
type: 'snackbar', | ||
id: 'template-parts-create', | ||
} | ||
); | ||
|
||
onSuccess?.( newTemplatePart ); | ||
} | ||
|
||
return ( | ||
<CreateTemplatePartModal | ||
content={ templatePart.content } | ||
closeModal={ onClose } | ||
confirmLabel={ __( 'Duplicate' ) } | ||
defaultArea={ templatePart.area } | ||
defaultTitle={ sprintf( | ||
/* translators: %s: Existing template part title */ | ||
__( '%s (Copy)' ), | ||
typeof templatePart.title === 'string' | ||
? templatePart.title | ||
: templatePart.title.raw | ||
) } | ||
modalTitle={ __( 'Duplicate template part' ) } | ||
onCreate={ handleOnSuccess } | ||
onError={ onClose } | ||
/> | ||
); | ||
} |
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
52 changes: 52 additions & 0 deletions
52
packages/edit-site/src/components/template-part-modal/duplicate.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,52 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { store as interfaceStore } from '@wordpress/interface'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
import { getQueryArgs } from '@wordpress/url'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useEditedEntityRecord from '../use-edited-entity-record'; | ||
import DuplicateTemplatePartModal from '../duplicate-template-part-modal'; | ||
import { TEMPLATE_PART_MODALS } from './'; | ||
import { unlock } from '../../lock-unlock'; | ||
import { TEMPLATE_PART_POST_TYPE } from '../../utils/constants'; | ||
|
||
const { useHistory } = unlock( routerPrivateApis ); | ||
|
||
export default function PatternDuplicateModal() { | ||
const { record } = useEditedEntityRecord(); | ||
const { categoryType, categoryId } = getQueryArgs( window.location.href ); | ||
const { closeModal } = useDispatch( interfaceStore ); | ||
const history = useHistory(); | ||
|
||
const isActive = useSelect( ( select ) => | ||
select( interfaceStore ).isModalActive( TEMPLATE_PART_MODALS.duplicate ) | ||
); | ||
|
||
if ( ! isActive ) { | ||
return null; | ||
} | ||
|
||
function onSuccess( newTemplatePart ) { | ||
history.push( { | ||
postType: TEMPLATE_PART_POST_TYPE, | ||
postId: newTemplatePart.id, | ||
categoryType, | ||
categoryId, | ||
} ); | ||
|
||
closeModal(); | ||
} | ||
|
||
return ( | ||
<DuplicateTemplatePartModal | ||
onClose={ closeModal } | ||
onSuccess={ onSuccess } | ||
templatePart={ record } | ||
/> | ||
); | ||
} |
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
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