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: All Templates: Add filters to template author. #56338

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Link from '../routes/link';
import { useAddedBy, AvatarImage } from '../list/added-by';
import { TEMPLATE_POST_TYPE } from '../../utils/constants';
import { DataViews } from '../dataviews';
import { ENUMERATION_TYPE, OPERATOR_IN } from '../dataviews/constants';
import {
useResetTemplateAction,
deleteTemplateAction,
Expand Down Expand Up @@ -59,6 +60,7 @@ const DEFAULT_VIEW = {
// better to keep track of the hidden ones.
hiddenFields: [ 'preview' ],
layout: {},
filters: [],
};

function normalizeSearchInput( input = '' ) {
Expand Down Expand Up @@ -143,6 +145,20 @@ export default function DataviewsTemplates() {
per_page: -1,
} );

const authors = useMemo( () => {
if ( ! allTemplates ) {
return EMPTY_ARRAY;
}
const authorsSet = new Set();
allTemplates.forEach( ( template ) => {
authorsSet.add( template.author_text );
} );
return Array.from( authorsSet ).map( ( author ) => ( {
value: author,
label: author,
} ) );
}, [ allTemplates ] );

const fields = useMemo(
() => [
{
Expand Down Expand Up @@ -192,9 +208,11 @@ export default function DataviewsTemplates() {
return <AuthorField item={ item } />;
},
enableHiding: false,
type: ENUMERATION_TYPE,
elements: authors,
},
],
[]
[ authors ]
);

const { shownTemplates, paginationInfo } = useMemo( () => {
Expand All @@ -221,6 +239,21 @@ export default function DataviewsTemplates() {
} );
}

// Handle filters.
if ( view.filters.length > 0 ) {
view.filters.forEach( ( filter ) => {
if (
filter.field === 'author' &&
filter.operator === OPERATOR_IN &&
!! filter.value
) {
filteredTemplates = filteredTemplates.filter( ( item ) => {
return item.author_text === filter.value;
} );
}
} );
}

// Handle sorting.
if ( view.sort ) {
const stringSortingFields = [ 'title', 'author' ];
Expand Down
Loading