-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Template parts in patterns page: add author field #60372
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
@@ -30,6 +35,7 @@ import { | |
lockSmall, | ||
} from '@wordpress/icons'; | ||
import { usePrevious } from '@wordpress/compose'; | ||
import { useEntityRecords } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -39,6 +45,7 @@ import Page from '../page'; | |
import { | ||
LAYOUT_GRID, | ||
LAYOUT_TABLE, | ||
LAYOUT_LIST, | ||
PATTERN_TYPES, | ||
TEMPLATE_PART_POST_TYPE, | ||
PATTERN_SYNC_TYPES, | ||
|
@@ -59,6 +66,7 @@ import { unlock } from '../../lock-unlock'; | |
import usePatterns from './use-patterns'; | ||
import PatternsHeader from './header'; | ||
import { useLink } from '../routes/link'; | ||
import { useAddedBy } from '../page-templates-template-parts/hooks'; | ||
|
||
const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock( | ||
blockEditorPrivateApis | ||
|
@@ -201,6 +209,39 @@ function Preview( { item, categoryId, viewType } ) { | |
); | ||
} | ||
|
||
function Author( { item, viewType } ) { | ||
const [ isImageLoaded, setIsImageLoaded ] = useState( false ); | ||
const { text, icon, imageUrl } = useAddedBy( item.type, item.id ); | ||
const withIcon = viewType !== LAYOUT_LIST; | ||
|
||
return ( | ||
<HStack alignment="left" spacing={ 1 }> | ||
{ withIcon && imageUrl && ( | ||
<div | ||
className={ classnames( | ||
'page-templates-author-field__avatar', | ||
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. This style rules are defined in |
||
{ | ||
'is-loaded': isImageLoaded, | ||
} | ||
) } | ||
> | ||
<img | ||
onLoad={ () => setIsImageLoaded( true ) } | ||
alt="" | ||
src={ imageUrl } | ||
/> | ||
</div> | ||
) } | ||
{ withIcon && ! imageUrl && ( | ||
<div className="page-templates-author-field__icon"> | ||
<Icon icon={ icon } /> | ||
</div> | ||
) } | ||
<span className="page-templates-author-field__name">{ text }</span> | ||
</HStack> | ||
); | ||
} | ||
|
||
function Title( { item, categoryId } ) { | ||
const isUserPattern = item.type === PATTERN_TYPES.user; | ||
const isNonUserPattern = item.type === PATTERN_TYPES.theme; | ||
|
@@ -289,6 +330,24 @@ export default function DataviewsPatterns() { | |
syncStatus: viewSyncStatus, | ||
} | ||
); | ||
|
||
const { records } = useEntityRecords( 'postType', TEMPLATE_PART_POST_TYPE, { | ||
per_page: -1, | ||
} ); | ||
const authors = useMemo( () => { | ||
if ( ! records ) { | ||
return EMPTY_ARRAY; | ||
} | ||
const authorsSet = new Set(); | ||
records.forEach( ( template ) => { | ||
authorsSet.add( template.author_text ); | ||
} ); | ||
return Array.from( authorsSet ).map( ( author ) => ( { | ||
value: author, | ||
label: author, | ||
} ) ); | ||
}, [ records ] ); | ||
|
||
const fields = useMemo( () => { | ||
const _fields = [ | ||
{ | ||
|
@@ -313,6 +372,7 @@ export default function DataviewsPatterns() { | |
enableHiding: false, | ||
}, | ||
]; | ||
|
||
if ( type === PATTERN_TYPES.theme ) { | ||
_fields.push( { | ||
header: __( 'Sync status' ), | ||
|
@@ -342,23 +402,42 @@ export default function DataviewsPatterns() { | |
}, | ||
enableSorting: false, | ||
} ); | ||
} else if ( type === TEMPLATE_PART_POST_TYPE ) { | ||
_fields.push( { | ||
header: __( 'Author' ), | ||
id: 'author', | ||
getValue: ( { item } ) => item.templatePart.author_text, | ||
render: ( { item } ) => { | ||
return <Author viewType={ view.type } item={ item } />; | ||
}, | ||
type: ENUMERATION_TYPE, | ||
elements: authors, | ||
filterBy: { | ||
isPrimary: true, | ||
}, | ||
width: '1%', | ||
} ); | ||
} | ||
|
||
return _fields; | ||
}, [ view.type, categoryId, type ] ); | ||
}, [ view.type, categoryId, type, authors ] ); | ||
|
||
// Reset the page number when the category changes. | ||
useEffect( () => { | ||
if ( previousCategoryId !== categoryId ) { | ||
setView( DEFAULT_VIEW ); | ||
} | ||
}, [ categoryId, previousCategoryId ] ); | ||
const { data, paginationInfo } = useMemo( () => { | ||
// Since filters are applied server-side, | ||
// we need to remove them from the view | ||
// Search is managed server-side as well as filters for patterns. | ||
// However, the author filter in template parts is done client-side. | ||
const viewWithoutFilters = { ...view }; | ||
delete viewWithoutFilters.search; | ||
viewWithoutFilters.filters = []; | ||
if ( type !== TEMPLATE_PART_POST_TYPE ) { | ||
viewWithoutFilters.filters = []; | ||
} | ||
return filterSortAndPaginate( patterns, viewWithoutFilters, fields ); | ||
}, [ patterns, view, fields ] ); | ||
}, [ patterns, view, fields, type ] ); | ||
|
||
const actions = useMemo( | ||
() => [ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This component is copied verbatim from https://github.com/WordPress/gutenberg/blob/add/author-field-filter-template-parts/packages/edit-site/src/components/page-templates-template-parts/index.js#L117