-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce3eeb6
commit a8c08ef
Showing
4 changed files
with
44 additions
and
56 deletions.
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
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,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; | ||
} |
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
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