Skip to content
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

Quick Edit: Support bulk selection #63841

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dataviews/src/components/dataform/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function DataFormTextControl< Item >( {
<TextControl
label={ header }
placeholder={ placeholder }
value={ value }
value={ value ?? '' }
onChange={ onChangeControl }
__next40pxDefaultSize
/>
Expand Down
42 changes: 26 additions & 16 deletions packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import clsx from 'clsx';
*/
import { __ } from '@wordpress/i18n';
import { DataForm } from '@wordpress/dataviews';
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
import { Button } from '@wordpress/components';
import { useState, useMemo } from '@wordpress/element';
Expand All @@ -20,18 +20,25 @@ import Page from '../page';
import usePostFields from '../post-fields';

function PostEditForm( { postType, postId } ) {
const { item } = useSelect(
const ids = useMemo( () => postId.split( ',' ), [ postId ] );
const { initialEdits } = useSelect(
( select ) => {
if ( ids.length !== 1 ) {
}
return {
item: select( coreDataStore ).getEntityRecord(
'postType',
postType,
postId
),
initialEdits:
ids.length === 1
? select( coreDataStore ).getEntityRecord(
'postType',
postType,
ids[ 0 ]
)
: null,
};
},
[ postType, postId ]
[ postType, ids ]
);
const registry = useRegistry();
const { saveEntityRecord } = useDispatch( coreDataStore );
const { fields } = usePostFields();
const form = {
Expand All @@ -40,20 +47,23 @@ function PostEditForm( { postType, postId } ) {
const [ edits, setEdits ] = useState( {} );
const itemWithEdits = useMemo( () => {
return {
...item,
...initialEdits,
...edits,
};
}, [ item, edits ] );
const onSubmit = ( event ) => {
}, [ initialEdits, edits ] );
const onSubmit = async ( event ) => {
event.preventDefault();
saveEntityRecord( 'postType', postType, itemWithEdits );
const { getEntityRecord } = registry.resolveSelect( coreDataStore );
for ( const id of ids ) {
const item = await getEntityRecord( 'postType', postType, id );
saveEntityRecord( 'postType', postType, {
...item,
...edits,
} );
}
setEdits( {} );
};

if ( ! item ) {
return null;
}

return (
<form onSubmit={ onSubmit }>
<DataForm
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ export default function PostList( { postType } ) {
const history = useHistory();
const location = useLocation();
const { postId, quickEdit = false } = location.params;
const [ selection, setSelection ] = useState( [ postId ] );
const [ selection, setSelection ] = useState( postId?.split( ',' ) ?? [] );
const onChangeSelection = useCallback(
( items ) => {
setSelection( items );
const { params } = history.getLocationWithParams();
if ( ( params.isCustom ?? 'false' ) === 'false' ) {
history.push( {
...params,
postId: items.length === 1 ? items[ 0 ] : undefined,
postId: items.join( ',' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that his change was triggering 404 requests in the Pages table view. My proposed fix - #64254.

} );
}
},
Expand Down
Loading