Skip to content

Commit

Permalink
Absorb logic to filter fields based on the form
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 24, 2024
1 parent 5cde7a6 commit bc4b99d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
9 changes: 6 additions & 3 deletions packages/dataviews/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
* Internal dependencies
*/
import { normalizeFields } from './normalize-fields';
import type { Field } from './types';
import type { Field, Form } from './types';

export function isItemValid< Item >(
item: Item,
fields: Field< Item >[]
fields: Field< Item >[],
form: Form
): boolean {
const _fields = normalizeFields( fields );
const _fields = normalizeFields(
fields.filter( ( { id } ) => !! form.visibleFields?.includes( id ) )
);
return _fields.every( ( field ) => {
const value = field.getValue( { item } );

Expand Down
16 changes: 2 additions & 14 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,7 @@ function ReorderModal( { items, closeModal, onActionPerformed } ) {
async function onOrder( event ) {
event.preventDefault();

if (
! isItemValid(
item,
fields.filter( ( field ) =>
formOrderAction.visibleFields.includes( field.id )
)
)
) {
if ( ! isItemValid( item, fields, formOrderAction ) ) {
return;
}

Expand All @@ -688,12 +681,7 @@ function ReorderModal( { items, closeModal, onActionPerformed } ) {
} );
}
}
const isSaveDisabled = ! isItemValid(
item,
fields.filter( ( field ) =>
formOrderAction.visibleFields.includes( field.id )
)
);
const isSaveDisabled = ! isItemValid( item, fields, formOrderAction );
return (
<form onSubmit={ onOrder }>
<VStack spacing="5">
Expand Down

0 comments on commit bc4b99d

Please sign in to comment.