-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 Sidebar: Display item count on DataViews sidebar #62028
Changes from all commits
1b5531a
ce135d9
eb0647f
fb217d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import { | |
LAYOUT_TABLE, | ||
LAYOUT_GRID, | ||
OPERATOR_IS_ANY, | ||
OPERATOR_IS_NONE, | ||
} from '../../utils/constants'; | ||
|
||
export const DEFAULT_CONFIG_PER_VIEW_TYPE = { | ||
|
@@ -52,7 +53,16 @@ export const DEFAULT_VIEWS = { | |
title: __( 'All pages' ), | ||
slug: 'all', | ||
icon: pages, | ||
view: DEFAULT_PAGE_BASE, | ||
view: { | ||
...DEFAULT_PAGE_BASE, | ||
filters: [ | ||
{ | ||
field: 'status', | ||
operator: OPERATOR_IS_NONE, | ||
value: 'trash', | ||
}, | ||
], | ||
Comment on lines
+58
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although this filter is not needed for the query which is run to fetch all the pages, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we doing JS filtering? IMO, we shouldn't because the REST API can be altered and extended in multiple ways so the only way to get a reliable result is to use the same REST API used in the dataviews. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to spend more time with data views, so I can't respond to the above point, but I don't think we need this filter here at all. The resulting filter badge, when clicked, does nothing. |
||
}, | ||
}, | ||
{ | ||
title: __( 'Published' ), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityRecords } from '@wordpress/core-data'; | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { OPERATOR_IS_ANY, OPERATOR_IS_NONE } from '../../utils/constants'; | ||
import { DEFAULT_VIEWS } from './default-views'; | ||
|
||
export default function useDataViews( postType ) { | ||
// Fetch all the posts of the post type. | ||
const data = useEntityRecords( 'postType', postType, { | ||
per_page: -1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a little skeptical about fetching all the results using -1 like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you just want the total count, did you try looking at Example: const { totalItems } = useEntityRecords( 'postType', 'post', { status: [ 'any', 'trash' ] } ) );
Or you call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying something over here with a new route and wp_count_posts |
||
status: [ 'any', 'trash' ], | ||
} )?.records; | ||
|
||
// Insert the records for each view. | ||
const views = useMemo( () => { | ||
return DEFAULT_VIEWS[ postType ].map( ( view ) => { | ||
const viewFilters = view?.view?.filters; | ||
// Filter the records matching the view filters. | ||
view.records = data?.filter( ( record ) => { | ||
// Check if the record matches all the filters. | ||
return viewFilters.every( ( filter ) => { | ||
let filterValues = filter.value; | ||
const filterField = filter.field; | ||
const filterOperator = filter.operator; | ||
|
||
// Convert string to array value if required. | ||
if ( ! Array.isArray( filterValues ) ) { | ||
filterValues = [ filterValues ]; | ||
} | ||
|
||
if ( filterOperator === OPERATOR_IS_ANY ) { | ||
return filterValues.includes( record[ filterField ] ); | ||
} | ||
|
||
if ( filterOperator === OPERATOR_IS_NONE ) { | ||
return ! filterValues.includes( record[ filterField ] ); | ||
} | ||
|
||
return true; | ||
} ); | ||
} ); | ||
|
||
return view; | ||
} ); | ||
}, [ data, postType ] ); | ||
|
||
return views; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the span needed? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
span
is not needed, but ifcount
is0
it might mess up with the boolean logic:gutenberg/packages/edit-site/src/components/sidebar-navigation-item/index.js
Line 56 in e1aaee2
Passing an object (which the React element
<span />
essentially is) will always evaluate totrue
, bypassing that problem.But again, the
span
is not needed. The same can be achieved with aReact.Fragment
:Alternatively, we'd alter
suffix
to deal better with0
values, but this sounds like overkill.