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