Skip to content
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

Consolidate and fix rename post action #61857

Merged
merged 2 commits into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 36 additions & 112 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,37 @@ const renamePostAction = {
id: 'rename-post',
label: __( 'Rename' ),
isEligible( post ) {
return post.status !== 'trash';
if ( post.status === 'trash' ) {
return false;
}
// Templates, template parts and patterns have special checks for renaming.
if (
! [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
...Object.values( PATTERN_TYPES ),
].includes( post.type )
) {
return true;
}
// In the case of templates, we can only remove custom templates.
if ( post.type === TEMPLATE_POST_TYPE ) {
return isTemplateRemovable( post ) && post.is_custom;
}
// Make necessary checks for template parts and patterns.
const isTemplatePart = post.type === TEMPLATE_PART_POST_TYPE;
const isUserPattern = post.type === PATTERN_TYPES.user;
// In patterns list page we map the templates parts to a different object
// than the one returned from the endpoint. This is why we need to check for
// two props whether is custom or has a theme file.
const isCustomPattern =
isUserPattern ||
( isTemplatePart &&
( post.isCustom || post.source === TEMPLATE_ORIGINS.custom ) );
const hasThemeFile =
isTemplatePart &&
( post.templatePart?.has_theme_file || post.has_theme_file );
return isCustomPattern && ! hasThemeFile;
},
RenderModal: ( { items, closeModal, onActionPerformed } ) => {
const [ item ] = items;
Expand Down Expand Up @@ -908,111 +938,6 @@ const deleteTemplateAction = {
},
};

const renameTemplateAction = {
id: 'rename-template',
label: __( 'Rename' ),
isEligible: ( template ) => {
// We can only remove templates or template parts that can be removed.
// Additionally in the case of templates, we can only remove custom templates.
if (
! isTemplateRemovable( template ) ||
( template.type === TEMPLATE_POST_TYPE && ! template.is_custom )
) {
return false;
}
return true;
},
RenderModal: ( { items: templates, closeModal, onActionPerformed } ) => {
const template = templates[ 0 ];
const title = decodeEntities( template.title.rendered );
const [ editedTitle, setEditedTitle ] = useState( title );
const {
editEntityRecord,
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
} = useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
async function onTemplateRename( event ) {
event.preventDefault();
try {
await editEntityRecord(
'postType',
template.type,
template.id,
{
title: editedTitle,
}
);
// Update state before saving rerenders the list.
setEditedTitle( '' );
closeModal();
// Persist edited entity.
await saveSpecifiedEntityEdits(
'postType',
template.type,
template.id,
[ 'title' ], // Only save title to avoid persisting other edits.
{
throwOnError: true,
}
);
createSuccessNotice(
template.type === TEMPLATE_POST_TYPE
? __( 'Template renamed.' )
: __( 'Template part renamed.' ),
{
type: 'snackbar',
}
);
onActionPerformed?.( templates );
} catch ( error ) {
const fallbackErrorMessage =
template.type === TEMPLATE_POST_TYPE
? __( 'An error occurred while renaming the template.' )
: __(
'An error occurred while renaming the template part.'
);
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: fallbackErrorMessage;

createErrorNotice( errorMessage, { type: 'snackbar' } );
}
}
return (
<form onSubmit={ onTemplateRename }>
<VStack spacing="5">
<TextControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Name' ) }
value={ editedTitle }
onChange={ setEditedTitle }
required
/>
<HStack justify="right">
<Button
variant="tertiary"
onClick={ closeModal }
__next40pxDefaultSize
>
{ __( 'Cancel' ) }
</Button>
<Button
variant="primary"
type="submit"
__next40pxDefaultSize
>
{ __( 'Save' ) }
</Button>
</HStack>
</VStack>
</form>
);
},
};

const canDeleteOrReset = ( item ) => {
const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE;
const isUserPattern = item.type === PATTERN_TYPES.user;
Expand Down Expand Up @@ -1196,13 +1121,12 @@ export function usePostActions( postType, onActionPerformed ) {
! isPattern &&
duplicatePostAction
: false,
! isTemplateOrTemplatePart && renamePostAction,
isTemplateOrTemplatePart && renameTemplateAction,
renamePostAction,
isPattern && exportPatternAsJSONAction,
isTemplateOrTemplatePart && resetTemplateAction,
! isTemplateOrTemplatePart && restorePostAction,
isTemplateOrTemplatePart && deleteTemplateAction,
! isTemplateOrTemplatePart && permanentlyDeletePostAction,
isTemplateOrTemplatePart ? resetTemplateAction : restorePostAction,
isTemplateOrTemplatePart
? deleteTemplateAction
: permanentlyDeletePostAction,
Comment on lines +1126 to +1129
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sidenote: The way we build the actions array here is difficult to parse. Maybe we should convert it into separate if conditions.

Example:

if ( condition ) {
    actions.push( action );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order matters for UI for now and would need multiple if conditions. So I didn't do it here, even though I thought about it too. Will check it in follow ups where more actions will be consolidated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely not a blocker, just something that comes to my mind every time I see it 😄

isPattern && deletePatternAction,
! isTemplateOrTemplatePart && trashPostAction,
].filter( Boolean );
Expand Down
Loading