-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataViews Extensibility: Allow unregistering the reorder-page action #64199
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This PR is an opportunity to create a dedicated folder/file for the post type fields. If the fields declaration grows and becomes too big we can separate each field in its own file.