From 9f055aeeb8a925567bef6f4ede92e9f532036908 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 23 Jul 2024 11:27:04 +0200 Subject: [PATCH 1/2] Quick Edit: Support bulk selection --- .../src/components/dataform/index.tsx | 2 +- .../src/components/post-edit/index.js | 42 ++++++++++++------- .../src/components/post-list/index.js | 4 +- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/dataviews/src/components/dataform/index.tsx b/packages/dataviews/src/components/dataform/index.tsx index e61fea075c9b5a..848d0bb251717e 100644 --- a/packages/dataviews/src/components/dataform/index.tsx +++ b/packages/dataviews/src/components/dataform/index.tsx @@ -49,7 +49,7 @@ function DataFormTextControl< Item >( { diff --git a/packages/edit-site/src/components/post-edit/index.js b/packages/edit-site/src/components/post-edit/index.js index 6e556c56a9152c..21dbb10e514660 100644 --- a/packages/edit-site/src/components/post-edit/index.js +++ b/packages/edit-site/src/components/post-edit/index.js @@ -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'; @@ -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 = { @@ -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 (
{ setSelection( items ); @@ -150,7 +150,7 @@ export default function PostList( { postType } ) { if ( ( params.isCustom ?? 'false' ) === 'false' ) { history.push( { ...params, - postId: items.length === 1 ? items[ 0 ] : undefined, + postId: items.join( ',' ), } ); } }, From aca7484420946ee21f7b8654cfeb7d566770ef66 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 23 Jul 2024 16:17:22 +0200 Subject: [PATCH 2/2] Fix small bug --- packages/edit-site/src/components/post-list/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/post-list/index.js b/packages/edit-site/src/components/post-list/index.js index 79cdfecf0b8a37..ff25fbcd1962fb 100644 --- a/packages/edit-site/src/components/post-list/index.js +++ b/packages/edit-site/src/components/post-list/index.js @@ -142,7 +142,7 @@ export default function PostList( { postType } ) { const history = useHistory(); const location = useLocation(); const { postId, quickEdit = false } = location.params; - const [ selection, setSelection ] = useState( postId.split( ',' ) ); + const [ selection, setSelection ] = useState( postId?.split( ',' ) ?? [] ); const onChangeSelection = useCallback( ( items ) => { setSelection( items );