Skip to content
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: improve UX of bundled views for Pages #65295

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ function getItemId( item ) {

export default function PostList( { postType } ) {
const [ view, setView ] = useView( postType );
const defaultViews = useDefaultViews( { postType } );
const history = useHistory();
const location = useLocation();
const {
Expand All @@ -202,7 +203,26 @@ export default function PostList( { postType } ) {
[ history ]
);

const { isLoading: isLoadingFields, fields } = usePostFields( view.type );
const getActiveViewFilters = ( views, match ) => {
const found = views.find( ( { slug } ) => slug === match );
return found?.filters ?? [];
};

const { isLoading: isLoadingFields, fields: _fields } = usePostFields(
view.type
);
const fields = useMemo( () => {
const activeViewFilters = getActiveViewFilters(
defaultViews,
activeView
).map( ( { field } ) => field );
return _fields.map( ( field ) => ( {
...field,
elements: activeViewFilters.includes( field.id )
? []
: field.elements,
} ) );
}, [ _fields, defaultViews, activeView ] );

const queryArgs = useMemo( () => {
const filters = {};
Expand All @@ -225,6 +245,32 @@ export default function PostList( { postType } ) {
filters.author_exclude = filter.value;
}
} );

// The bundled views want data filtered without displaying the filter.
const activeViewFilters = getActiveViewFilters(
defaultViews,
activeView
);
activeViewFilters.forEach( ( filter ) => {
if (
filter.field === 'status' &&
filter.operator === OPERATOR_IS_ANY
) {
filters.status = filter.value;
}
if (
filter.field === 'author' &&
filter.operator === OPERATOR_IS_ANY
) {
filters.author = filter.value;
} else if (
filter.field === 'author' &&
filter.operator === OPERATOR_IS_NONE
) {
filters.author_exclude = filter.value;
}
} );

// We want to provide a different default item for the status filter
// than the REST API provides.
if ( ! filters.status || filters.status === '' ) {
Expand All @@ -240,7 +286,7 @@ export default function PostList( { postType } ) {
search: view.search,
...filters,
};
}, [ view ] );
}, [ view, activeView, defaultViews ] );
const {
records,
isResolving: isLoadingData,
Expand Down
104 changes: 47 additions & 57 deletions packages/edit-site/src/components/sidebar-dataviews/default-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,76 +88,66 @@ export function useDefaultViews( { postType } ) {
title: __( 'Published' ),
slug: 'published',
icon: published,
view: {
...DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'publish',
},
],
},
view: DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'publish',
},
],
},
{
title: __( 'Scheduled' ),
slug: 'future',
icon: scheduled,
view: {
...DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'future',
},
],
},
view: DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'future',
},
],
},
{
title: __( 'Drafts' ),
slug: 'drafts',
icon: drafts,
view: {
...DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'draft',
},
],
},
view: DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'draft',
},
],
},
{
title: __( 'Pending' ),
slug: 'pending',
icon: pending,
view: {
...DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'pending',
},
],
},
view: DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'pending',
},
],
},
{
title: __( 'Private' ),
slug: 'private',
icon: notAllowed,
view: {
...DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'private',
},
],
},
view: DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'private',
},
],
},
{
title: __( 'Trash' ),
Expand All @@ -167,14 +157,14 @@ export function useDefaultViews( { postType } ) {
...DEFAULT_POST_BASE,
type: LAYOUT_TABLE,
layout: defaultLayouts[ LAYOUT_TABLE ].layout,
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'trash',
},
],
},
filters: [
{
field: 'status',
operator: OPERATOR_IS_ANY,
value: 'trash',
},
],
},
];
}, [ labels ] );
Expand Down
Loading