| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
import { AlignmentToolbar, BlockControls, InspectorControls } from '@wordpress/block-editor'; |
| 6 |
import { PanelBody, Placeholder, Spinner, QueryControls } from '@wordpress/components'; |
| 7 |
import { Fragment, RawHTML, useEffect, useState } from '@wordpress/element'; |
| 8 |
import { __ } from '@wordpress/i18n'; |
| 9 |
import { addQueryArgs } from '@wordpress/url'; |
| 10 |
|
| 11 |
/** |
| 12 |
* Related Posts block edit component. |
| 13 |
* |
| 14 |
* @param {object} props Component props. |
| 15 |
* @param {object} props.attributes Block attributes. |
| 16 |
* @param {string} props.className Additional CSS class(es). |
| 17 |
* @param {object} props.context Block context, |
| 18 |
* @param {Function} props.setAttributes Attribute setter. |
| 19 |
* @returns {Function} Component element. |
| 20 |
*/ |
| 21 |
const RelatedPostsEdit = ({ attributes, className, context, setAttributes }) => { |
| 22 |
const { alignment, number } = attributes; |
| 23 |
const [posts, setPosts] = useState(false); |
| 24 |
|
| 25 |
/** |
| 26 |
* Related posts, limited by the selected number. |
| 27 |
*/ |
| 28 |
const displayPosts = posts.length > number ? posts.slice(0, number) : posts; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initialize block. |
| 32 |
*/ |
| 33 |
const handleInit = () => { |
| 34 |
const urlArgs = { |
| 35 |
number: 100, |
| 36 |
}; |
| 37 |
|
| 38 |
const { postId = 0 } = context; |
| 39 |
|
| 40 |
apiFetch({ |
| 41 |
path: addQueryArgs(`/wp/v2/posts/${postId}/related`, urlArgs), |
| 42 |
}) |
| 43 |
.then((posts) => { |
| 44 |
setPosts(posts); |
| 45 |
}) |
| 46 |
.catch(() => { |
| 47 |
setPosts(false); |
| 48 |
}); |
| 49 |
}; |
| 50 |
|
| 51 |
/** |
| 52 |
* Effects. |
| 53 |
*/ |
| 54 |
useEffect(handleInit, [context]); |
| 55 |
|
| 56 |
return ( |
| 57 |
<Fragment> |
| 58 |
<BlockControls> |
| 59 |
<AlignmentToolbar |
| 60 |
value={alignment} |
| 61 |
onChange={(newValue) => setAttributes({ alignment: newValue })} |
| 62 |
/> |
| 63 |
</BlockControls> |
| 64 |
<InspectorControls> |
| 65 |
<PanelBody title={__('Related Post Settings', 'elasticpress')}> |
| 66 |
<QueryControls |
| 67 |
numberOfItems={number} |
| 68 |
onNumberOfItemsChange={(value) => setAttributes({ number: value })} |
| 69 |
/> |
| 70 |
</PanelBody> |
| 71 |
</InspectorControls> |
| 72 |
|
| 73 |
<div className={className}> |
| 74 |
{displayPosts === false || displayPosts.length === 0 ? ( |
| 75 |
<Placeholder icon="admin-post" label={__('Related Posts', 'elasticpress')}> |
| 76 |
{posts === false ? ( |
| 77 |
<Spinner /> |
| 78 |
) : ( |
| 79 |
__('No related posts yet.', 'elasticpress') |
| 80 |
)} |
| 81 |
</Placeholder> |
| 82 |
) : ( |
| 83 |
<ul style={{ textAlign: alignment }}> |
| 84 |
{displayPosts.map((post) => { |
| 85 |
const titleTrimmed = post.title.rendered.trim(); |
| 86 |
return ( |
| 87 |
<li key={post.id}> |
| 88 |
<a href={post.link} onClick={(e) => e.preventDefault()}> |
| 89 |
{titleTrimmed ? ( |
| 90 |
<RawHTML>{titleTrimmed}</RawHTML> |
| 91 |
) : ( |
| 92 |
__('(Untitled)', 'elasticpress') |
| 93 |
)} |
| 94 |
</a> |
| 95 |
</li> |
| 96 |
); |
| 97 |
})} |
| 98 |
</ul> |
| 99 |
)} |
| 100 |
</div> |
| 101 |
</Fragment> |
| 102 |
); |
| 103 |
}; |
| 104 |
|
| 105 |
export default RelatedPostsEdit; |
| 106 |
|