useDynamicContent.js
4 years ago
usePostRecord.js
4 years ago
usePostTypeRecords.js
4 years ago
usePostTypes.js
4 years ago
usePostRecord.js
81 lines
| 1 | import { useSelect } from '@wordpress/data'; |
| 2 | import { store as coreStore } from '@wordpress/core-data'; |
| 3 | import { applyFilters } from '@wordpress/hooks'; |
| 4 | |
| 5 | export default function usePostRecord( postType, postId, load = [], options = {} ) { |
| 6 | return useSelect( ( select ) => { |
| 7 | const { |
| 8 | getUser, |
| 9 | isResolving, |
| 10 | getEntityRecord, |
| 11 | getEntityRecords, |
| 12 | hasFinishedResolution, |
| 13 | } = select( coreStore ); |
| 14 | |
| 15 | // Post data fetching. |
| 16 | const params = [ 'postType', postType, postId ]; |
| 17 | |
| 18 | let postRecord = getEntityRecord( ...params ); |
| 19 | |
| 20 | const postRecordIsLoading = ( |
| 21 | ! hasFinishedResolution( 'getEntityRecord', params ) || |
| 22 | isResolving( 'getEntityRecord', params ) |
| 23 | ); |
| 24 | |
| 25 | // Author data fetching. |
| 26 | let authorIsLoading = false; |
| 27 | |
| 28 | if ( load.includes( 'author' ) && ! postRecordIsLoading && !! postRecord ) { |
| 29 | const author = getUser( postRecord.author ); |
| 30 | |
| 31 | authorIsLoading = ( |
| 32 | ! hasFinishedResolution( 'getUser', [ postRecord.author ] ) || |
| 33 | isResolving( 'getUser', [ postRecord.author ] ) |
| 34 | ); |
| 35 | |
| 36 | if ( ! authorIsLoading && !! author ) { |
| 37 | postRecord = Object.assign( {}, postRecord, { author } ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Comments data fetching. |
| 42 | let commentsIsLoading = false; |
| 43 | |
| 44 | if ( load.includes( 'comments' ) && ! postRecordIsLoading && !! postRecord ) { |
| 45 | const commentsParams = [ 'root', 'comment', { post: postId } ]; |
| 46 | const comments = getEntityRecords( ...commentsParams ); |
| 47 | |
| 48 | commentsIsLoading = ( |
| 49 | ! hasFinishedResolution( 'getEntityRecords', commentsParams ) || |
| 50 | isResolving( 'getEntityRecords', commentsParams ) |
| 51 | ); |
| 52 | |
| 53 | if ( ! commentsIsLoading && !! comments ) { |
| 54 | postRecord = Object.assign( {}, postRecord, { comments } ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Terms data fetching. |
| 59 | let termsIsLoading = false; |
| 60 | |
| 61 | if ( load.includes( 'terms' ) && ! postRecordIsLoading && !! postRecord ) { |
| 62 | const termParams = [ 'taxonomy', options.taxonomy, { post: postId } ]; |
| 63 | const terms = getEntityRecords( ...termParams ); |
| 64 | |
| 65 | termsIsLoading = ( |
| 66 | ! hasFinishedResolution( 'getEntityRecords', termParams ) || |
| 67 | isResolving( 'getEntityRecords', termParams ) |
| 68 | ); |
| 69 | |
| 70 | if ( ! termsIsLoading && !! terms ) { |
| 71 | postRecord = Object.assign( {}, postRecord, { terms } ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return { |
| 76 | record: applyFilters( 'generateblocks.editor.postRecord', postRecord ), |
| 77 | isLoading: ( postRecordIsLoading || authorIsLoading || commentsIsLoading || termsIsLoading ), |
| 78 | }; |
| 79 | }, [ postType, postId, load.join(), JSON.stringify( options ) ] ); |
| 80 | } |
| 81 |