inspector-controls
3 years ago
BlockControls.js
3 years ago
InspectorAdvancedControls.js
2 years ago
InspectorControls.js
3 years ago
LayoutSelector.js
3 years ago
LoopRenderer.js
3 years ago
QueryLoopRenderer.js
2 years ago
utils.js
3 years ago
QueryLoopRenderer.js
59 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 | canUser, |
| 21 | } = select( coreStore ); |
| 22 | |
| 23 | let queryData = normalizedQuery; |
| 24 | |
| 25 | // If the user can't update settings, we'll only show published posts. |
| 26 | if ( ! canUser( 'update', 'settings' ) ) { |
| 27 | queryData = { |
| 28 | ...queryData, |
| 29 | status: 'publish', |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | const queryParams = [ 'postType', query.post_type || 'post', queryData ]; |
| 34 | |
| 35 | return { |
| 36 | data: getEntityRecords( ...queryParams ), |
| 37 | isResolvingData: isResolving( 'getEntityRecords', queryParams ), |
| 38 | hasResolvedData: hasFinishedResolution( 'getEntityRecords', queryParams ), |
| 39 | }; |
| 40 | }, [ JSON.stringify( normalizedQuery ) ] ); |
| 41 | |
| 42 | return ( |
| 43 | <div className="gb-post-template-wrapper"> |
| 44 | <LoopRenderer |
| 45 | data={ data } |
| 46 | hasData={ !! ( hasResolvedData && data?.length ) } |
| 47 | isResolvingData={ isResolvingData } |
| 48 | hasResolvedData={ hasResolvedData } |
| 49 | templateLock={ true } |
| 50 | clientId={ clientId } |
| 51 | contextCallback={ ( post ) => ( { |
| 52 | postType: post.type, |
| 53 | postId: post.id, |
| 54 | } ) } |
| 55 | /> |
| 56 | </div> |
| 57 | ); |
| 58 | } |
| 59 |