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

Latest Posts block: Refactor to drop apiFetch usage in favor of using the data module #33063

Merged
merged 1 commit into from
Jul 7, 2021
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
76 changes: 24 additions & 52 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, RawHTML, useEffect, useRef } from '@wordpress/element';
import { RawHTML } from '@wordpress/element';
import {
BaseControl,
PanelBody,
Expand All @@ -19,8 +19,6 @@ import {
ToggleControl,
ToolbarGroup,
} from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import { __, sprintf } from '@wordpress/i18n';
import { dateI18n, format, __experimentalGetSettings } from '@wordpress/date';
import {
Expand Down Expand Up @@ -49,10 +47,12 @@ import {
*/
const CATEGORIES_LIST_QUERY = {
per_page: -1,
context: 'view',
};
const USERS_LIST_QUERY = {
per_page: -1,
has_published_posts: [ 'post' ],
context: 'view',
};

export default function LatestPostsEdit( { attributes, setAttributes } ) {
Expand Down Expand Up @@ -81,9 +81,13 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
latestPosts,
defaultImageWidth,
defaultImageHeight,
categoriesList,
authorList,
} = useSelect(
( select ) => {
const { getEntityRecords, getMedia } = select( coreStore );
const { getEntityRecords, getMedia, getUsers } = select(
coreStore
);
const { getSettings } = select( blockEditorStore );
const { imageSizes, imageDimensions } = getSettings();
const catIds =
Expand Down Expand Up @@ -150,6 +154,12 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
};
return { ...post, featuredImageInfo };
} ),
categoriesList: getEntityRecords(
'taxonomy',
'category',
CATEGORIES_LIST_QUERY
),
authorList: getUsers( USERS_LIST_QUERY ),
};
},
[
Expand All @@ -161,15 +171,14 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
selectedAuthor,
]
);
const [ categoriesList, setCategoriesList ] = useState( [] );
const [ authorList, setAuthorList ] = useState( [] );
const categorySuggestions = categoriesList.reduce(
( accumulator, category ) => ( {
...accumulator,
[ category.name ]: category,
} ),
{}
);
const categorySuggestions =
categoriesList?.reduce(
( accumulator, category ) => ( {
...accumulator,
[ category.name ]: category,
} ),
{}
) ?? {};
const selectCategories = ( tokens ) => {
const hasNoSuggestion = tokens.some(
( token ) =>
Expand All @@ -193,43 +202,6 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
setAttributes( { categories: allCategories } );
};

const isStillMounted = useRef();

useEffect( () => {
isStillMounted.current = true;

apiFetch( {
path: addQueryArgs( `/wp/v2/categories`, CATEGORIES_LIST_QUERY ),
} )
.then( ( data ) => {
if ( isStillMounted.current ) {
setCategoriesList( data );
}
} )
.catch( () => {
if ( isStillMounted.current ) {
setCategoriesList( [] );
}
} );
apiFetch( {
path: addQueryArgs( `/wp/v2/users`, USERS_LIST_QUERY ),
} )
.then( ( data ) => {
if ( isStillMounted.current ) {
setAuthorList( data );
}
} )
.catch( () => {
if ( isStillMounted.current ) {
setAuthorList( [] );
}
} );

return () => {
isStillMounted.current = false;
};
}, [] );

const hasPosts = !! latestPosts?.length;
const inspectorControls = (
<InspectorControls>
Expand Down Expand Up @@ -377,7 +349,7 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
'' !== value ? Number( value ) : undefined,
} )
}
authorList={ authorList }
authorList={ authorList ?? [] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Maybe we can use a shared EMPTY_ARRAY reference to avoid rerenders.

selectedAuthorId={ selectedAuthor }
/>

Expand Down Expand Up @@ -466,7 +438,7 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
'trim',
] );
let excerpt = post.excerpt.rendered;
const currentAuthor = authorList.find(
const currentAuthor = authorList?.find(
( author ) => author.id === post.author
);

Expand Down