Skip to content

Commit

Permalink
use apiFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Jun 25, 2021
1 parent ce3eeb6 commit a8c08ef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 56 deletions.
15 changes: 3 additions & 12 deletions packages/block-library/src/post-terms/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { find } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -34,17 +33,9 @@ export default function PostTermsEdit( {
const selectedTerm = useSelect(
( select ) => {
if ( ! term ) return {};
const taxonomies = select( coreStore ).getTaxonomies( {
per_page: -1,
context: 'view',
} );
return (
find(
taxonomies,
( taxonomy ) =>
taxonomy.slug === term && taxonomy.visibility.show_ui
) || {}
);
const { getTaxonomy } = select( coreStore );
const taxonomy = getTaxonomy( term );
return taxonomy?.visibility?.show_ui ? taxonomy : {};
},
[ term ]
);
Expand Down
69 changes: 38 additions & 31 deletions packages/block-library/src/post-terms/use-post-terms.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
/**
* WordPress dependencies
*/
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import { useEffect, useState } from '@wordpress/element';

export default function usePostTerms( { postId, postType, term } ) {
const { rest_base: restBase, slug } = term;
const { rest_base: restBase } = term;
const [ termIds ] = useEntityProp( 'postType', postType, restBase, postId );
return useSelect(
( select ) => {
if ( ! termIds ) {
// Waiting for post terms to be fetched.
return { isLoading: true };
}
if ( ! termIds.length ) {
return { isLoading: false };
}
const { getEntityRecords, isResolving } = select( coreStore );
const taxonomyArgs = [
'taxonomy',
slug,
{
include: termIds,
context: 'view',
},
];
const terms = getEntityRecords( ...taxonomyArgs );
const _isLoading = isResolving( 'getEntityRecords', taxonomyArgs );
return {
postTerms: terms,
isLoading: _isLoading,
hasPostTerms: !! terms?.length,
};
},
[ termIds ]
);
const [ result, setResult ] = useState( {} );
useEffect( () => {
if ( ! termIds ) {
// Waiting for post terms to be fetched.
setResult( { isLoading: true } );
return;
}
if ( ! termIds.length ) {
setResult( { isLoading: false } );
return;
}
const query = {
per_page: -1,
orderby: 'name',
order: 'asc',
include: termIds,
_fields: 'id,name,link',
};
apiFetch( {
path: addQueryArgs( `/wp/v2/${ restBase }`, query ),
} )
.then( ( terms ) => {
setResult( {
postTerms: terms,
isLoading: false,
hasPostTerms: !! terms?.length,
} );
} )
.catch( () => {
setResult( { isLoading: false } );
} );
}, [ termIds ] );
return result;
}
2 changes: 1 addition & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export function* getEntityRecords( kind, name, query = {} ) {
}

const path = addQueryArgs( entity.baseURL, {
...entity.baseURLParams,
...query,
context: 'edit',
} );

let records = Object.values( yield apiFetch( { path } ) );
Expand Down
14 changes: 2 additions & 12 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,8 @@ describe( 'getEntityRecords', () => {
page: { slug: 'page', id: 2 },
};
const ENTITIES = [
{
name: 'postType',
kind: 'root',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
},
{
name: 'postType',
kind: 'root',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
},
{ name: 'postType', kind: 'root', baseURL: '/wp/v2/types' },
{ name: 'postType', kind: 'root', baseURL: '/wp/v2/types' },
];

it( 'yields with requested post type', async () => {
Expand Down

0 comments on commit a8c08ef

Please sign in to comment.