-
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.
Add: Delete template action. (#31678)
Co-authored-by: Carolina Nymark <[email protected]>
- Loading branch information
Showing
7 changed files
with
211 additions
and
12 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
packages/edit-post/src/components/header/template-title/delete-template.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,92 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { pickBy } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { MenuGroup, MenuItem } from '@wordpress/components'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editPostStore } from '../../../store'; | ||
|
||
export default function DeleteTemplate() { | ||
const { clearSelectedBlock } = useDispatch( blockEditorStore ); | ||
const { setIsEditingTemplate } = useDispatch( editPostStore ); | ||
const { getEditorSettings } = useSelect( editorStore ); | ||
const { updateEditorSettings, editPost } = useDispatch( editorStore ); | ||
const { deleteEntityRecord } = useDispatch( coreStore ); | ||
const { template } = useSelect( ( select ) => { | ||
const { isEditingTemplate, getEditedPostTemplate } = select( | ||
editPostStore | ||
); | ||
const _isEditing = isEditingTemplate(); | ||
return { | ||
template: _isEditing ? getEditedPostTemplate() : null, | ||
}; | ||
}, [] ); | ||
|
||
if ( ! template || ! template.wp_id ) { | ||
return null; | ||
} | ||
let templateTitle = template.slug; | ||
if ( template?.title ) { | ||
templateTitle = template.title; | ||
} | ||
|
||
return ( | ||
<MenuGroup className="edit-post-template-top-area__second-menu-group"> | ||
<MenuItem | ||
isDestructive | ||
isTertiary | ||
isLink | ||
aria-label={ __( 'Delete template' ) } | ||
onClick={ () => { | ||
if ( | ||
// eslint-disable-next-line no-alert | ||
window.confirm( | ||
/* translators: %1$s: template name */ | ||
sprintf( | ||
'Are you sure you want to delete the %s template? It may be used by other pages or posts.', | ||
templateTitle | ||
) | ||
) | ||
) { | ||
clearSelectedBlock(); | ||
setIsEditingTemplate( false ); | ||
|
||
editPost( { | ||
template: '', | ||
} ); | ||
const settings = getEditorSettings(); | ||
const newAvailableTemplates = pickBy( | ||
settings.availableTemplates, | ||
( _title, id ) => { | ||
return id !== template.slug; | ||
} | ||
); | ||
updateEditorSettings( { | ||
...settings, | ||
availableTemplates: newAvailableTemplates, | ||
} ); | ||
deleteEntityRecord( | ||
'postType', | ||
'wp_template', | ||
template.id | ||
); | ||
} | ||
} } | ||
> | ||
{ __( 'Delete template' ) } | ||
</MenuItem> | ||
</MenuGroup> | ||
); | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/edit-post/src/components/header/template-title/edit-template-title.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,64 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { mapValues } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { TextControl } from '@wordpress/components'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { store as editorStore } from '@wordpress/editor'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editPostStore } from '../../../store'; | ||
|
||
export default function EditTemplateTitle() { | ||
const { template } = useSelect( ( select ) => { | ||
const { getEditedPostTemplate } = select( editPostStore ); | ||
return { | ||
template: getEditedPostTemplate(), | ||
}; | ||
}, [] ); | ||
|
||
const { editEntityRecord } = useDispatch( coreStore ); | ||
const { getEditorSettings } = useSelect( editorStore ); | ||
const { updateEditorSettings } = useDispatch( editorStore ); | ||
|
||
let templateTitle = __( 'Default' ); | ||
if ( template?.title ) { | ||
templateTitle = template.title; | ||
} else if ( !! template ) { | ||
templateTitle = template.slug; | ||
} | ||
|
||
return ( | ||
<TextControl | ||
label={ __( 'Title' ) } | ||
value={ templateTitle } | ||
onChange={ ( newTitle ) => { | ||
const settings = getEditorSettings(); | ||
const newAvailableTemplates = mapValues( | ||
settings.availableTemplates, | ||
( existingTitle, id ) => { | ||
if ( id !== template.slug ) { | ||
return existingTitle; | ||
} | ||
return newTitle; | ||
} | ||
); | ||
updateEditorSettings( { | ||
...settings, | ||
availableTemplates: newAvailableTemplates, | ||
} ); | ||
editEntityRecord( 'postType', 'wp_template', template.id, { | ||
title: newTitle, | ||
} ); | ||
} } | ||
/> | ||
); | ||
} |
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
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
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