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

DataViews Quick Edit: Rely on the global save flow instead of a custom save button #64389

Merged
merged 1 commit into from
Aug 9, 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
1 change: 1 addition & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- `setSelection` prop has been removed. Please use `onChangeSelection` instead.
- `header` field property has been renamed to `label`.
- `DataForm`'s `visibleFields` prop has been renamed to `fields`.
- `DataForm`'s `onChange` prop has been update to receive as argument only the fields that have changed.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is important for the save flow to show the exact fields that were changed...


### New features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ export const Default = ( { type }: { type: 'panel' | 'regular' } ) => {
...form,
type,
} }
onChange={ setPost }
onChange={ ( edits ) =>
setPost( ( prev ) => ( {
...prev,
...edits,
} ) )
}
/>
);
};
6 changes: 1 addition & 5 deletions packages/dataviews/src/field-types/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ function Edit< Item >( {
const value = field.getValue( { item: data } );

const onChangeControl = useCallback(
( newValue: string | null ) =>
onChange( ( prevItem: Item ) => ( {
...prevItem,
[ id ]: newValue,
} ) ),
( newValue: string | null ) => onChange( { [ id ]: newValue } ),
[ id, onChange ]
);

Expand Down
5 changes: 2 additions & 3 deletions packages/dataviews/src/field-types/integer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ function Edit< Item >( {
const value = field.getValue( { item: data } ) ?? '';
const onChangeControl = useCallback(
( newValue: string | undefined ) =>
onChange( ( prevItem: Item ) => ( {
...prevItem,
onChange( {
[ id ]: Number( newValue ),
} ) ),
} ),
[ id, onChange ]
);

Expand Down
5 changes: 2 additions & 3 deletions packages/dataviews/src/field-types/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ function Edit< Item >( {

const onChangeControl = useCallback(
( newValue: string ) =>
onChange( ( prevItem: Item ) => ( {
...prevItem,
onChange( {
[ id ]: newValue,
} ) ),
} ),
[ id, onChange ]
);

Expand Down
11 changes: 3 additions & 8 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/**
* External dependencies
*/
import type {
ReactElement,
ComponentType,
Dispatch,
SetStateAction,
} from 'react';
import type { ReactElement, ComponentType } from 'react';

/**
* Internal dependencies
Expand Down Expand Up @@ -181,7 +176,7 @@ export type Form = {
export type DataFormControlProps< Item > = {
data: Item;
field: NormalizedField< Item >;
onChange: Dispatch< SetStateAction< Item > >;
onChange: ( value: Record< string, any > ) => void;
hideLabelFromVision?: boolean;
};

Expand Down Expand Up @@ -516,5 +511,5 @@ export interface DataFormProps< Item > {
data: Item;
fields: Field< Item >[];
form: Form;
onChange: Dispatch< SetStateAction< Item > >;
onChange: ( value: Record< string, any > ) => void;
}
73 changes: 23 additions & 50 deletions packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import clsx from 'clsx';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { DataForm, isItemValid } from '@wordpress/dataviews';
import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import { DataForm } from '@wordpress/dataviews';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
import {
Button,
FlexItem,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { useState, useMemo } from '@wordpress/element';
import { __experimentalVStack as VStack } from '@wordpress/components';
import { useState, useMemo, useEffect } from '@wordpress/element';
import { privateApis as editorPrivateApis } from '@wordpress/editor';

/**
Expand All @@ -29,14 +25,12 @@ const { PostCardPanel } = unlock( editorPrivateApis );

function PostEditForm( { postType, postId } ) {
const ids = useMemo( () => postId.split( ',' ), [ postId ] );
const { initialEdits } = useSelect(
const { record } = useSelect(
( select ) => {
if ( ids.length !== 1 ) {
}
return {
initialEdits:
record:
ids.length === 1
? select( coreDataStore ).getEntityRecord(
? select( coreDataStore ).getEditedEntityRecord(
'postType',
postType,
ids[ 0 ]
Expand All @@ -46,60 +40,39 @@ function PostEditForm( { postType, postId } ) {
},
[ postType, ids ]
);
const registry = useRegistry();
const { saveEntityRecord } = useDispatch( coreDataStore );
const [ multiEdits, setMultiEdits ] = useState( {} );
const { editEntityRecord } = useDispatch( coreDataStore );
const { fields } = usePostFields();
const form = {
type: 'panel',
fields: [ 'title', 'author', 'date', 'comment_status' ],
};
const [ edits, setEdits ] = useState( initialEdits );
const itemWithEdits = useMemo( () => {
return {
...initialEdits,
...edits,
};
}, [ initialEdits, edits ] );
const onSubmit = async ( event ) => {
event.preventDefault();

if ( ! isItemValid( itemWithEdits, fields, form ) ) {
return;
}

const { getEntityRecord } = registry.resolveSelect( coreDataStore );
const onChange = ( edits ) => {
for ( const id of ids ) {
const item = await getEntityRecord( 'postType', postType, id );
saveEntityRecord( 'postType', postType, {
...item,
...edits,
} );
editEntityRecord( 'postType', postType, id, edits );
if ( ids.length > 1 ) {
setMultiEdits( ( prev ) => ( {
...prev,
...edits,
} ) );
}
}
};
useEffect( () => {
setMultiEdits( {} );
}, [ ids ] );

const isUpdateDisabled = ! isItemValid( itemWithEdits, fields, form );
return (
<VStack as="form" onSubmit={ onSubmit } spacing={ 4 }>
<VStack spacing={ 4 }>
{ ids.length === 1 && (
<PostCardPanel postType={ postType } postId={ ids[ 0 ] } />
) }
<DataForm
data={ itemWithEdits }
data={ ids.length === 1 ? record : multiEdits }
fields={ fields }
form={ form }
onChange={ setEdits }
onChange={ onChange }
/>
<FlexItem>
<Button
variant="primary"
type="submit"
accessibleWhenDisabled
disabled={ isUpdateDisabled }
__next40pxDefaultSize
>
{ __( 'Update' ) }
</Button>
</FlexItem>
</VStack>
);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ const useDuplicatePostAction = ( postType ) => {
data={ item }
fields={ fields }
form={ formDuplicateAction }
onChange={ setItem }
onChange={ ( changes ) =>
setItem( {
...item,
...changes,
} )
}
/>
<HStack spacing={ 2 } justify="end">
<Button
Expand Down
7 changes: 6 additions & 1 deletion packages/editor/src/dataviews/actions/reorder-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ function ReorderModal( {
data={ item }
fields={ fields }
form={ formOrderAction }
onChange={ setItem }
onChange={ ( changes ) =>
setItem( {
...item,
...changes,
} )
}
/>
<HStack justify="right">
<Button
Expand Down
Loading