Skip to content

Commit

Permalink
reduce change
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Sep 13, 2024
1 parent bf1462a commit 63adf35
Show file tree
Hide file tree
Showing 12 changed files with 908 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ npm install @wordpress/fields --save

<!-- START TOKEN(Autogenerated API docs) -->

### deleteTemplate

Undocumented declaration.

### duplicatePattern

Undocumented declaration.
Expand Down Expand Up @@ -54,6 +58,10 @@ Undocumented declaration.

Undocumented declaration.

### resetTemplate

Undocumented declaration.

### restorePost

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
],
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/blob": "file:../blob",
"@wordpress/blocks": "file:../blocks",
"@wordpress/components": "file:../components",
Expand Down
1 change: 1 addition & 0 deletions packages/fields/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './base-post';
export * from './common';
export * from './pattern';
export * from './template';
167 changes: 167 additions & 0 deletions packages/fields/src/actions/pattern/delete-pattern.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* WordPress dependencies
*/
import { trash } from '@wordpress/icons';
import { __, _n, sprintf } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import {
Button,
__experimentalText as Text,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import type { Action } from '@wordpress/dataviews';

/**
* Internal dependencies
*/
import { getItemTitle } from '../utils';
import type { Pattern } from '../../types';
import { decodeEntities } from '@wordpress/html-entities';
import { unlock } from '../../lock-unlock';
import type { Notice } from '../../mutation';
import { deleteWithNotices } from '../../mutation';

const { PATTERN_TYPES } = unlock( patternsPrivateApis );

const deletePatternAction: Action< Pattern > = {
id: 'delete-pattern',
label: __( 'Delete' ),
isPrimary: true,
icon: trash,
isEligible( post ) {
return post.type === PATTERN_TYPES.user;
},
supportsBulk: true,
hideModalHeader: true,
RenderModal: ( { items, closeModal, onActionPerformed } ) => {
const [ isBusy, setIsBusy ] = useState( false );

return (
<VStack spacing="5">
<Text>
{ items.length > 1
? sprintf(
// translators: %d: number of items to delete.
_n(
'Delete %d item?',
'Delete %d items?',
items.length
),
items.length
)
: sprintf(
// translators: %s: The template or template part's titles
__( 'Delete "%s"?' ),
getItemTitle( items[ 0 ] )
) }
</Text>
<HStack justify="right">
<Button
variant="tertiary"
onClick={ closeModal }
disabled={ isBusy }
accessibleWhenDisabled
__next40pxDefaultSize
>
{ __( 'Cancel' ) }
</Button>
<Button
variant="primary"
onClick={ async () => {
setIsBusy( true );
const notices: Notice< Pattern > = {
onSuccess: {
messages: {
getOneItemMessage: ( item ) => {
let title = '';
if (
typeof item.title === 'string'
) {
title = item.title;
} else if (
typeof item.title?.raw ===
'string'
) {
title = item.title?.raw;
} else if (
'rendered' in item.title &&
typeof item.title?.rendered ===
'string'
) {
title = item.title?.rendered;
}
return sprintf(
/* translators: The template/part's name. */
__( '"%s" deleted.' ),
decodeEntities( title )
);
},
getMultipleItemMessage: () =>
__( 'Items deleted.' ),
},
},
onError: {
messages: {
getOneItemMessage: ( error ) => {
if ( error.size === 1 ) {
return sprintf(
/* translators: The template/part's name. */
__(
'Could not delete "%s".'
),
[ ...error ][ 0 ]
);
}
return __(
'An error occurred while deleting the item.'
);
},
getMultipleItemMessage: ( errors ) => {
if ( errors.size === 0 ) {
return __(
'An error occurred while deleting the items.'
);
}
if ( errors.size === 1 ) {
return sprintf(
/* translators: %s: an error message */
__(
'An error occurred while deleting the items: %s'
),
[ ...errors ][ 0 ]
);
}
return sprintf(
/* translators: %s: a list of comma separated error messages */
__(
'Some errors occurred while deleting the items: %s'
),
[ ...errors ].join( ',' )
);
},
},
},
};
await deleteWithNotices( items, notices, {
onActionPerformed,
onActionError: () => void 0,
} );
setIsBusy( false );
closeModal?.();
} }
isBusy={ isBusy }
disabled={ isBusy }
accessibleWhenDisabled
__next40pxDefaultSize
>
{ __( 'Delete' ) }
</Button>
</HStack>
</VStack>
);
},
};

export default deletePatternAction;
Loading

0 comments on commit 63adf35

Please sign in to comment.