-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataViews Extensibility: Allow unregistering the reorder-page action (#…
…64199) Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org> Co-authored-by: sirreal <jonsurrell@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
- Loading branch information
1 parent
d578bb2
commit 5562199
Showing
8 changed files
with
171 additions
and
120 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
3 changes: 3 additions & 0 deletions
3
packages/editor/src/dataviews/actions/reorder-page.native.tsx
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,3 @@ | ||
const reorderPage = undefined; | ||
|
||
export default reorderPage; |
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,120 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
import { useState } from '@wordpress/element'; | ||
import { DataForm, isItemValid } from '@wordpress/dataviews'; | ||
import { | ||
Button, | ||
__experimentalHStack as HStack, | ||
__experimentalVStack as VStack, | ||
} from '@wordpress/components'; | ||
import type { Action, RenderModalProps } from '@wordpress/dataviews'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { CoreDataError, PostWithPageAttributesSupport } from '../types'; | ||
import { orderField } from '../fields'; | ||
|
||
const fields = [ orderField ]; | ||
const formOrderAction = { | ||
fields: [ 'menu_order' ], | ||
}; | ||
|
||
function ReorderModal( { | ||
items, | ||
closeModal, | ||
onActionPerformed, | ||
}: RenderModalProps< PostWithPageAttributesSupport > ) { | ||
const [ item, setItem ] = useState( items[ 0 ] ); | ||
const orderInput = item.menu_order; | ||
const { editEntityRecord, saveEditedEntityRecord } = | ||
useDispatch( coreStore ); | ||
const { createSuccessNotice, createErrorNotice } = | ||
useDispatch( noticesStore ); | ||
|
||
async function onOrder( event: React.FormEvent ) { | ||
event.preventDefault(); | ||
|
||
if ( ! isItemValid( item, fields, formOrderAction ) ) { | ||
return; | ||
} | ||
|
||
try { | ||
await editEntityRecord( 'postType', item.type, item.id, { | ||
menu_order: orderInput, | ||
} ); | ||
closeModal?.(); | ||
// Persist edited entity. | ||
await saveEditedEntityRecord( 'postType', item.type, item.id, { | ||
throwOnError: true, | ||
} ); | ||
createSuccessNotice( __( 'Order updated.' ), { | ||
type: 'snackbar', | ||
} ); | ||
onActionPerformed?.( items ); | ||
} catch ( error ) { | ||
const typedError = error as CoreDataError; | ||
const errorMessage = | ||
typedError.message && typedError.code !== 'unknown_error' | ||
? typedError.message | ||
: __( 'An error occurred while updating the order' ); | ||
createErrorNotice( errorMessage, { | ||
type: 'snackbar', | ||
} ); | ||
} | ||
} | ||
const isSaveDisabled = ! isItemValid( item, fields, formOrderAction ); | ||
return ( | ||
<form onSubmit={ onOrder }> | ||
<VStack spacing="5"> | ||
<div> | ||
{ __( | ||
'Determines the order of pages. Pages with the same order value are sorted alphabetically. Negative order values are supported.' | ||
) } | ||
</div> | ||
<DataForm | ||
data={ item } | ||
fields={ fields } | ||
form={ formOrderAction } | ||
onChange={ setItem } | ||
/> | ||
<HStack justify="right"> | ||
<Button | ||
__next40pxDefaultSize | ||
variant="tertiary" | ||
onClick={ () => { | ||
closeModal?.(); | ||
} } | ||
> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
<Button | ||
__next40pxDefaultSize | ||
variant="primary" | ||
type="submit" | ||
accessibleWhenDisabled | ||
disabled={ isSaveDisabled } | ||
> | ||
{ __( 'Save' ) } | ||
</Button> | ||
</HStack> | ||
</VStack> | ||
</form> | ||
); | ||
} | ||
|
||
const reorderPage: Action< PostWithPageAttributesSupport > = { | ||
id: 'order-pages', | ||
label: __( 'Order' ), | ||
isEligible( { status } ) { | ||
return status !== 'trash'; | ||
}, | ||
RenderModal: ReorderModal, | ||
}; | ||
|
||
export default reorderPage; |
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,25 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import type { Field } from '@wordpress/dataviews'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { BasePost, PostWithPageAttributesSupport } from '../types'; | ||
|
||
export const titleField: Field< BasePost > = { | ||
type: 'text', | ||
id: 'title', | ||
label: __( 'Title' ), | ||
placeholder: __( 'No title' ), | ||
getValue: ( { item } ) => item.title, | ||
}; | ||
|
||
export const orderField: Field< PostWithPageAttributesSupport > = { | ||
type: 'integer', | ||
id: 'menu_order', | ||
label: __( 'Order' ), | ||
description: __( 'Determines the order of pages.' ), | ||
}; |
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
5562199
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 5562199.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10299441832
📝 Reported issues:
/test/e2e/specs/site-editor/site-editor-url-navigation.spec.js