-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
908 additions
and
3 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
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
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'; |
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,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; |
Oops, something went wrong.