PluginProbe
ElasticPress / 5.0.1
ElasticPress v5.0.1
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / assets / js / blocks / related-posts / Edit.js

Edit.js in ElasticPress 5.0.1, at assets/js/blocks/related-posts/Edit.js

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