Skip to content

Commit

Permalink
Dont change template
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jul 8, 2024
1 parent 8730110 commit fc71ee8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 94 deletions.
84 changes: 0 additions & 84 deletions packages/edit-site/src/components/page-templates/author-field.js

This file was deleted.

47 changes: 39 additions & 8 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import {
Icon,
__experimentalText as Text,
__experimentalHStack as HStack,
VisuallyHidden,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -25,7 +32,7 @@ import { Async } from '../async';
import Page from '../page';
import { default as Link, useLink } from '../routes/link';
import AddNewTemplate from '../add-new-template';

import { useAddedBy } from './hooks';
import {
TEMPLATE_POST_TYPE,
OPERATOR_IS_ANY,
Expand All @@ -37,7 +44,6 @@ import {
import usePatternSettings from '../page-patterns/use-pattern-settings';
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';
import { TemplateAuthorField } from './author-field';

const { usePostActions } = unlock( editorPrivateApis );

Expand Down Expand Up @@ -95,6 +101,36 @@ function Title( { item, viewType } ) {
);
}

function AuthorField( { 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={ clsx( 'page-templates-author-field__avatar', {
'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 Preview( { item, viewType } ) {
const settings = usePatternSettings();
const [ backgroundColor = 'white' ] = useGlobalStyle( 'color.background' );
Expand Down Expand Up @@ -274,12 +310,7 @@ export default function PageTemplates() {
id: 'author',
getValue: ( { item } ) => item.author_text,
render: ( { item } ) => {
return (
<TemplateAuthorField
viewType={ view.type }
item={ item }
/>
);
return <AuthorField viewType={ view.type } item={ item } />;
},
elements: authors,
width: '1%',
Expand Down
53 changes: 51 additions & 2 deletions packages/edit-site/src/components/posts-app/posts-list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import { Button, __experimentalHStack as HStack } from '@wordpress/components';
import {
Button,
__experimentalHStack as HStack,
Icon,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
Expand All @@ -17,6 +26,7 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useSelect, useDispatch } from '@wordpress/data';
import { DataViews } from '@wordpress/dataviews';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { commentAuthorAvatar as authorIcon } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -40,7 +50,6 @@ import Media from '../media';
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';
import { usePrevious } from '@wordpress/compose';
import { PostAuthorField } from '../page-templates/author-field';

const { usePostActions } = unlock( editorPrivateApis );
const { useLocation, useHistory } = unlock( routerPrivateApis );
Expand Down Expand Up @@ -208,6 +217,46 @@ function getItemId( item ) {
return item.id.toString();
}

function PostAuthorField( { item, viewType } ) {
const { text, icon, imageUrl } = useSelect(
( select ) => {
const { getUser } = select( coreStore );
const user = getUser( item.author );
return {
icon: authorIcon,
imageUrl: user?.avatar_urls?.[ 48 ],
text: user?.name,
};
},
[ item ]
);
const withIcon = viewType !== LAYOUT_LIST;
const [ isImageLoaded, setIsImageLoaded ] = useState( false );
return (
<HStack alignment="left" spacing={ 1 }>
{ withIcon && imageUrl && (
<div
className={ clsx( 'page-templates-author-field__avatar', {
'is-loaded': isImageLoaded,
} ) }
>
<img
onLoad={ () => setIsImageLoaded( true ) }
alt={ __( 'Author avatar' ) }
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>
);
}

export default function PostsList( { postType } ) {
const [ view, setView ] = useView( postType );
const history = useHistory();
Expand Down

0 comments on commit fc71ee8

Please sign in to comment.