Skip to content

Commit

Permalink
Add status entity and use it in "admin views" experiment (#55050)
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Oct 5, 2023
1 parent 5735a49 commit 94de5db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
9 changes: 9 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ export const rootEntitiesConfig = [
baseURLParams: { context: 'edit' },
key: 'plugin',
},
{
label: __( 'Post status' ),
name: 'status',
kind: 'root',
baseURL: '/wp/v2/statuses',
baseURLParams: { context: 'edit' },
plural: 'postStatuses',
key: 'slug',
},
];

export const additionalEntityConfigLoaders = [
Expand Down
24 changes: 14 additions & 10 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import PageActions from '../page-actions';
import { DataViews, PAGE_SIZE_VALUES } from '../dataviews';

const EMPTY_ARRAY = [];
const EMPTY_OBJECT = {};

export default function PagePages() {
const [ reset, setResetQuery ] = useState( ( v ) => ! v );
Expand All @@ -32,12 +33,14 @@ export default function PagePages() {
pageSize: PAGE_SIZE_VALUES[ 0 ],
} );
// Request post statuses to get the proper labels.
const [ postStatuses, setPostStatuses ] = useState( EMPTY_ARRAY );
useEffect( () => {
apiFetch( {
path: '/wp/v2/statuses',
} ).then( setPostStatuses );
}, [] );
const { records: statuses } = useEntityRecords( 'root', 'status' );
const postStatuses =
statuses === null
? EMPTY_OBJECT
: statuses.reduce( ( acc, status ) => {
acc[ status.slug ] = status.name;
return acc;
}, EMPTY_OBJECT );

// TODO: probably memo other objects passed as state(ex:https://tanstack.com/table/v8/docs/examples/react/pagination-controlled).
const pagination = useMemo(
Expand Down Expand Up @@ -66,7 +69,7 @@ export default function PagePages() {
reset,
]
);
const { records, isResolving: isLoading } = useEntityRecords(
const { records: pages, isResolving: isLoadingPages } = useEntityRecords(
'postType',
'page',
queryArgs
Expand Down Expand Up @@ -136,7 +139,8 @@ export default function PagePages() {
header: 'Status',
id: 'status',
cell: ( props ) =>
postStatuses[ props.row.original.status ]?.name,
postStatuses[ props.row.original.status ] ??
props.row.original.status,
},
{
header: <VisuallyHidden>{ __( 'Actions' ) }</VisuallyHidden>,
Expand All @@ -161,8 +165,8 @@ export default function PagePages() {
<Page title={ __( 'Pages' ) }>
<DataViews
paginationInfo={ paginationInfo }
data={ records || EMPTY_ARRAY }
isLoading={ isLoading }
data={ pages || EMPTY_ARRAY }
isLoading={ isLoadingPages }
fields={ fields }
options={ {
manualSorting: true,
Expand Down

1 comment on commit 94de5db

@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 94de5db.
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/6419273715
📝 Reported issues:

Please sign in to comment.