Skip to content

Commit

Permalink
DataViews: update filters in view config (#55735)
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal authored Nov 2, 2023
1 parent 7016c56 commit 5e0ffdb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 47 deletions.
31 changes: 20 additions & 11 deletions packages/edit-site/src/components/dataviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ Example:
direction: 'desc',
},
search: '',
filters: {
author: 2,
status: 'publish, draft'
},
filters: [
{ field: 'author', operator: 'in', value: 2 },
{ field: 'status', operator: 'in', value: 'publish,draft' }
],
visibleFilters: [ 'author', 'status' ],
hiddenFields: [ 'date', 'featured-image' ],
layout: {},
Expand Down Expand Up @@ -76,17 +76,26 @@ function MyCustomPageList() {
"...": "..."
} );

const queryArgs = useMemo(
() => ( {
const queryArgs = useMemo( () => {
const filters = {};
view.filters.forEach( ( filter ) => {
if ( filter.field === 'status' && filter.operator === 'in' ) {
filters.status = filter.value;
}
if ( filter.field === 'author' && filter.operator === 'in' ) {
filters.author = filter.value;
}
} );
return {
per_page: view.perPage,
page: view.page,
_embed: 'author',
order: view.sort?.direction,
orderby: view.sort?.field
orderby: view.sort?.field,
search: view.search,
...view.filters
} ),
[ view ]
);
...filters,
};
}, [ view ] );

const {
records
Expand Down
3 changes: 1 addition & 2 deletions packages/edit-site/src/components/dataviews/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Filters( { fields, view, onChangeView } ) {
}

field.filters.forEach( ( filter ) => {
let id = field.id;
const id = field.id;
if ( 'string' === typeof filter ) {
filterIndex[ id ] = {
id,
Expand All @@ -26,7 +26,6 @@ export default function Filters( { fields, view, onChangeView } ) {
}

if ( 'object' === typeof filter ) {
id = filter.id || field.id;
filterIndex[ id ] = {
id,
name: filter.name || field.header,
Expand Down
30 changes: 16 additions & 14 deletions packages/edit-site/src/components/dataviews/in-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import {
__experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
SelectControl,
} from '@wordpress/components';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';

const { cleanEmptyObject } = unlock( blockEditorPrivateApis );
const OPERATOR_IN = 'in';

export default ( { filter, view, onChangeView } ) => {
const activeValue = view.filters.find(
( f ) => f.field === filter.id && f.operator === OPERATOR_IN
)?.value;

return (
<SelectControl
value={ view.filters[ filter.id ] }
value={ activeValue }
prefix={
<InputControlPrefixWrapper
as="span"
Expand All @@ -28,17 +26,21 @@ export default ( { filter, view, onChangeView } ) => {
}
options={ filter.elements }
onChange={ ( value ) => {
if ( value === '' ) {
value = undefined;
const filters = view.filters.filter(
( f ) => f.field !== filter.id || f.operator !== OPERATOR_IN
);
if ( value !== '' ) {
filters.push( {
field: filter.id,
operator: OPERATOR_IN,
value,
} );
}

onChangeView( ( currentView ) => ( {
...currentView,
page: 1,
filters: cleanEmptyObject( {
...currentView.filters,
[ filter.id ]: value,
} ),
filters,
} ) );
} }
/>
Expand Down
12 changes: 3 additions & 9 deletions packages/edit-site/src/components/page-pages/default-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export const DEFAULT_STATUSES = 'draft,future,pending,private,publish'; // All s
const DEFAULT_PAGE_BASE = {
type: 'list',
search: '',
filters: {
status: DEFAULT_STATUSES,
},
filters: [ { field: 'status', operator: 'in', value: DEFAULT_STATUSES } ],
page: 1,
perPage: 5,
sort: {
Expand All @@ -39,9 +37,7 @@ const DEFAULT_VIEWS = [
slug: 'drafts',
view: {
...DEFAULT_PAGE_BASE,
filters: {
status: 'draft',
},
filters: [ { field: 'status', operator: 'in', value: 'draft' } ],
},
},
{
Expand All @@ -50,9 +46,7 @@ const DEFAULT_VIEWS = [
icon: trash,
view: {
...DEFAULT_PAGE_BASE,
filters: {
status: 'trash',
},
filters: [ { field: 'status', operator: 'in', value: 'trash' } ],
},
},
];
Expand Down
31 changes: 20 additions & 11 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,36 @@ export default function PagePages() {
if ( DEFAULT_STATUSES !== defaultStatuses ) {
setView( {
...view,
filters: {
...view.filters,
status: defaultStatuses,
},
filters: [
...view.filters.filter(
( f ) => f.field !== 'status' || f.operator !== 'in'
),
{ field: 'status', operator: 'in', value: defaultStatuses },
],
} );
}
}, [ defaultStatuses ] );

const queryArgs = useMemo(
() => ( {
const queryArgs = useMemo( () => {
const filters = {};
view.filters.forEach( ( filter ) => {
if ( filter.field === 'status' && filter.operator === 'in' ) {
filters.status = filter.value;
}
if ( filter.field === 'author' && filter.operator === 'in' ) {
filters.author = filter.value;
}
} );
return {
per_page: view.perPage,
page: view.page,
_embed: 'author',
order: view.sort?.direction,
orderby: view.sort?.field,
search: view.search,
...view.filters,
} ),
[ view ]
);
...filters,
};
}, [ view ] );
const {
records: pages,
isResolving: isLoadingPages,
Expand Down Expand Up @@ -188,7 +198,6 @@ export default function PagePages() {
filters: [
{
type: 'enumeration',
id: 'status',
resetValue: defaultStatuses,
},
],
Expand Down

1 comment on commit 5e0ffdb

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 5e0ffdb.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6734459322
📝 Reported issues:

Please sign in to comment.