-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Site Editor: Template list add rename action #36879
Changes from all commits
10bacbc
64cf40f
4e69669
c7fec94
4adb507
af8b3d5
63719ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useState } from '@wordpress/element'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { | ||
Button, | ||
Flex, | ||
FlexItem, | ||
MenuItem, | ||
Modal, | ||
TextControl, | ||
} from '@wordpress/components'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
export default function RenameMenuItem( { template, onClose } ) { | ||
const [ title, setTitle ] = useState( () => template.title.rendered ); | ||
const [ isModalOpen, setIsModalOpen ] = useState( false ); | ||
|
||
const { getLastEntitySaveError } = useSelect( coreStore ); | ||
const { editEntityRecord, saveEditedEntityRecord } = useDispatch( | ||
coreStore | ||
); | ||
const { createSuccessNotice, createErrorNotice } = useDispatch( | ||
noticesStore | ||
); | ||
|
||
if ( ! template.is_custom ) { | ||
return null; | ||
} | ||
|
||
async function onTemplateRename( event ) { | ||
event.preventDefault(); | ||
|
||
try { | ||
await editEntityRecord( 'postType', template.type, template.id, { | ||
title, | ||
} ); | ||
|
||
// Update state before saving rerenders the list. | ||
setTitle( '' ); | ||
setIsModalOpen( false ); | ||
onClose(); | ||
|
||
// Persist edited entity. | ||
await saveEditedEntityRecord( | ||
'postType', | ||
template.type, | ||
template.id | ||
); | ||
|
||
const lastError = getLastEntitySaveError( | ||
'postType', | ||
template.type, | ||
template.id | ||
); | ||
|
||
if ( lastError ) { | ||
throw lastError; | ||
} | ||
|
||
createSuccessNotice( __( 'Template has been renamed.' ), { | ||
type: 'snackbar', | ||
} ); | ||
} catch ( error ) { | ||
const errorMessage = | ||
error.message && error.code !== 'unknown_error' | ||
? error.message | ||
: __( 'An error occurred while renaming the template.' ); | ||
|
||
createErrorNotice( errorMessage, { type: 'snackbar' } ); | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<MenuItem | ||
onClick={ () => { | ||
setIsModalOpen( true ); | ||
setTitle( template.title.rendered ); | ||
} } | ||
> | ||
{ __( 'Rename' ) } | ||
</MenuItem> | ||
{ isModalOpen && ( | ||
<Modal | ||
title={ __( 'Rename template' ) } | ||
closeLabel={ __( 'Close' ) } | ||
onRequestClose={ () => { | ||
setIsModalOpen( false ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a little weird to me, as I'd imagine closing the modal would also remove previously edited title. I believe that's the side-effect of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to use state. |
||
} } | ||
overlayClassName="edit-site-list__rename-modal" | ||
> | ||
<form onSubmit={ onTemplateRename }> | ||
<Flex align="flex-start" gap={ 8 }> | ||
<FlexItem> | ||
<TextControl | ||
label={ __( 'Name' ) } | ||
value={ title } | ||
onChange={ setTitle } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
required | ||
/> | ||
</FlexItem> | ||
</Flex> | ||
|
||
<Flex | ||
className="edit-site-list__rename-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> | ||
) } | ||
</> | ||
); | ||
} |
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.
Why not calling
onClose
here once the modal is opened?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.
Calling
onClose
here will unmount dropdown, so modal is never rendered.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.
I see. Then why aren't we rendering the modal outside of the dropdown?
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.
No strong preference for this. I just thought having a separate component would be a little cleaner. Happy to move logic in the main file. Probably going to save a few lines of code as well :)
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.
BTW, I don't think we should be closing the modal for accessibility reasons. Closing the modal should move focus back to the Rename button again.
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.
I also like that behavior but wasn't sure if it was accessibility improvement.