Skip to content

Commit

Permalink
Add option to delete pattern category
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Oct 4, 2023
1 parent 4e94684 commit 008d6a3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* WordPress dependencies
*/
import {
MenuItem,
__experimentalConfirmDialog as ConfirmDialog,
} from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { PATTERN_TYPES, PATTERN_DEFAULT_CATEGORY } from '../../utils/constants';

const { useHistory } = unlock( routerPrivateApis );

export default function DeleteCategoryMenuItem( { category, onClose } ) {
const [ isModalOpen, setIsModalOpen ] = useState( false );
const history = useHistory();

const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
const { deleteEntityRecord, invalidateResolution } =
useDispatch( coreStore );

if ( ! category?.id ) {
return null;
}

const onDelete = async () => {
try {
await deleteEntityRecord(
'taxonomy',
'wp_pattern_category',
category.id,
{ force: true },
{ throwOnError: true }
);

invalidateResolution( 'getUserPatternCategories' );

createSuccessNotice(
sprintf(
/* translators: The pattern category's name */
__( '"%s" deleted.' ),
category.label
),
{ type: 'snackbar', id: 'pattern-category-delete' }
);

onClose?.();
history.push( {
path: `/patterns`,
categoryType: PATTERN_TYPES.theme,
categoryId: PATTERN_DEFAULT_CATEGORY,
} );
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while deleting the pattern.' );

createErrorNotice( errorMessage, {
type: 'snackbar',
id: 'pattern-category-delete',
} );
}
};

return (
<>
<MenuItem isDestructive onClick={ () => setIsModalOpen( true ) }>
{ __( 'Delete' ) }
</MenuItem>
<ConfirmDialog
isOpen={ isModalOpen }
onConfirm={ onDelete }
onCancel={ () => setIsModalOpen( false ) }
>
{ sprintf(
// translators: %s: The pattern category's name.
__( 'Are you sure you want to delete "%s"?' ),
decodeEntities( category.label )
) }
</ConfirmDialog>
</>
);
}
5 changes: 5 additions & 0 deletions packages/edit-site/src/components/page-patterns/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { moreVertical } from '@wordpress/icons';
* Internal dependencies
*/
import RenameCategoryMenuItem from './rename-category-menu-item';
import DeleteCategoryMenuItem from './delete-category-menu-item';
import usePatternCategories from '../sidebar-navigation-screen-patterns/use-pattern-categories';
import { TEMPLATE_PART_POST_TYPE, PATTERN_TYPES } from '../../utils/constants';

Expand Down Expand Up @@ -76,6 +77,10 @@ export default function PatternsHeader( {
category={ patternCategory }
onClose={ onClose }
/>
<DeleteCategoryMenuItem
category={ patternCategory }
onClose={ onClose }
/>
</MenuGroup>
) }
</DropdownMenu>
Expand Down

0 comments on commit 008d6a3

Please sign in to comment.