diff --git a/assets/js/blocks/related-posts/Edit.js b/assets/js/blocks/related-posts/Edit.js index 491102f966..17816f01dc 100644 --- a/assets/js/blocks/related-posts/Edit.js +++ b/assets/js/blocks/related-posts/Edit.js @@ -1,109 +1,105 @@ /** * WordPress dependencies. */ +import apiFetch from '@wordpress/api-fetch'; import { AlignmentToolbar, BlockControls, InspectorControls } from '@wordpress/block-editor'; import { PanelBody, Placeholder, Spinner, QueryControls } from '@wordpress/components'; -import { Fragment, Component, RawHTML } from '@wordpress/element'; +import { Fragment, RawHTML, useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { addQueryArgs } from '@wordpress/url'; /** - * Edit component + * Related Posts block edit component. + * + * @param {object} props Component props. + * @param {object} props.attributes Block attributes. + * @param {string} props.className Additional CSS class(es). + * @param {object} props.context Block context, + * @param {Function} props.setAttributes Attribute setter. + * @returns {Function} Component element. */ -class Edit extends Component { +const RelatedPostsEdit = ({ attributes, className, context, setAttributes }) => { + const { alignment, number } = attributes; + const [posts, setPosts] = useState(false); + /** - * Setup class - * - * @param {object} props Component properties + * Related posts, limited by the selected number. */ - constructor(props) { - super(props); - - this.state = { - posts: false, - }; - } + const displayPosts = posts.length > number ? posts.slice(0, number) : posts; /** - * Load preview data + * Initialize block. */ - componentDidMount() { + const handleInit = () => { const urlArgs = { number: 100, }; - // Use 0 if in the Widgets Screen - const { context: { postId = 0 } = {} } = this.props; + const { postId = 0 } = context; - wp.apiFetch({ + apiFetch({ path: addQueryArgs(`/wp/v2/posts/${postId}/related`, urlArgs), }) .then((posts) => { - this.setState({ posts }); + setPosts(posts); }) .catch(() => { - this.setState({ posts: false }); + setPosts(false); }); - } + }; - render() { - const { - attributes: { alignment, number }, - setAttributes, - className, - } = this.props; - const { posts } = this.state; - - const displayPosts = posts.length > number ? posts.slice(0, number) : posts; + /** + * Effects. + */ + useEffect(handleInit, [context]); - return ( - - - setAttributes({ alignment: newValue })} + return ( + + + setAttributes({ alignment: newValue })} + /> + + + + setAttributes({ number: value })} /> - - - - setAttributes({ number: value })} - /> - - + + -
- {displayPosts === false || displayPosts.length === 0 ? ( - - {posts === false ? ( - - ) : ( - __('No related posts yet.', 'elasticpress') - )} - - ) : ( - - )} -
-
- ); - } -} +
+ {displayPosts === false || displayPosts.length === 0 ? ( + + {posts === false ? ( + + ) : ( + __('No related posts yet.', 'elasticpress') + )} + + ) : ( + + )} +
+ + ); +}; -export default Edit; +export default RelatedPostsEdit; diff --git a/tests/cypress/integration/features/related-posts.spec.js b/tests/cypress/integration/features/related-posts.spec.js index b94c7e1e53..aabadcf51c 100644 --- a/tests/cypress/integration/features/related-posts.spec.js +++ b/tests/cypress/integration/features/related-posts.spec.js @@ -70,6 +70,12 @@ describe('Related Posts Feature', () => { .should('contain', 'Test related posts block #') .should('have.length', 2); + /** + * Clicking a related post link in the editor shouldn't change the URL. + */ + cy.get('@block').find('a').first().click(); + cy.url().should('include', 'wp-admin/post.php'); + /** * Update the post and visit the front end. */