Skip to content

Commit

Permalink
Quick Edit: Support bulk selection (#63841)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent cee2a71 commit 9506009
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
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={ label }
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( ',' ),
} );
}
},
Expand Down

0 comments on commit 9506009

Please sign in to comment.