Skip to content

Commit

Permalink
Dataviews: list all pages, not only those with publish and draft stat…
Browse files Browse the repository at this point in the history
…uses (#55476)
  • Loading branch information
oandregal authored Oct 19, 2023
1 parent 274fe00 commit 3423180
Showing 1 changed file with 45 additions and 26 deletions.
71 changes: 45 additions & 26 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { __ } from '@wordpress/i18n';
import { useEntityRecords } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { useState, useMemo, useCallback } from '@wordpress/element';
import { useState, useMemo, useCallback, useEffect } from '@wordpress/element';
import { dateI18n, getDate, getSettings } from '@wordpress/date';

/**
Expand All @@ -21,7 +21,6 @@ import useTrashPostAction from '../actions/trash-post';
import Media from '../media';

const EMPTY_ARRAY = [];
const EMPTY_OBJECT = {};
const defaultConfigPerViewType = {
list: {},
grid: {
Expand All @@ -30,11 +29,14 @@ const defaultConfigPerViewType = {
};

export default function PagePages() {
// DEFAULT_STATUSES is intentionally sorted. Items do not have spaces in between them.
// The reason for that is to match defaultStatuses because we compare both strings below (see useEffect).
const DEFAULT_STATUSES = 'draft,future,pending,private,publish'; // All statuses but 'trash'.
const [ view, setView ] = useState( {
type: 'list',
filters: {
search: '',
status: 'publish, draft',
status: DEFAULT_STATUSES,
},
page: 1,
perPage: 5,
Expand All @@ -48,17 +50,39 @@ export default function PagePages() {
hiddenFields: [ 'date', 'featured-image' ],
layout: {},
} );
// Request post statuses to get the proper labels.

const { records: statuses } = useEntityRecords( 'root', 'status' );
const postStatuses = useMemo(
() =>
statuses === null
? EMPTY_OBJECT
: Object.fromEntries(
statuses.map( ( { slug, name } ) => [ slug, name ] )
),
[ statuses ]
);
const defaultStatuses = useMemo( () => {
return statuses === null
? DEFAULT_STATUSES
: statuses
.filter( ( { slug } ) => slug !== 'trash' )
.map( ( { slug } ) => slug )
.sort()
.join();
}, [ statuses ] );

useEffect( () => {
// Only update the view if the statuses received from the endpoint
// are different from the DEFAULT_STATUSES provided initially.
//
// The pages endpoint depends on the status endpoint via the status filter.
// Initially, this code filters the pages request by DEFAULT_STATUTES,
// instead of using the default (publish).
// https://developer.wordpress.org/rest-api/reference/pages/#list-pages
//
// By doing so, it avoids a second request to the pages endpoint
// upon receiving the statuses when they are the same (most common scenario).
if ( DEFAULT_STATUSES !== defaultStatuses ) {
setView( {
...view,
filters: {
...view.filters,
status: defaultStatuses,
},
} );
}
}, [ defaultStatuses ] );

const queryArgs = useMemo(
() => ( {
Expand Down Expand Up @@ -163,25 +187,20 @@ export default function PagePages() {
header: __( 'Status' ),
id: 'status',
getValue: ( { item } ) =>
postStatuses[ item.status ] ?? item.status,
statuses?.find( ( { slug } ) => slug === item.status )
?.name ?? item.status,
filters: [
{
type: 'enumeration',
id: 'status',
resetValue: 'publish,draft',
resetValue: defaultStatuses,
},
],
elements:
( postStatuses &&
Object.entries( postStatuses )
.filter( ( [ slug ] ) =>
[ 'publish', 'draft' ].includes( slug )
)
.map( ( [ slug, name ] ) => ( {
value: slug,
label: name,
} ) ) ) ||
[],
statuses?.map( ( { slug, name } ) => ( {
value: slug,
label: name,
} ) ) || [],
enableSorting: false,
},
{
Expand All @@ -197,7 +216,7 @@ export default function PagePages() {
},
},
],
[ postStatuses, authors ]
[ statuses, authors ]
);

const trashPostAction = useTrashPostAction();
Expand Down

1 comment on commit 3423180

@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 3423180.
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/6573173666
📝 Reported issues:

Please sign in to comment.