forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataViews: Register the deletePost action like any third-party action. (
WordPress#62913) Co-authored-by: youknowriad <[email protected]> Co-authored-by: sirreal <[email protected]> Co-authored-by: jorgefilipecosta <[email protected]>
- Loading branch information
1 parent
d261d75
commit cf614c8
Showing
14 changed files
with
244 additions
and
93 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
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { trash } from '@wordpress/icons'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { __, _n, sprintf, _x } from '@wordpress/i18n'; | ||
import { useState } from '@wordpress/element'; | ||
import { | ||
Button, | ||
__experimentalText as Text, | ||
__experimentalHStack as HStack, | ||
__experimentalVStack as VStack, | ||
} from '@wordpress/components'; | ||
// @ts-ignore | ||
import { privateApis as patternsPrivateApis } from '@wordpress/patterns'; | ||
import type { Action } from '@wordpress/dataviews'; | ||
import type { StoreDescriptor } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
isTemplateRemovable, | ||
getItemTitle, | ||
isTemplateOrTemplatePart, | ||
} from './utils'; | ||
// @ts-ignore | ||
import { store as editorStore } from '../../store'; | ||
import { unlock } from '../../lock-unlock'; | ||
import type { Post } from '../types'; | ||
|
||
const { PATTERN_TYPES } = unlock( patternsPrivateApis ); | ||
|
||
// This action is used for templates, patterns and template parts. | ||
// Every other post type uses the similar `trashPostAction` which | ||
// moves the post to trash. | ||
const deletePostAction: Action< Post > = { | ||
id: 'delete-post', | ||
label: __( 'Delete' ), | ||
isPrimary: true, | ||
icon: trash, | ||
isEligible( post ) { | ||
if ( isTemplateOrTemplatePart( post ) ) { | ||
return isTemplateRemovable( post ); | ||
} | ||
// We can only remove user patterns. | ||
return post.type === PATTERN_TYPES.user; | ||
}, | ||
supportsBulk: true, | ||
hideModalHeader: true, | ||
RenderModal: ( { items, closeModal, onActionPerformed } ) => { | ||
const [ isBusy, setIsBusy ] = useState( false ); | ||
const { removeTemplates } = unlock( | ||
useDispatch( editorStore as StoreDescriptor ) | ||
); | ||
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 } | ||
__experimentalIsFocusable | ||
> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
<Button | ||
variant="primary" | ||
onClick={ async () => { | ||
setIsBusy( true ); | ||
await removeTemplates( items, { | ||
allowUndo: false, | ||
} ); | ||
onActionPerformed?.( items ); | ||
setIsBusy( false ); | ||
closeModal?.(); | ||
} } | ||
isBusy={ isBusy } | ||
disabled={ isBusy } | ||
__experimentalIsFocusable | ||
> | ||
{ __( 'Delete' ) } | ||
</Button> | ||
</HStack> | ||
</VStack> | ||
); | ||
}, | ||
}; | ||
|
||
export default deletePostAction; |
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,20 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { type StoreDescriptor, dispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import deletePost from './delete-post'; | ||
// @ts-ignore | ||
import { store as editorStore } from '../../store'; | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
export default function registerDefaultActions() { | ||
const { registerEntityAction } = unlock( | ||
dispatch( editorStore as StoreDescriptor ) | ||
); | ||
|
||
registerEntityAction( 'postType', '*', deletePost ); | ||
} |
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,50 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
TEMPLATE_ORIGINS, | ||
TEMPLATE_PART_POST_TYPE, | ||
TEMPLATE_POST_TYPE, | ||
} from '../../store/constants'; | ||
|
||
import type { Post, TemplateOrTemplatePart } from '../types'; | ||
|
||
export function isTemplateOrTemplatePart( | ||
p: Post | ||
): p is TemplateOrTemplatePart { | ||
return p.type === TEMPLATE_POST_TYPE || p.type === TEMPLATE_PART_POST_TYPE; | ||
} | ||
|
||
export function getItemTitle( item: Post ) { | ||
if ( typeof item.title === 'string' ) { | ||
return decodeEntities( item.title ); | ||
} | ||
return decodeEntities( item.title?.rendered || '' ); | ||
} | ||
|
||
/** | ||
* Check if a template is removable. | ||
* | ||
* @param template The template entity to check. | ||
* @return Whether the template is removable. | ||
*/ | ||
export function isTemplateRemovable( template: TemplateOrTemplatePart ) { | ||
if ( ! template ) { | ||
return false; | ||
} | ||
// 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. | ||
return ( | ||
[ template.source, template.source ].includes( | ||
TEMPLATE_ORIGINS.custom | ||
) && | ||
! template.has_theme_file && | ||
! template?.has_theme_file | ||
); | ||
} |
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,15 +1,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import type { Action } from '@wordpress/dataviews'; | ||
import { createSelector } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { State } from './reducer'; | ||
|
||
const EMPTY_ARRAY: Action< any >[] = []; | ||
|
||
export function getEntityActions( state: State, kind: string, name: string ) { | ||
return state.actions[ kind ]?.[ name ] ?? EMPTY_ARRAY; | ||
} | ||
export const getEntityActions = createSelector( | ||
( state: State, kind: string, name: string ) => { | ||
return [ | ||
...( state.actions[ kind ]?.[ name ] ?? [] ), | ||
...( state.actions[ kind ]?.[ '*' ] ?? [] ), | ||
]; | ||
}, | ||
( state: State, kind: string, name: string ) => [ | ||
state.actions[ kind ]?.[ name ], | ||
state.actions[ kind ]?.[ '*' ], | ||
] | ||
); |
Oops, something went wrong.