inspector-controls
3 years ago
BlockControls.js
3 years ago
InspectorControls.js
3 years ago
LayoutSelector.js
3 years ago
LoopRenderer.js
3 years ago
QueryLoopRenderer.js
4 years ago
utils.js
3 years ago
QueryLoopRenderer.js
48 lines
| 1 | import { useMemo } from '@wordpress/element'; |
| 2 | import { useSelect } from '@wordpress/data'; |
| 3 | import LoopRenderer from './LoopRenderer'; |
| 4 | import { normalizeRepeatableArgs, removeEmpty } from './utils'; |
| 5 | import { store as coreStore } from '@wordpress/core-data'; |
| 6 | |
| 7 | export default function QueryLoopRenderer( props ) { |
| 8 | const { clientId, context } = props; |
| 9 | const query = context[ 'generateblocks/query' ] || {}; |
| 10 | |
| 11 | const normalizedQuery = useMemo( () => { |
| 12 | return normalizeRepeatableArgs( removeEmpty( query ) ); |
| 13 | }, [ JSON.stringify( query ) ] ); |
| 14 | |
| 15 | const { data, isResolvingData, hasResolvedData } = useSelect( ( select ) => { |
| 16 | const { |
| 17 | getEntityRecords, |
| 18 | isResolving, |
| 19 | hasFinishedResolution, |
| 20 | } = select( coreStore ); |
| 21 | |
| 22 | const queryParams = [ 'postType', query.post_type || 'post', normalizedQuery ]; |
| 23 | |
| 24 | return { |
| 25 | data: getEntityRecords( ...queryParams ), |
| 26 | isResolvingData: isResolving( 'getEntityRecords', queryParams ), |
| 27 | hasResolvedData: hasFinishedResolution( 'getEntityRecords', queryParams ), |
| 28 | }; |
| 29 | }, [ JSON.stringify( normalizedQuery ) ] ); |
| 30 | |
| 31 | return ( |
| 32 | <div className="gb-post-template-wrapper"> |
| 33 | <LoopRenderer |
| 34 | data={ data } |
| 35 | hasData={ !! ( hasResolvedData && data?.length ) } |
| 36 | isResolvingData={ isResolvingData } |
| 37 | hasResolvedData={ hasResolvedData } |
| 38 | templateLock={ true } |
| 39 | clientId={ clientId } |
| 40 | contextCallback={ ( post ) => ( { |
| 41 | postType: post.type, |
| 42 | postId: post.id, |
| 43 | } ) } |
| 44 | /> |
| 45 | </div> |
| 46 | ); |
| 47 | } |
| 48 |