-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site Editor: Template list add rename action
- Loading branch information
Showing
4 changed files
with
216 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components'; | ||
import { moreVertical } from '@wordpress/icons'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../../store'; | ||
import isTemplateRemovable from '../../../utils/is-template-removable'; | ||
import isTemplateRevertable from '../../../utils/is-template-revertable'; | ||
import RenameMenuItem from './rename-menu-item'; | ||
|
||
export default function Actions( { template } ) { | ||
const { removeTemplate, revertTemplate } = useDispatch( editSiteStore ); | ||
const { saveEditedEntityRecord } = useDispatch( coreStore ); | ||
const { createSuccessNotice, createErrorNotice } = useDispatch( | ||
noticesStore | ||
); | ||
|
||
const isRemovable = isTemplateRemovable( template ); | ||
const isRevertable = isTemplateRevertable( template ); | ||
|
||
if ( ! isRemovable && ! isRevertable ) { | ||
return null; | ||
} | ||
|
||
async function revertAndSaveTemplate() { | ||
try { | ||
await revertTemplate( template, { allowUndo: false } ); | ||
await saveEditedEntityRecord( | ||
'postType', | ||
template.type, | ||
template.id | ||
); | ||
|
||
createSuccessNotice( __( 'Template reverted.' ), { | ||
type: 'snackbar', | ||
} ); | ||
} catch ( error ) { | ||
const errorMessage = | ||
error.message && error.code !== 'unknown_error' | ||
? error.message | ||
: __( 'An error occurred while reverting the template.' ); | ||
|
||
createErrorNotice( errorMessage, { type: 'snackbar' } ); | ||
} | ||
} | ||
|
||
return ( | ||
<DropdownMenu | ||
icon={ moreVertical } | ||
label={ __( 'Actions' ) } | ||
className="edit-site-list-table__actions" | ||
> | ||
{ ( { onClose } ) => ( | ||
<MenuGroup> | ||
{ isRemovable && ( | ||
<> | ||
<RenameMenuItem | ||
template={ template } | ||
onClose={ onClose } | ||
/> | ||
<MenuItem | ||
isDestructive | ||
onClick={ () => { | ||
removeTemplate( template ); | ||
onClose(); | ||
} } | ||
> | ||
{ __( 'Delete template' ) } | ||
</MenuItem> | ||
</> | ||
) } | ||
{ isRevertable && ( | ||
<MenuItem | ||
info={ __( 'Restore template to theme default' ) } | ||
onClick={ () => { | ||
revertAndSaveTemplate(); | ||
onClose(); | ||
} } | ||
> | ||
{ __( 'Clear customizations' ) } | ||
</MenuItem> | ||
) } | ||
</MenuGroup> | ||
) } | ||
</DropdownMenu> | ||
); | ||
} |
92 changes: 92 additions & 0 deletions
92
packages/edit-site/src/components/list/actions/rename-menu-item.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 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { useEntityProp, store as coreStore } from '@wordpress/core-data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
Button, | ||
Flex, | ||
FlexItem, | ||
MenuItem, | ||
Modal, | ||
TextControl, | ||
} from '@wordpress/components'; | ||
|
||
export default function RenameMenuItem( { template, onClose } ) { | ||
const [ isModalOpen, setIsModalOpen ] = useState( false ); | ||
|
||
const { saveEditedEntityRecord } = useDispatch( coreStore ); | ||
const [ title, setTitle ] = useEntityProp( | ||
'postType', | ||
template.type, | ||
'title', | ||
template.id | ||
); | ||
|
||
async function onTemplateRename( event ) { | ||
event.preventDefault(); | ||
setIsModalOpen( false ); | ||
onClose(); | ||
|
||
// Presist edited entity. | ||
await saveEditedEntityRecord( 'postType', template.type, template.id ); | ||
} | ||
|
||
return ( | ||
<> | ||
<MenuItem | ||
onClick={ () => { | ||
setIsModalOpen( true ); | ||
} } | ||
> | ||
{ __( 'Rename' ) } | ||
</MenuItem> | ||
{ isModalOpen && ( | ||
<Modal | ||
title={ __( 'Rename template' ) } | ||
closeLabel={ __( 'Close' ) } | ||
onRequestClose={ () => { | ||
setIsModalOpen( false ); | ||
} } | ||
overlayClassName="edit-site-template__modal" | ||
> | ||
<form onSubmit={ onTemplateRename }> | ||
<Flex align="flex-start" gap={ 8 }> | ||
<FlexItem> | ||
<TextControl | ||
label={ __( 'Name' ) } | ||
value={ title } | ||
onChange={ setTitle } | ||
/> | ||
</FlexItem> | ||
</Flex> | ||
|
||
<Flex | ||
className="edit-site-template__modal-actions" | ||
justify="flex-end" | ||
expanded={ false } | ||
> | ||
<FlexItem> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => { | ||
setIsModalOpen( false ); | ||
} } | ||
> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
</FlexItem> | ||
<FlexItem> | ||
<Button variant="primary" type="submit"> | ||
{ __( 'Save' ) } | ||
</Button> | ||
</FlexItem> | ||
</Flex> | ||
</form> | ||
</Modal> | ||
) } | ||
</> | ||
); | ||
} |
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